summaryrefslogtreecommitdiff
path: root/script/core
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2021-08-23 14:58:17 +0800
committer最萌小汐 <sumneko@hotmail.com>2021-08-23 14:58:17 +0800
commit22d7f6c23be209c7b5de149f18efa850f84915f1 (patch)
treeeb5d6feeba3f37bb9c704c2c8f418cff40c8da3f /script/core
parentfdab3eaed0f5e3e85252d5126e1f1927b633c80d (diff)
downloadlua-language-server-22d7f6c23be209c7b5de149f18efa850f84915f1.zip
resolve #624 `different-requires`
Diffstat (limited to 'script/core')
-rw-r--r--script/core/diagnostics/different-requires.lua52
1 files changed, 52 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