diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2021-08-23 14:58:17 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2021-08-23 14:58:17 +0800 |
commit | 22d7f6c23be209c7b5de149f18efa850f84915f1 (patch) | |
tree | eb5d6feeba3f37bb9c704c2c8f418cff40c8da3f /script | |
parent | fdab3eaed0f5e3e85252d5126e1f1927b633c80d (diff) | |
download | lua-language-server-22d7f6c23be209c7b5de149f18efa850f84915f1.zip |
resolve #624 `different-requires`
Diffstat (limited to 'script')
-rw-r--r-- | script/core/diagnostics/different-requires.lua | 52 | ||||
-rw-r--r-- | script/parser/guide.lua | 3 | ||||
-rw-r--r-- | script/proto/define.lua | 2 |
3 files changed, 57 insertions, 0 deletions
diff --git a/script/core/diagnostics/different-requires.lua b/script/core/diagnostics/different-requires.lua new file mode 100644 index 00000000..9e3dfc8f --- /dev/null +++ b/script/core/diagnostics/different-requires.lua @@ -0,0 +1,52 @@ +local files = require 'files' +local guide = require 'parser.guide' +local lang = require 'language' +local config = require 'config' +local vm = require 'vm' +local ws = require 'workspace' + +return function (uri, callback) + local state = files.getState(uri) + if not state then + return + end + local cache = vm.getCache 'different-requires' + guide.eachSpecialOf(state.ast, 'require', function (source) + local call = source.next + if not call or call.type ~= 'call' then + return + end + local arg1 = call.args and call.args[1] + if not arg1 or arg1.type ~= 'string' then + return + end + local literal = arg1[1] + local results = ws.findUrisByRequirePath(literal) + local result = results and results[1] + if not result then + return + end + local other = cache[result] + if not other then + cache[result] = { + source = arg1, + require = literal, + } + return + end + if other.require ~= literal then + callback { + start = arg1.start, + finish = arg1.finish, + related = { + { + start = other.source.start, + finish = other.source.finish, + uri = guide.getUri(other.source), + } + }, + message = lang.script('DIAG_DIFFERENT_REQUIRES'), + } + end + end) +end diff --git a/script/parser/guide.lua b/script/parser/guide.lua index 378d2613..6dcfbf95 100644 --- a/script/parser/guide.lua +++ b/script/parser/guide.lua @@ -722,6 +722,9 @@ function m.eachSource(ast, callback) end --- 获取指定的 special +---@param ast parser.guide.object +---@param name string +---@param callback fun(source: parser.guide.object) function m.eachSpecialOf(ast, name, callback) local root = m.getRoot(ast) if not root.specials then diff --git a/script/proto/define.lua b/script/proto/define.lua index a07a3903..9fbbea35 100644 --- a/script/proto/define.lua +++ b/script/proto/define.lua @@ -74,6 +74,7 @@ m.DiagnosticDefaultSeverity = { ['count-down-loop'] = 'Warning', ['no-implicit-any'] = 'Information', ['deprecated'] = 'Warning', + ['different-requires'] = 'Warning', ['duplicate-doc-class'] = 'Warning', ['undefined-doc-class'] = 'Warning', @@ -125,6 +126,7 @@ m.DiagnosticDefaultNeededFileStatus = { ['count-down-loop'] = 'Any', ['no-implicit-any'] = 'None', ['deprecated'] = 'Opened', + ['different-requires'] = 'Any', ['duplicate-doc-class'] = 'Any', ['undefined-doc-class'] = 'Any', |