summaryrefslogtreecommitdiff
path: root/script/core
diff options
context:
space:
mode:
authorAndreaWalchshoferSCCH <122894794+AndreaWalchshoferSCCH@users.noreply.github.com>2023-03-30 14:49:30 +0200
committerGitHub <noreply@github.com>2023-03-30 14:49:30 +0200
commit43794193ef981ffecb477bfce444428ded852aa0 (patch)
tree956e4c3feb41a309e1345c48aaff987501a483c2 /script/core
parent429c48fcb453aeb2de0df55492895d51ad9596f0 (diff)
downloadlua-language-server-43794193ef981ffecb477bfce444428ded852aa0.zip
Add diagnostic to enforce lowercase local element names (#2)
Diffstat (limited to 'script/core')
-rw-r--r--script/core/diagnostics/uppercase-local.lua47
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