summaryrefslogtreecommitdiff
path: root/script/core/diagnostics/duplicate-set-field.lua
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2021-02-23 17:46:30 +0800
committer最萌小汐 <sumneko@hotmail.com>2021-02-23 17:46:30 +0800
commit54876cb601466c409eb0be1af489c7ceca7f22cf (patch)
tree5f163db31216d283347246c04b6fe469dd4c7d76 /script/core/diagnostics/duplicate-set-field.lua
parentea72b346a69e29110482e55044b8dcf02c378863 (diff)
downloadlua-language-server-54876cb601466c409eb0be1af489c7ceca7f22cf.zip
close #415 new diagnostic: `duplicate-set-field`
Diffstat (limited to 'script/core/diagnostics/duplicate-set-field.lua')
-rw-r--r--script/core/diagnostics/duplicate-set-field.lua90
1 files changed, 90 insertions, 0 deletions
diff --git a/script/core/diagnostics/duplicate-set-field.lua b/script/core/diagnostics/duplicate-set-field.lua
new file mode 100644
index 00000000..bf7fd069
--- /dev/null
+++ b/script/core/diagnostics/duplicate-set-field.lua
@@ -0,0 +1,90 @@
+local files = require 'files'
+local guide = require 'parser.guide'
+local lang = require 'language'
+local define = require 'proto.define'
+local vm = require 'vm'
+
+return function (uri, callback)
+ local ast = files.getAst(uri)
+ if not ast then
+ return
+ end
+
+ guide.eachSourceType(ast.ast, 'local', function (source)
+ if not source.ref then
+ return
+ end
+ local sets = {}
+ for _, ref in ipairs(source.ref) do
+ if ref.type ~= 'getlocal' then
+ goto CONTINUE
+ end
+ local nxt = ref.next
+ if not nxt then
+ goto CONTINUE
+ end
+ if nxt.type == 'setfield'
+ or nxt.type == 'setmethod'
+ or nxt.type == 'setindex' then
+ local name = guide.getKeyName(nxt)
+ if not name then
+ goto CONTINUE
+ end
+ if not sets[name] then
+ sets[name] = {}
+ end
+ sets[name][#sets[name]+1] = nxt
+ end
+ ::CONTINUE::
+ end
+ for name, values in pairs(sets) do
+ if #values <= 1 then
+ goto CONTINUE_SETS
+ end
+ local blocks = {}
+ for _, value in ipairs(values) do
+ local block = guide.getBlock(value)
+ if not blocks[block] then
+ blocks[block] = {}
+ end
+ blocks[block][#blocks[block]+1] = value
+ end
+ for _, defs in pairs(blocks) do
+ if #defs <= 1 then
+ goto CONTINUE_BLOCKS
+ end
+ local related = {}
+ for i = 1, #defs do
+ local def = defs[i]
+ related[i] = {
+ start = def.start,
+ finish = def.finish,
+ uri = uri,
+ }
+ end
+ for i = 1, #defs - 1 do
+ local def = defs[i]
+ callback {
+ start = def.start,
+ finish = def.finish,
+ related = related,
+ message = lang.script('DIAG_DUPLICATE_SET_FIELD', name),
+ level = define.DiagnosticSeverity.Hint,
+ tags = { define.DiagnosticTag.Unnecessary },
+ }
+ end
+ for i = #defs, #defs do
+ local def = defs[i]
+ callback {
+ start = def.start,
+ finish = def.finish,
+ related = related,
+ message = lang.script('DIAG_DUPLICATE_SET_FIELD', name),
+ }
+ end
+ ::CONTINUE_BLOCKS::
+ end
+ ::CONTINUE_SETS::
+ end
+ end)
+end