diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2023-03-31 14:20:34 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-31 14:20:34 +0800 |
commit | ac3d06d74bbbdd9650e5261888cd6d31d41f7338 (patch) | |
tree | cba599a339833b09eddc06dc13369b7d03740d63 /script | |
parent | b281facd333f6f10b46a2b0e84f30688bb24772e (diff) | |
parent | a44c0e51996c44d98e11ebdc8839d5e62589d2a4 (diff) | |
download | lua-language-server-ac3d06d74bbbdd9650e5261888cd6d31d41f7338.zip |
Merge pull request #2040 from AlWoSp/master
Add optional diagnostic warning about any global element
Diffstat (limited to 'script')
-rw-r--r-- | script/core/diagnostics/global-element.lua | 57 | ||||
-rw-r--r-- | script/proto/diagnostic.lua | 8 |
2 files changed, 65 insertions, 0 deletions
diff --git a/script/core/diagnostics/global-element.lua b/script/core/diagnostics/global-element.lua new file mode 100644 index 00000000..e9dd46ce --- /dev/null +++ b/script/core/diagnostics/global-element.lua @@ -0,0 +1,57 @@ +local files = require 'files' +local guide = require 'parser.guide' +local lang = require 'language' +local config = require 'config' +local vm = require 'vm' +local util = require 'utility' + +local function isDocClass(source) + if not source.bindDocs then + return false + end + for _, doc in ipairs(source.bindDocs) do + if doc.type == 'doc.class' then + return true + end + end + return false +end + +-- If global elements are discouraged by coding convention, this diagnostic helps with reminding about that +-- Exceptions may be added to Lua.diagnostics.globals +return function (uri, callback) + local ast = files.getState(uri) + if not ast then + return + end + + local definedGlobal = util.arrayToHash(config.get(uri, 'Lua.diagnostics.globals')) + + guide.eachSourceType(ast.ast, 'setglobal', function (source) + local name = guide.getKeyName(source) + if not name or definedGlobal[name] then + return + end + -- If the assignment is marked as doc.class, then it is considered allowed + if isDocClass(source) then + return + end + if definedGlobal[name] == nil then + definedGlobal[name] = false + local global = vm.getGlobal('variable', name) + if global then + for _, set in ipairs(global:getSets(uri)) do + if vm.isMetaFile(guide.getUri(set)) then + definedGlobal[name] = true + return + end + end + end + end + callback { + start = source.start, + finish = source.finish, + message = lang.script.DIAG_GLOBAL_ELEMENT, + } + end) +end diff --git a/script/proto/diagnostic.lua b/script/proto/diagnostic.lua index ceb42f8b..24ab0814 100644 --- a/script/proto/diagnostic.lua +++ b/script/proto/diagnostic.lua @@ -172,6 +172,14 @@ m.register { } m.register { + 'global-element', +} { + group = 'conventions', + severity = 'Warning', + status = 'None' +} + +m.register { 'duplicate-index', } { group = 'duplicate', |