diff options
-rw-r--r-- | locale/en-us/script.lua | 1 | ||||
-rw-r--r-- | locale/zh-cn/script.lua | 1 | ||||
-rw-r--r-- | script/core/diagnostics/undefined-field.lua | 106 | ||||
-rw-r--r-- | script/core/diagnostics/undefined-global.lua | 2 | ||||
-rw-r--r-- | script/parser/guide.lua | 4 | ||||
-rw-r--r-- | script/proto/define.lua | 1 | ||||
-rw-r--r-- | script/vm/eachField.lua | 5 | ||||
-rw-r--r-- | test/diagnostics/init.lua | 118 |
8 files changed, 237 insertions, 1 deletions
diff --git a/locale/en-us/script.lua b/locale/en-us/script.lua index 5bf8597d..20c24c90 100644 --- a/locale/en-us/script.lua +++ b/locale/en-us/script.lua @@ -2,6 +2,7 @@ DIAG_LINE_ONLY_SPACE = 'Line with spaces only.' DIAG_LINE_POST_SPACE = 'Line with postspace.' DIAG_UNUSED_LOCAL = 'Unused local `{}`.' DIAG_UNDEF_GLOBAL = 'Undefined global `{}`.' +DIAG_UNDEF_FIELD = 'Undefined field `{}`.' DIAG_UNDEF_ENV_CHILD = 'Undefined variable `{}` (overloaded `_ENV` ).' DIAG_UNDEF_FENV_CHILD = 'Undefined variable `{}` (inside module).' DIAG_GLOBAL_IN_NIL_ENV = 'Invalid global (`_ENV` is `nil`).' diff --git a/locale/zh-cn/script.lua b/locale/zh-cn/script.lua index c4fd8a7a..3dda7699 100644 --- a/locale/zh-cn/script.lua +++ b/locale/zh-cn/script.lua @@ -2,6 +2,7 @@ DIAG_LINE_ONLY_SPACE = '只有空格的空行。' DIAG_LINE_POST_SPACE = '后置空格。' DIAG_UNUSED_LOCAL = '未使用的局部变量 `{}`。' DIAG_UNDEF_GLOBAL = '未定义的全局变量 `{}`。' +DIAG_UNDEF_FIELD = '未定义的 field `{}`。' DIAG_UNDEF_ENV_CHILD = '未定义的变量 `{}`(重载了 `_ENV` )。' DIAG_UNDEF_FENV_CHILD = '未定义的变量 `{}`(处于模块中)。' DIAG_GLOBAL_IN_NIL_ENV = '不能使用全局变量(`_ENV`被置为了`nil`)。' diff --git a/script/core/diagnostics/undefined-field.lua b/script/core/diagnostics/undefined-field.lua new file mode 100644 index 00000000..d01375dd --- /dev/null +++ b/script/core/diagnostics/undefined-field.lua @@ -0,0 +1,106 @@ +local files = require 'files' +local vm = require 'vm' +local lang = require 'language' +local config = require 'config' +local guide = require 'parser.guide' +local define = require 'proto.define' + +return function (uri, callback) + local ast = files.getAst(uri) + if not ast then + return + end + + local function getAllDocClassFromInfer(src) + local infers = vm.getInfers(src, 0) + + if not infers then + return nil + end + + local mark = {} + local function addTo(allDocClass, src) + if not mark[src] then + allDocClass[#allDocClass+1] = src + mark[src] = true + end + end + + local allDocClass = {} + for i = 1, #infers do + local infer = infers[i] + if infer.type ~= '_G' then + local inferSource = infer.source + if inferSource.type == 'doc.class' then + addTo(allDocClass, inferSource) + elseif inferSource.type == 'doc.class.name' then + addTo(allDocClass, inferSource.parent) + elseif inferSource.type == 'doc.type.name' then + local docTypes = vm.getDocTypes(inferSource[1]) + for _, docType in ipairs(docTypes) do + if docType.type == 'doc.class.name' then + addTo(allDocClass, docType.parent) + end + end + end + end + end + + return allDocClass + end + + ---@param allDocClass table int + local function getAllFieldsFromAllDocClass(allDocClass) + local fields = {} + local empty = true + for _, docClass in ipairs(allDocClass) do + local refs = vm.getFields(docClass) + + for _, ref in ipairs(refs) do + if ref.type == 'getfield' or ref.type == 'getmethod' then + goto CONTINUE + end + local name = vm.getKeyName(ref) + if not name or vm.getKeyType(ref) ~= 'string' then + goto CONTINUE + end + fields[name] = true + empty = false + ::CONTINUE:: + end + end + + if empty then + return nil + else + return fields + end + end + + local function checkUndefinedField(src) + local fieldName = guide.getKeyName(src) + + local allDocClass = getAllDocClassFromInfer(src.node) + if (not allDocClass) or (#allDocClass == 0) then + return + end + + local fields = getAllFieldsFromAllDocClass(allDocClass) + + -- 没找到任何 field,跳过检查 + if not fields then + return + end + + if not fields[fieldName] then + local message = lang.script('DIAG_UNDEF_FIELD', fieldName) + callback { + start = src.start, + finish = src.finish, + message = message, + } + end + end + guide.eachSourceType(ast.ast, 'getfield', checkUndefinedField); + guide.eachSourceType(ast.ast, 'getmethod', checkUndefinedField); +end diff --git a/script/core/diagnostics/undefined-global.lua b/script/core/diagnostics/undefined-global.lua index 2efd75f8..5d647993 100644 --- a/script/core/diagnostics/undefined-global.lua +++ b/script/core/diagnostics/undefined-global.lua @@ -30,7 +30,7 @@ return function (uri, callback) if config.config.runtime.special[key] then return end - if #vm.getGlobalSets(guide.getKeyName(src)) == 0 then + if #vm.getGlobalSets(key) == 0 then local message = lang.script('DIAG_UNDEF_GLOBAL', key) if requireLike[key:lower()] then message = ('%s(%s)'):format(message, lang.script('DIAG_REQUIRE_LIKE', key)) diff --git a/script/parser/guide.lua b/script/parser/guide.lua index 90d09f7a..5fcfc0f3 100644 --- a/script/parser/guide.lua +++ b/script/parser/guide.lua @@ -1242,6 +1242,10 @@ function m.isDoc(source) return source.type:sub(1, 4) == 'doc.' end +function m.isDocClass(source) + return source.type == 'doc.class' +end + --- 根据函数的调用参数,获取:调用,参数索引 function m.getCallAndArgIndex(callarg) local callargs = callarg.parent diff --git a/script/proto/define.lua b/script/proto/define.lua index 082e382a..6ddcd3a7 100644 --- a/script/proto/define.lua +++ b/script/proto/define.lua @@ -140,6 +140,7 @@ m.DiagnosticDefaultSeverity = { ['unused-local'] = 'Hint', ['unused-function'] = 'Hint', ['undefined-global'] = 'Warning', + ['undefined-field'] = 'Warning', ['global-in-nil-env'] = 'Warning', ['unused-label'] = 'Hint', ['unused-vararg'] = 'Hint', diff --git a/script/vm/eachField.lua b/script/vm/eachField.lua index fabb11c7..19ac77e9 100644 --- a/script/vm/eachField.lua +++ b/script/vm/eachField.lua @@ -45,6 +45,11 @@ function vm.getFields(source, deep) or getFieldsBySource(source, deep) vm.getCache('eachFieldOfGlobal')[name] = cache return cache + elseif guide.isDocClass(source) then + local cache = vm.getCache('eachFieldOfDocClass')[source] + or getFieldsBySource(source, deep) + vm.getCache('eachFieldOfDocClass')[source] = cache + return cache else return getFieldsBySource(source, deep) end diff --git a/test/diagnostics/init.lua b/test/diagnostics/init.lua index 5ff74dc6..3f3c563c 100644 --- a/test/diagnostics/init.lua +++ b/test/diagnostics/init.lua @@ -827,3 +827,121 @@ local t TEST [[ local _ <close> = function () end ]] + +-- checkUndefinedField 通用 +TEST [[ +---@class Foo +---@field field1 integer +local mt = {} +function mt:Constructor() + self.field2 = 1 +end +function mt:method1() return 1 end +function mt.method2() return 2 end + +---@class Bar: Foo +---@field field4 integer +local mt2 = {} + +---@type Foo +local v +print(v.field1 + 1) +print(v.field2 + 1) +print(<!v.field3!> + 1) +print(v:method1()) +print(v.method2()) +print(<!v:method3!>()) + +---@type Bar +local v2 +print(v2.field1 + 1) +print(v2.field2 + 1) +print(<!v2.field3!> + 1) +print(v2.field4 + 1) +print(v2:method1()) +print(v2.method2()) +print(<!v2:method3!>()) + +local v3 = {} +print(v3.abc) + +---@class Bar2 +local mt3 +function mt3:method() return 1 end +print(mt3:method()) +]] + +-- checkUndefinedField 通过type找到class +TEST [[ +---@class Foo +local Foo +function Foo:method1() end + +---@type Foo +local v +v:method1() +<!v:method2!>() -- doc.class.name +]] + +-- checkUndefinedField 通过type找到class,涉及到 class 继承版 +TEST [[ +---@class Foo +local Foo +function Foo:method1() end +---@class Bar: Foo +local Bar +function Bar:method3() end + +---@type Bar +local v +v:method1() +<!v:method2!>() -- doc.class.name +v:method3() +]] + +-- checkUndefinedField 类名和类变量同名,类变量被直接使用 +TEST [[ +---@class Foo +local Foo +function Foo:method1() end +<!Foo:method2!>() -- doc.class +<!Foo:method2!>() -- doc.class +]] + +-- checkUndefinedField 没有@class的不检测 +TEST [[ +local Foo +function Foo:method1() + return Foo:method2() -- table +end +]] + +-- checkUndefinedField 类名和类变量不同名,类变量被直接使用、使用self +TEST [[ +---@class Foo +local mt +function mt:method1() + <!mt.method2!>() -- doc.class + self.method1() + return <!self.method2!>() -- doc.class.name +end +]] + +-- checkUndefinedField 当会推导成多个class类型时 +TEST [[ +---@class Foo +local mt +function mt:method1() end + +---@class Bar +local mt2 +function mt2:method2() end + +---@type Foo +local v +---@type Bar +local v2 +v2 = v +v2:method1() +v2:method2() -- 这个感觉实际应该报错更合适 +]] |