diff options
author | AndreaWalchshoferSCCH <122894794+AndreaWalchshoferSCCH@users.noreply.github.com> | 2023-03-30 14:49:30 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-30 14:49:30 +0200 |
commit | 43794193ef981ffecb477bfce444428ded852aa0 (patch) | |
tree | 956e4c3feb41a309e1345c48aaff987501a483c2 | |
parent | 429c48fcb453aeb2de0df55492895d51ad9596f0 (diff) | |
download | lua-language-server-43794193ef981ffecb477bfce444428ded852aa0.zip |
Add diagnostic to enforce lowercase local element names (#2)
-rw-r--r-- | doc/en-us/config.md | 15 | ||||
-rw-r--r-- | doc/pt-br/config.md | 15 | ||||
-rw-r--r-- | doc/zh-cn/config.md | 15 | ||||
-rw-r--r-- | doc/zh-tw/config.md | 15 | ||||
-rw-r--r-- | locale/en-us/script.lua | 5 | ||||
-rw-r--r-- | locale/en-us/setting.lua | 6 | ||||
-rw-r--r-- | locale/pt-br/script.lua | 4 | ||||
-rw-r--r-- | locale/pt-br/setting.lua | 6 | ||||
-rw-r--r-- | locale/zh-cn/script.lua | 4 | ||||
-rw-r--r-- | locale/zh-cn/setting.lua | 6 | ||||
-rw-r--r-- | locale/zh-tw/script.lua | 4 | ||||
-rw-r--r-- | locale/zh-tw/setting.lua | 6 | ||||
-rw-r--r-- | script/core/diagnostics/uppercase-local.lua | 47 | ||||
-rw-r--r-- | script/proto/diagnostic.lua | 1 | ||||
-rw-r--r-- | test/diagnostics/init.lua | 3 | ||||
-rw-r--r-- | test/diagnostics/uppercase-local.lua | 66 |
16 files changed, 197 insertions, 21 deletions
diff --git a/doc/en-us/config.md b/doc/en-us/config.md index 0e5d09ee..96a023b9 100644 --- a/doc/en-us/config.md +++ b/doc/en-us/config.md @@ -329,6 +329,7 @@ Array<string> * ``"unused-label"`` * ``"unused-local"`` * ``"unused-vararg"`` +* ``"uppercase-local"`` ## default @@ -470,6 +471,7 @@ object<string, string> "strict": "Fallback", /* * global-element + * uppercase-local */ "strict-convention": "None", /* @@ -593,6 +595,7 @@ object<string, string> "strict": "Fallback", /* * global-element + * uppercase-local */ "strict-convention": "Fallback", /* @@ -926,7 +929,11 @@ object<string, string> /* Enable unused vararg diagnostics. */ - "unused-vararg": "Opened" + "unused-vararg": "Opened", + /* + Enable diagnostics to warn about local element names starting with an uppercase letter. + */ + "uppercase-local": "None" } ``` @@ -1178,7 +1185,11 @@ object<string, string> /* Enable unused vararg diagnostics. */ - "unused-vararg": "Hint" + "unused-vararg": "Hint", + /* + Enable diagnostics to warn about local element names starting with an uppercase letter. + */ + "uppercase-local": "Warning" } ``` diff --git a/doc/pt-br/config.md b/doc/pt-br/config.md index 762f2259..952980d8 100644 --- a/doc/pt-br/config.md +++ b/doc/pt-br/config.md @@ -329,6 +329,7 @@ Array<string> * ``"unused-label"`` * ``"unused-local"`` * ``"unused-vararg"`` +* ``"uppercase-local"`` ## default @@ -470,6 +471,7 @@ object<string, string> "strict": "Fallback", /* * global-element + * uppercase-local */ "strict-convention": "None", /* @@ -593,6 +595,7 @@ object<string, string> "strict": "Fallback", /* * global-element + * uppercase-local */ "strict-convention": "Fallback", /* @@ -926,7 +929,11 @@ object<string, string> /* 未使用的不定参数 */ - "unused-vararg": "Opened" + "unused-vararg": "Opened", + /* + Enable diagnostics to warn about local element names starting with an uppercase letter. + */ + "uppercase-local": "None" } ``` @@ -1178,7 +1185,11 @@ object<string, string> /* 未使用的不定参数 */ - "unused-vararg": "Hint" + "unused-vararg": "Hint", + /* + Enable diagnostics to warn about local element names starting with an uppercase letter. + */ + "uppercase-local": "Warning" } ``` diff --git a/doc/zh-cn/config.md b/doc/zh-cn/config.md index 869e2506..294fe046 100644 --- a/doc/zh-cn/config.md +++ b/doc/zh-cn/config.md @@ -329,6 +329,7 @@ Array<string> * ``"unused-label"`` * ``"unused-local"`` * ``"unused-vararg"`` +* ``"uppercase-local"`` ## default @@ -470,6 +471,7 @@ object<string, string> "strict": "Fallback", /* * global-element + * uppercase-local */ "strict-convention": "None", /* @@ -593,6 +595,7 @@ object<string, string> "strict": "Fallback", /* * global-element + * uppercase-local */ "strict-convention": "Fallback", /* @@ -926,7 +929,11 @@ object<string, string> /* 未使用的不定参数 */ - "unused-vararg": "Opened" + "unused-vararg": "Opened", + /* + Enable diagnostics to warn about local element names starting with an uppercase letter. + */ + "uppercase-local": "None" } ``` @@ -1177,7 +1184,11 @@ object<string, string> /* 未使用的不定参数 */ - "unused-vararg": "Hint" + "unused-vararg": "Hint", + /* + Enable diagnostics to warn about local element names starting with an uppercase letter. + */ + "uppercase-local": "Warning" } ``` diff --git a/doc/zh-tw/config.md b/doc/zh-tw/config.md index c922ad6b..e12bfde7 100644 --- a/doc/zh-tw/config.md +++ b/doc/zh-tw/config.md @@ -329,6 +329,7 @@ Array<string> * ``"unused-label"`` * ``"unused-local"`` * ``"unused-vararg"`` +* ``"uppercase-local"`` ## default @@ -470,6 +471,7 @@ object<string, string> "strict": "Fallback", /* * global-element + * uppercase-local */ "strict-convention": "None", /* @@ -593,6 +595,7 @@ object<string, string> "strict": "Fallback", /* * global-element + * uppercase-local */ "strict-convention": "Fallback", /* @@ -926,7 +929,11 @@ object<string, string> /* 未使用的不定引數 */ - "unused-vararg": "Opened" + "unused-vararg": "Opened", + /* + Enable diagnostics to warn about local element names starting with an uppercase letter. + */ + "uppercase-local": "None" } ``` @@ -1177,7 +1184,11 @@ object<string, string> /* 未使用的不定引數 */ - "unused-vararg": "Hint" + "unused-vararg": "Hint", + /* + Enable diagnostics to warn about local element names starting with an uppercase letter. + */ + "uppercase-local": "Warning" } ``` diff --git a/locale/en-us/script.lua b/locale/en-us/script.lua index 2a13214f..e7f4b98b 100644 --- a/locale/en-us/script.lua +++ b/locale/en-us/script.lua @@ -150,8 +150,11 @@ DIAG_INVISIBLE_PROTECTED = 'Field `{field}` is protected, it can only be accessed in class `{class}` and its subclasses.' DIAG_INVISIBLE_PACKAGE = 'Field `{field}` can only be accessed in same file `{uri}`.' -DIAG_GLOBAL_ELEMENT = +DIAG_GLOBAL_ELEMENT = 'Element is global.' +DIAG_UPPERCASE_LOCAL = +'Local element `{}` must start with lowercase letter' + MWS_NOT_SUPPORT = '{} does not support multi workspace for now, I may need to restart to support the new workspace ...' diff --git a/locale/en-us/setting.lua b/locale/en-us/setting.lua index 9f606cd7..b03e3e43 100644 --- a/locale/en-us/setting.lua +++ b/locale/en-us/setting.lua @@ -415,8 +415,10 @@ config.diagnostics['unknown-operator'] = 'Enable diagnostics for unknown operators.' config.diagnostics['unreachable-code'] = 'Enable diagnostics for unreachable code.' -config.diagnostics['global-element'] = -'Enable diagnostics to warn about global elements.' +config.diagnostics['global-element'] = +'Enable diagnostics to warn about global variables.' +config.diagnostics['uppercase-local'] = +'Enable diagnostics to warn about local element names starting with an uppercase letter.' config.typeFormat.config = 'Configures the formatting behavior while typing Lua code.' config.typeFormat.config.auto_complete_end = diff --git a/locale/pt-br/script.lua b/locale/pt-br/script.lua index affcd893..6497c520 100644 --- a/locale/pt-br/script.lua +++ b/locale/pt-br/script.lua @@ -150,8 +150,10 @@ DIAG_INVISIBLE_PROTECTED = -- TODO: need translate! 'Field `{field}` is protected, it can only be accessed in class `{class}` and its subclasses.' DIAG_INVISIBLE_PACKAGE = -- TODO: need translate! 'Field `{field}` can only be accessed in same file `{uri}`.' -DIAG_GLOBAL_ELEMENT = -- TODO: need translate! +DIAG_GLOBAL_ELEMENT = -- TODO: need translate! 'Element is global.' +DIAG_UPPERCASE_LOCAL = -- TODO: need translate! +'Local element `{}` must start with lowercase letter' MWS_NOT_SUPPORT = '{} não é suportado múltiplos espaços de trabalho por enquanto, posso precisar reiniciar para estabelecer um novo espaço de trabalho ...' diff --git a/locale/pt-br/setting.lua b/locale/pt-br/setting.lua index 95f26b76..ec94f575 100644 --- a/locale/pt-br/setting.lua +++ b/locale/pt-br/setting.lua @@ -415,8 +415,10 @@ config.diagnostics['unknown-operator'] = -- TODO: need translate! 'Enable diagnostics for unknown operators.' config.diagnostics['unreachable-code'] = -- TODO: need translate! 'Enable diagnostics for unreachable code.' -config.diagnostics['global-element'] = -- TODO: need translate! -'Enable diagnostics to warn about global elements.' +config.diagnostics['global-element'] = -- TODO: need translate! +'Enable diagnostics to warn about global variables.' +config.diagnostics['uppercase-local'] = -- TODO: need translate! +'Enable diagnostics to warn about local element names starting with an uppercase letter.' config.typeFormat.config = -- TODO: need translate! 'Configures the formatting behavior while typing Lua code.' config.typeFormat.config.auto_complete_end = -- TODO: need translate! diff --git a/locale/zh-cn/script.lua b/locale/zh-cn/script.lua index f6f639ea..da817698 100644 --- a/locale/zh-cn/script.lua +++ b/locale/zh-cn/script.lua @@ -150,8 +150,10 @@ DIAG_INVISIBLE_PROTECTED = '字段 `{field}` 受到保护,只能在 `{class}` 类极其子类中才能访问。' DIAG_INVISIBLE_PACKAGE = '字段 `{field}` 只能在相同的文件 `{uri}` 中才能访问。' -DIAG_GLOBAL_ELEMENT = -- TODO: need translate! +DIAG_GLOBAL_ELEMENT = -- TODO: need translate! 'Element is global.' +DIAG_UPPERCASE_LOCAL = -- TODO: need translate! +'Local element `{}` must start with lowercase letter' MWS_NOT_SUPPORT = '{} 目前还不支持多工作目录,我可能需要重启才能支持新的工作目录...' diff --git a/locale/zh-cn/setting.lua b/locale/zh-cn/setting.lua index 731190e2..e1eb7de9 100644 --- a/locale/zh-cn/setting.lua +++ b/locale/zh-cn/setting.lua @@ -414,8 +414,10 @@ config.diagnostics['unknown-operator'] = -- TODO: need translate! 'Enable diagnostics for unknown operators.' config.diagnostics['unreachable-code'] = -- TODO: need translate! 'Enable diagnostics for unreachable code.' -config.diagnostics['global-element'] = -- TODO: need translate! -'Enable diagnostics to warn about global elements.' +config.diagnostics['global-element'] = -- TODO: need translate! +'Enable diagnostics to warn about global variables.' +config.diagnostics['uppercase-local'] = -- TODO: need translate! +'Enable diagnostics to warn about local element names starting with an uppercase letter.' config.typeFormat.config = -- TODO: need translate! 'Configures the formatting behavior while typing Lua code.' config.typeFormat.config.auto_complete_end = -- TODO: need translate! diff --git a/locale/zh-tw/script.lua b/locale/zh-tw/script.lua index 4d9a89ad..77ff6fea 100644 --- a/locale/zh-tw/script.lua +++ b/locale/zh-tw/script.lua @@ -150,8 +150,10 @@ DIAG_INVISIBLE_PROTECTED = -- TODO: need translate! 'Field `{field}` is protected, it can only be accessed in class `{class}` and its subclasses.' DIAG_INVISIBLE_PACKAGE = -- TODO: need translate! 'Field `{field}` can only be accessed in same file `{uri}`.' -DIAG_GLOBAL_ELEMENT = -- TODO: need translate! +DIAG_GLOBAL_ELEMENT = -- TODO: need translate! 'Element is global.' +DIAG_UPPERCASE_LOCAL = -- TODO: need translate! +'Local element `{}` must start with lowercase letter' MWS_NOT_SUPPORT = '{} 目前還不支援多工作目錄,我可能需要重新啟動才能支援新的工作目錄...' diff --git a/locale/zh-tw/setting.lua b/locale/zh-tw/setting.lua index fd363d3f..ebf615ac 100644 --- a/locale/zh-tw/setting.lua +++ b/locale/zh-tw/setting.lua @@ -414,8 +414,10 @@ config.diagnostics['unknown-operator'] = -- TODO: need translate! 'Enable diagnostics for unknown operators.' config.diagnostics['unreachable-code'] = -- TODO: need translate! 'Enable diagnostics for unreachable code.' -config.diagnostics['global-element'] = -- TODO: need translate! -'Enable diagnostics to warn about global elements.' +config.diagnostics['global-element'] = -- TODO: need translate! +'Enable diagnostics to warn about global variables.' +config.diagnostics['uppercase-local'] = -- TODO: need translate! +'Enable diagnostics to warn about local element names starting with an uppercase letter.' config.typeFormat.config = -- TODO: need translate! 'Configures the formatting behavior while typing Lua code.' config.typeFormat.config.auto_complete_end = -- TODO: need translate! diff --git a/script/core/diagnostics/uppercase-local.lua b/script/core/diagnostics/uppercase-local.lua new file mode 100644 index 00000000..cc377dd2 --- /dev/null +++ b/script/core/diagnostics/uppercase-local.lua @@ -0,0 +1,47 @@ +local files = require 'files' +local guide = require 'parser.guide' +local lang = require 'language' + +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 local elements must be named lowercase by coding convention, this diagnostic helps with reminding about that +return function (uri, callback) + local ast = files.getState(uri) + if not ast then + return + end + + guide.eachSourceType(ast.ast, 'local', function (source) + local name = guide.getKeyName(source) + if not name then + return + end + local first = name:match '%w' -- alphanumeric character + if not first then + return + end + if not first:match '%u' then -- uppercase + return + end + -- If the assignment is marked as doc.class, then it is considered allowed + if isDocClass(source) then + return + end + + callback { + start = source.start, + finish = source.finish, + message = lang.script('DIAG_UPPERCASE_LOCAL', name), + } + end) +end diff --git a/script/proto/diagnostic.lua b/script/proto/diagnostic.lua index bea7c160..a6232b8a 100644 --- a/script/proto/diagnostic.lua +++ b/script/proto/diagnostic.lua @@ -173,6 +173,7 @@ m.register { m.register { 'global-element', + 'uppercase-local', } { group = 'strict-conventions', severity = 'Warning', diff --git a/test/diagnostics/init.lua b/test/diagnostics/init.lua index 4025dd8c..a18e5cea 100644 --- a/test/diagnostics/init.lua +++ b/test/diagnostics/init.lua @@ -86,4 +86,5 @@ end require 'diagnostics.common' require 'diagnostics.type-check' -require 'diagnostics.global-element'
\ No newline at end of file +require 'diagnostics.global-element' +require 'diagnostics.uppercase-local'
\ No newline at end of file diff --git a/test/diagnostics/uppercase-local.lua b/test/diagnostics/uppercase-local.lua new file mode 100644 index 00000000..fb8b726d --- /dev/null +++ b/test/diagnostics/uppercase-local.lua @@ -0,0 +1,66 @@ +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', +{ + ['uppercase-local'] = 'Any!' -- override groupFileStatus +}) + +-- check that global elements are not warned about +TEST [[ +Var1 = "global" +VAR2 = "global" + +local x = true +local <!Y!> = false +local <!Var!> = false +]] + +TEST [[ +local function test1() + print() +end + +local function <!Test2!>() + print() +end +]] + +TEST [[ +local function closure1() + local elem1 = 1 + local <!Elem2!> = 2 +end + +local function <!Closure2!>() + local elem1 = 1 + local <!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 |