diff options
-rw-r--r-- | locale/en-US/script.lni | 1 | ||||
-rw-r--r-- | locale/zh-CN/script.lni | 1 | ||||
-rw-r--r-- | script-beta/core/diagnostics/code-after-break.lua | 34 | ||||
-rw-r--r-- | script-beta/proto/define.lua | 1 | ||||
-rw-r--r-- | test-beta/diagnostics/init.lua | 8 |
5 files changed, 45 insertions, 0 deletions
diff --git a/locale/en-US/script.lni b/locale/en-US/script.lni index ac792282..c8e832a5 100644 --- a/locale/en-US/script.lni +++ b/locale/en-US/script.lni @@ -33,6 +33,7 @@ DIAG_NEED_CLASS = 'Class needs to be defined first.' DIAG_DUPLICATE_FIELD = 'Duplicate field.' DIAG_SET_CONST = 'Assignment to const variable.' DIAG_SET_FOR_STATE = 'Assignment to for-state variable.' +DIAG_CODE_AFTER_BREAK = 'Unable to execute code after `break`.' DIAG_CIRCLE_DOC_CLASS = 'Circularly inherited classes.' DIAG_DOC_FIELD_NO_CLASS = 'The field must be defined after the class.' diff --git a/locale/zh-CN/script.lni b/locale/zh-CN/script.lni index c31c7e89..b22b6be3 100644 --- a/locale/zh-CN/script.lni +++ b/locale/zh-CN/script.lni @@ -33,6 +33,7 @@ DIAG_NEED_CLASS = '需要先定义 Class 。' DIAG_DUPLICATE_FIELD = '重复定义的 field 。' DIAG_SET_CONST = '不能对常量赋值。' DIAG_SET_FOR_STATE = '修改了循环变量。' +DIAG_CODE_AFTER_BREAK = '无法执行到 `break` 后的代码。' DIAG_CIRCLE_DOC_CLASS = '循环继承的类。' DIAG_DOC_FIELD_NO_CLASS = '字段必须定义在类之后。' diff --git a/script-beta/core/diagnostics/code-after-break.lua b/script-beta/core/diagnostics/code-after-break.lua new file mode 100644 index 00000000..a2bac8a4 --- /dev/null +++ b/script-beta/core/diagnostics/code-after-break.lua @@ -0,0 +1,34 @@ +local files = require 'files' +local guide = require 'parser.guide' +local lang = require 'language' +local define = require 'proto.define' + +return function (uri, callback) + local state = files.getAst(uri) + if not state then + return + end + + local mark = {} + guide.eachSourceType(state.ast, 'break', function (source) + local list = source.parent + if mark[list] then + return + end + mark[list] = true + for i = #list, 1, -1 do + local src = list[i] + if src == source then + if i == #list then + return + end + callback { + start = list[i+1].start, + finish = list[#list].range or list[#list].finish, + tags = { define.DiagnosticTag.Unnecessary }, + message = lang.script.DIAG_CODE_AFTER_BREAK, + } + end + end + end) +end diff --git a/script-beta/proto/define.lua b/script-beta/proto/define.lua index 4dd5215e..966a5161 100644 --- a/script-beta/proto/define.lua +++ b/script-beta/proto/define.lua @@ -135,6 +135,7 @@ m.DiagnosticDefaultSeverity = { ['duplicate-index'] = 'Warning', ['empty-block'] = 'Hint', ['redundant-value'] = 'Hint', + ['code-after-break'] = 'Hint', ['duplicate-doc-class'] = 'Warning', ['undefined-doc-class'] = 'Warning', diff --git a/test-beta/diagnostics/init.lua b/test-beta/diagnostics/init.lua index 579a132b..8b4ba219 100644 --- a/test-beta/diagnostics/init.lua +++ b/test-beta/diagnostics/init.lua @@ -632,6 +632,14 @@ table.insert({}, 1, 2, <!3!>) ]] TEST [[ +while true do + break + <!print() + print()!> +end +]] + +TEST [[ ---@class <!Class!> ---@class <!Class!> ]] |