diff options
author | AndreaWalchshoferSCCH <122894794+AndreaWalchshoferSCCH@users.noreply.github.com> | 2023-04-04 11:02:59 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-04 11:02:59 +0200 |
commit | 27cfa2e23648b015a94fab6e0938ec42a5a040e5 (patch) | |
tree | e247b1aac92882c72e5c694eb8090f883d43c76c /script | |
parent | f85e039b9e9369fc2936e899c81056837f04403e (diff) | |
download | lua-language-server-27cfa2e23648b015a94fab6e0938ec42a5a040e5.zip |
Add diagnostic to enforce lowercase local element names (#6)
- diagnostic `uppercase-local` in group `conventions`, disabled by
default
- isolated unit tests for diagnostic
Diffstat (limited to 'script')
-rw-r--r-- | script/core/diagnostics/uppercase-local.lua | 47 | ||||
-rw-r--r-- | script/proto/diagnostic.lua | 1 |
2 files changed, 48 insertions, 0 deletions
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 24ab0814..48a9094c 100644 --- a/script/proto/diagnostic.lua +++ b/script/proto/diagnostic.lua @@ -173,6 +173,7 @@ m.register { m.register { 'global-element', + 'uppercase-local', } { group = 'conventions', severity = 'Warning', |