summaryrefslogtreecommitdiff
path: root/script/core
diff options
context:
space:
mode:
authorAndreaWalchshoferSCCH <122894794+AndreaWalchshoferSCCH@users.noreply.github.com>2023-03-22 09:00:56 +0100
committerGitHub <noreply@github.com>2023-03-22 09:00:56 +0100
commit759e2f8c303ed9985b1a7cb9ad4886d705576476 (patch)
treeb46768fb781b04e65e5aca9890e23a7adc0c7513 /script/core
parent5b6542e8ff276a195c502cc2479b53685429eb2e (diff)
downloadlua-language-server-759e2f8c303ed9985b1a7cb9ad4886d705576476.zip
Add diagnostic warning about any global (#1)
* Add warning for any global variable via diagnostic * Add messages in en-US TODO: add messages in languages other than en-us as well * fallback: enable/disable diagnostics w/ annotation * Add tests for the new diagnostic * Add diagnostic and group to config.md
Diffstat (limited to 'script/core')
-rw-r--r--script/core/diagnostics/global-element.lua57
1 files changed, 57 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