From 759e2f8c303ed9985b1a7cb9ad4886d705576476 Mon Sep 17 00:00:00 2001 From: AndreaWalchshoferSCCH <122894794+AndreaWalchshoferSCCH@users.noreply.github.com> Date: Wed, 22 Mar 2023 09:00:56 +0100 Subject: 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 --- test/diagnostics/global-element.lua | 97 +++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 test/diagnostics/global-element.lua (limited to 'test/diagnostics/global-element.lua') 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 + = "global" + = "global" +]] + +TEST [[ +local function test1() + print() +end + +function () + print() +end +]] + +TEST [[ +local function closure1() + local elem1 = 1 + = 2 +end + +function () + local elem1 = 1 + = 2 +end +]] + +-- add elements to exemption list +config.set(nil, 'Lua.diagnostics.globals', +{ + 'GLOBAL1', + 'GLOBAL2', + 'GLOBAL_CLOSURE' +}) + +TEST [[ +GLOBAL1 = "allowed" + = "not allowed" + = "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 + = 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 -- cgit v1.2.3