local core = require 'core.diagnostics' local files = require 'files' local config = require 'config' local util = require 'utility' local catch = require 'catch' config.get 'Lua.diagnostics.neededFileStatus'['deprecated'] = 'Any' rawset(_G, 'TEST', true) local function founded(targets, results) if #targets ~= #results then return false end for _, target in ipairs(targets) do for _, result in ipairs(results) do if target[1] == result[1] and target[2] == result[2] then goto NEXT end end do return false end ::NEXT:: end return true end function TEST(script, ...) files.removeAll() local newScript, catched = catch(script, '!') files.setText('', newScript) files.open('') local datas = {} core('', function (results) for _, res in ipairs(results) do datas[#datas+1] = res end end) local results = {} for i, data in ipairs(datas) do results[i] = { data.start, data.finish } end if results[1] then if not founded(catched['!'] or {}, results) then error(('%s\n%s'):format(util.dump(catched['!']), util.dump(results))) end else assert(catched['!'] == nil) end end TEST [[ local ]] TEST [[ local y local x = y ]] TEST [[ local function x() end x() ]] TEST [[ return function (x) x.a = 1 end ]] TEST [[ local = {} .a = 1 ]] TEST [[ local () end!> ]] TEST [[ local = ]] TEST [[ local = ]] TEST [[ local local () x() end!> ]] TEST [[ local print, _G print() print() print() print() print() print(Z) print(_G) Z = 1 ]] TEST [[ :::: ]] TEST [[ ]] TEST [[ ]] TEST [[ X = 1 ]] TEST [[ X = [=[ ]=] ]] TEST [[ local x print(x) local print(x) ]] TEST [[ local x print(x) local print(x) local print(x) ]] TEST [[ local _ print(_) local _ print(_) local _ENV (_ENV) -- 由于重定义了_ENV,因此print变为了未定义全局变量 ]] TEST [[ local x return x, function () return x end ]] TEST [[ print(1) _ENV = nil ]] TEST [[ local _ENV = { print = print } print(1) ]] config.get 'Lua.diagnostics.disable'['undefined-env-child'] = true TEST [[ _ENV = nil = 1 --> _ENV.GLOBAL = 1 ]] TEST [[ _ENV = nil local _ = --> local _ = _ENV.print ]] TEST [[ _ENV = {} GLOBAL = 1 --> _ENV.GLOBAL = 1 ]] TEST [[ _ENV = {} local _ = print --> local _ = _ENV.print ]] TEST [[ GLOBAL = 1 _ENV = nil ]] config.get 'Lua.diagnostics.disable'['undefined-env-child'] = nil TEST [[ :sub(1, 1) ]] TEST [[ print() ('string') ]] TEST [[ pairs {} {} ]] TEST [[ local x return x : f(1) : f(1) ]] TEST [[ return { } ]] TEST [[ return { } ]] TEST [[ print() 'string' ]] TEST [[ print { x = 1, } ]] TEST [[ local function x(a, b) return a, b end x(1, 2, ) ]] TEST [[ local function x(a, b, ...) return a, b, ... end x(1, 2, 3, 4, 5) ]] TEST [[ local m = {} function m:x(a, b) return a, b end m:x(1, 2, ) ]] TEST [[ local m = {} function m:x(a, b) return a, b end m.x(1, 2, 3, ) ]] TEST [[ local m = {} function m.x(a, b) return a, b end m:x(1, , , ) ]] TEST [[ local m = {} function m.x() end m:x() ]] TEST [[ InstanceName = 1 Instance = _G[InstanceName] ]] TEST [[ (''):sub(1, 2) ]] TEST [=[ return [[ ]] ]=] config.get 'Lua.diagnostics.disable'['close-non-object'] = true TEST [[ local _ = function () end ]] config.get 'Lua.diagnostics.disable'['close-non-object'] = nil TEST [[ local _ = ]] config.get 'Lua.diagnostics.disable'['unused-local'] = true TEST [[ local f = ]] TEST [[ local f;f = ]] TEST [[ local ]] config.get 'Lua.diagnostics.disable'['unused-local'] = nil TEST [[ local mt, x function mt:m() function x:m() end end return mt, x ]] TEST [[ local mt = {} function mt:f() end return mt ]] TEST [[ local = {} function :f() end ]] TEST [[ local = {} .a = 1 ]] TEST [[ local = {} ['a'] = 1 ]] TEST [[ local function f() end f() ]] TEST [[ local function f() end f() ]] TEST [[ local function f(var) print(var) end local var f(var) ]] TEST [[ local function f(a, b) return a, b end f(1, 2, , ) ]] TEST [[ local mt = {} function mt:f(a, b) return a, b end mt.f(1, 2, 3, ) ]] TEST [[ local mt = {} function mt.f(a, b) return a, b end mt:f(1, , , ) ]] TEST [[ local mt = {} function mt:f(a, b) return a, b end mt:f(1, 2, , ) ]] TEST [[ local function f(a, b, ...) return a, b, ... end f(1, 2, 3, 4) ]] TEST [[ next({}, 1, ) print(1, 2, 3, 4, 5) ]] TEST [[ local function f(callback) callback(1, 2, 3) end f(function () end) ]] --TEST [[ --local realTostring = tostring --tostring = function () end --tostring() --tostring = realTostring --tostring(1) --]] TEST [[ = 1 tostring = 1 ROOT = 1 _G.bb = 1 ]] TEST [[ local f = load('') f(1, 2, 3) ]] TEST [[ (1) ]] TEST [[ X = table[] ]] TEST [[ return { = 1, y = 2, = 3, } ]] TEST [[ return { x = 1, y = 2, }, { x = 1, y = 2, } ]] TEST [[ local m = {} function m.open() end m:open() ]] TEST [[ local m = {} function m:open() end m.open('ok') ]] TEST [[ ]] TEST [[ ]] TEST [[ if true then else return end ]] TEST [[ while true do end ]] TEST [[ ]] TEST [[ ]] TEST [[ local _ = 1, ]] TEST [[ _ = 1, ]] TEST [[ local function x() do local k print(k) x() end local k = 1 print(k) end ]] TEST [[ local function x() local loc x() print(loc) end ]] TEST [[ local = {} [1] = 1 ]] TEST [[ T1 = 1 _ENV.T2 = 1 _G.T3 = 1 _ENV._G.T4 = 1 _G._G._G.T5 = 1 rawset(_G, 'T6', 1) rawset(_ENV, 'T7', 1) print(T1) print(T2) print(T3) print(T4) print(T5) print(T6) print(T7) ]] TEST [[ local x x = ]] TEST [[ local x, y x = ]] TEST [[ local x, y, z x = x and y or '' .. z ]] TEST [[ local x x = x or -1 ]] TEST [[ local x x = x or (0 + 1) ]] TEST [[ local x, y x = (x + y) or 0 ]] TEST [[ local t = {} t.a = 1 t.a = 2 return t ]] TEST [[ table.insert({}, 1, 2, ) ]] TEST [[ while true do break end ]] TEST [[ local x, , = 1 print(x, y, z) ]] TEST [[ local x, y, = 1, 2 print(x, y, z) ]] TEST [[ local x, y, z = print() print(x, y, z) ]] TEST [[ local x, y, z print(x, y, z) ]] TEST [[ local x, y, z x, , = 1 print(x, y, z) ]] TEST [[ X, , = 1 ]] TEST [[ T = {} T.x, , = 1 ]] TEST [[ T = {} T['x'], , = 1 ]] TEST [[ ---@class ---@class ]] TEST [[ ---@class A : ]] TEST [[ ---@class ---@class ---@class ---@class ]] TEST [[ ---@class A : B ---@class B : C ---@class C : D ---@class D ]] TEST [[ ---@type ]] TEST [[ ---@class A ---@type A|| ]] TEST [[ ---@class AAA ---@alias B AAA ---@type B ]] TEST [[ ---@alias B ]] TEST [[ ---@class ---@class B ---@alias ]] TEST [[ ---@param x ]] TEST [[ ---@class Class ---@param Class local function f(x) return x end f() ]] TEST [[ ---@class Class ---@param Class function F(x) return x end F() ]] TEST [[ ---@class Class ---@param Class ---@param y Class ---@param Class local function f(x, y) return x, y end f() ]] TEST [[ ---@field ---@class Class ]] TEST [[ ---@class Class ---@field ]] TEST [[ ---@class Class --- ---@field x Class ]] TEST [[ ---@class Class ---@field x Class ---@field Class ]] TEST [[ ---@class Class : any ]] TEST [[ ---@type fun(a: integer) local f f() ]] TEST [[ ---@class c c = {} ]] TEST [[ ---@generic T: any ---@param v T ---@param message any ---@return T function assert(v, message) return v, message end ]] TEST [[ ---@type string ---| ]] TEST [[ ---@type ---| 'xx' ]] TEST [[ ---@class class local t ]] ---[==[ -- 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. + 1) print(v:method1()) print(v.method2()) print(v:()) ---@type Bar local v2 print(v2.field1 + 1) print(v2.field2 + 1) print(v2. + 1) print(v2.field4 + 1) print(v2:method1()) print(v2.method2()) print(v2:()) 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:() -- 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:() -- doc.class.name v:method3() ]] -- checkUndefinedField 类名和类变量同名,类变量被直接使用 TEST [[ ---@class Foo local Foo function Foo:method1() end Foo:() -- doc.class Foo:() -- 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.() -- doc.class self.method1() return self.() -- 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 -- TODO 这里应该给警告 v2:() v2:method2() ]] TEST [[ ---@type table T1 = {} print(T1.f1) ---@type tablelib T2 = {} print(T2.) ]] --]==] TEST [[ ---@overload fun(...) local function f() end f(1) ]] TEST [[ for i = do print(i) end ]] TEST [[ for i = do print(i) end ]] TEST [[ for i = 1, 1 do print(i) end ]] TEST [[ ---@param a number return function (a) end ]] TEST [[ local m = {} function () end function () end return m ]] TEST [[ local m = {} function m:fff() end do function m:fff() end end return m ]] TEST [[ local m = {} m.x = true m.x = false return m ]] TEST [[ local m = {} m.x = io.open() m.x = nil return m ]] TEST [[ ---@class A ---@field a boolean ---@return A local function f() end local r = f() r.x = 1 return r.x ]] TEST [[ ---@diagnostic disable-next-line x = 1 ]] TEST [[ ---@diagnostic disable-next-line: lowercase-global x = 1 ]] TEST [[ ---@diagnostic disable-next-line: unused-local = 1 ]] TEST [[ ---@diagnostic disable x = 1 ]] TEST [[ ---@diagnostic disable ---@diagnostic enable = 1 ]] TEST [[ ---@diagnostic disable ---@diagnostic disable ---@diagnostic enable x = 1 ]] TEST [[ ---@diagnostic disable-next-line: ]] TEST [[ local mt = {} function mt:a(x) return self, x end function mt:b(y) self:a(1):b(2) return y end return mt ]] TEST [[ local function each() return function () end end for x in each() do print(x) end ]] TEST [[ ---@type string local s print(s:upper()) ]] TEST [[ local t = (). return t ]] TEST [[ return { [1] = 1, ['1'] = 1, } ]] TEST [[ return { [print()] = 1, [print()] = 1, } ]] TEST [[ ---@type { x: number, y: number} ---| "'resume'" ]] TEST [[ return { 1, , 3, [] = 4, } ]]