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/core | |
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/core')
-rw-r--r-- | script/core/diagnostics/uppercase-local.lua | 47 |
1 files changed, 47 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 |