diff options
author | AndreaWalchshoferSCCH <122894794+AndreaWalchshoferSCCH@users.noreply.github.com> | 2023-03-22 09:00:56 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-22 09:00:56 +0100 |
commit | 759e2f8c303ed9985b1a7cb9ad4886d705576476 (patch) | |
tree | b46768fb781b04e65e5aca9890e23a7adc0c7513 /test | |
parent | 5b6542e8ff276a195c502cc2479b53685429eb2e (diff) | |
download | lua-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 'test')
-rw-r--r-- | test/diagnostics/global-element.lua | 97 | ||||
-rw-r--r-- | test/diagnostics/init.lua | 1 |
2 files changed, 98 insertions, 0 deletions
diff --git a/test/diagnostics/global-element.lua b/test/diagnostics/global-element.lua new file mode 100644 index 00000000..176fda79 --- /dev/null +++ b/test/diagnostics/global-element.lua @@ -0,0 +1,97 @@ +local config = require 'config' +local util = require 'utility' + +-- disable all default groups to make isolated tests +config.set(nil, 'Lua.diagnostics.groupFileStatus', +{ + ['ambiguity'] = 'None', + ['await'] = 'None', + ['codestyle'] = 'None', + ['strict-conventions'] = 'None', + ['duplicate'] = 'None', + ['global'] = 'None', + ['luadoc'] = 'None', + ['redefined'] = 'None', + ['strict'] = 'None', + ['strong'] = 'None', + ['type-check'] = 'None', + ['unbalanced'] = 'None', + ['unused'] = 'None' +}) + +-- enable single diagnostic that is to be tested +config.set(nil, 'Lua.diagnostics.neededFileStatus', +{ + ['global-element'] = 'Any!' -- override groupFileStatus +}) + +-- check that local elements are not warned about +TEST [[ +local x = 123 +x = 321 +<!Y!> = "global" +<!z!> = "global" +]] + +TEST [[ +local function test1() + print() +end + +function <!Test2!>() + print() +end +]] + +TEST [[ +local function closure1() + local elem1 = 1 + <!elem2!> = 2 +end + +function <!Closure2!>() + local elem1 = 1 + <!elem2!> = 2 +end +]] + +-- add elements to exemption list +config.set(nil, 'Lua.diagnostics.globals', +{ + 'GLOBAL1', + 'GLOBAL2', + 'GLOBAL_CLOSURE' +}) + +TEST [[ +GLOBAL1 = "allowed" +<!global2!> = "not allowed" +<!GLOBAL3!> = "not allowed" +]] + +TEST [[ +function GLOBAL1() + print() +end +]] + +TEST [[ +local function closure1() + local elem1 = 1 + GLOBAL1 = 2 +end + +function GLOBAL_CLOSURE() + local elem1 = 1 + GLOBAL2 = 2 + <!elem2!> = 2 +end +]] + +-- reset configurations +config.set(nil, 'Lua.diagnostics.groupFileStatus', +{}) +config.set(nil, 'Lua.diagnostics.neededFileStatus', +{}) +config.set(nil, 'Lua.diagnostics.globals', +{})
\ No newline at end of file diff --git a/test/diagnostics/init.lua b/test/diagnostics/init.lua index 827d4d08..4025dd8c 100644 --- a/test/diagnostics/init.lua +++ b/test/diagnostics/init.lua @@ -86,3 +86,4 @@ end require 'diagnostics.common' require 'diagnostics.type-check' +require 'diagnostics.global-element'
\ No newline at end of file |