From 216f3d54901c6759b99850882a549210f0270745 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Mon, 4 Jan 2021 19:29:36 +0800 Subject: new dianostic: `count-down-loop` --- script/core/diagnostics/count-down-loop.lua | 37 +++++++++++++++++++++++++++++ script/proto/define.lua | 14 +++++++++++ 2 files changed, 51 insertions(+) create mode 100644 script/core/diagnostics/count-down-loop.lua (limited to 'script') diff --git a/script/core/diagnostics/count-down-loop.lua b/script/core/diagnostics/count-down-loop.lua new file mode 100644 index 00000000..b64391fd --- /dev/null +++ b/script/core/diagnostics/count-down-loop.lua @@ -0,0 +1,37 @@ +local files = require "files" +local guide = require "parser.guide" +local lang = require 'language' + +return function (uri, callback) + local state = files.getAst(uri) + local text = files.getText(uri) + if not state or not text then + return + end + + guide.eachSourceType(state.ast, 'loop', function (source) + if not source.loc or not source.loc.value then + return + end + local maxNumer = source.max and source.max.type == 'number' and tonumber(source.max[1]) + if maxNumer ~= 1 then + return + end + if not source.step then + callback { + start = source.loc.value.start, + finish = source.max.finish, + message = lang.script('DIAG_COUNT_DOWN_LOOP', ('%s, %s'):format(text:sub(source.loc.value.start, source.max.finish), '-1')) + } + else + local stepNumber = source.step.type == 'number' and tonumber(source.step[1]) + if stepNumber and stepNumber > 0 then + callback { + start = source.loc.value.start, + finish = source.step.finish, + message = lang.script('DIAG_COUNT_DOWN_LOOP', ('%s, -%s'):format(text:sub(source.loc.value.start, source.max.finish), source.step[1])) + } + end + end + end) +end diff --git a/script/proto/define.lua b/script/proto/define.lua index 50f4cd87..f6f8e9cc 100644 --- a/script/proto/define.lua +++ b/script/proto/define.lua @@ -135,7 +135,14 @@ m.DiagnosticSeverity = { Hint = 4, } +---@alias DiagnosticDefaultSeverity +---| '"Hint"' +---| '"Information"' +---| '"Warning"' +---| '"Error"' + --- 诊断类型与默认等级 +---@type table m.DiagnosticDefaultSeverity = { ['unused-local'] = 'Hint', ['unused-function'] = 'Hint', @@ -158,6 +165,7 @@ m.DiagnosticDefaultSeverity = { ['code-after-break'] = 'Hint', ['unbalanced-assignments'] = 'Warning', ['close-non-object'] = 'Warning', + ['count-down-loop'] = 'Warning', ['duplicate-doc-class'] = 'Warning', ['undefined-doc-class'] = 'Warning', @@ -169,6 +177,10 @@ m.DiagnosticDefaultSeverity = { ['duplicate-doc-field'] = 'Warning', } +---@alias DiagnosticDefaultNeededFileStatus +---| '"Any"' +---| '"Opened"' + -- 文件状态 m.FileStatus = { Any = 1, @@ -176,6 +188,7 @@ m.FileStatus = { } --- 诊断类型与需要的文件状态(可以控制只分析打开的文件、还是所有文件) +---@type table m.DiagnosticDefaultNeededFileStatus = { ['unused-local'] = 'Opened', ['unused-function'] = 'Opened', @@ -198,6 +211,7 @@ m.DiagnosticDefaultNeededFileStatus = { ['code-after-break'] = 'Opened', ['unbalanced-assignments'] = 'Any', ['close-non-object'] = 'Any', + ['count-down-loop'] = 'Any', ['duplicate-doc-class'] = 'Any', ['undefined-doc-class'] = 'Any', -- cgit v1.2.3