From 27cfa2e23648b015a94fab6e0938ec42a5a040e5 Mon Sep 17 00:00:00 2001 From: AndreaWalchshoferSCCH <122894794+AndreaWalchshoferSCCH@users.noreply.github.com> Date: Tue, 4 Apr 2023 11:02:59 +0200 Subject: Add diagnostic to enforce lowercase local element names (#6) - diagnostic `uppercase-local` in group `conventions`, disabled by default - isolated unit tests for diagnostic --- script/core/diagnostics/uppercase-local.lua | 47 +++++++++++++++++++++++++++++ script/proto/diagnostic.lua | 1 + 2 files changed, 48 insertions(+) create mode 100644 script/core/diagnostics/uppercase-local.lua (limited to 'script') 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', -- cgit v1.2.3