local files = require 'files' local config = require 'config' local vm = require 'vm' local guide = require 'parser.guide' rawset(_G, 'TEST', true) local function getSource(pos) local ast = files.getAst('') return guide.eachSourceContain(ast.ast, pos, function (source) if source.type == 'local' or source.type == 'getlocal' or source.type == 'setlocal' or source.type == 'setglobal' or source.type == 'getglobal' or source.type == 'field' or source.type == 'method' then return source end end) end function TEST(wanted) return function (script) files.removeAll() local start = script:find('', 1, true) local pos = (start + finish) // 2 + 1 local newScript = script:gsub('<[!?]', ' '):gsub('[!?]>', ' ') files.setText('', newScript) local source = getSource(pos) assert(source) local result = vm.getInferType(source, 0) assert(wanted == result) end end TEST 'string' [[ local = '111' ]] TEST 'boolean' [[ local = true ]] TEST 'integer' [[ local = 1 ]] TEST 'number' [[ local = 1.0 ]] TEST 'string' [[ local var = '111' t. = var ]] TEST 'string' [[ local var = '111' ]] TEST 'string' [[ local var var = '111' print() ]] TEST 'function' [[ function () end ]] TEST 'function' [[ local function () end ]] TEST 'function' [[ local xx = function () end ]] TEST 'table' [[ local = {} ]] TEST 'function' [[ () ]] TEST 'table' [[ .x = 1 ]] TEST 'boolean' [[ = not y ]] TEST 'integer' [[ = #y ]] TEST 'number' [[ = - y ]] TEST 'integer' [[ = ~ y ]] TEST 'integer' [[ local a = true local b = 1 = a and b ]] TEST 'integer' [[ local a = false local b = 1 = a or b ]] TEST 'boolean' [[ = a == b ]] TEST 'integer' [[ = a << b ]] TEST 'string' [[ = a .. b ]] TEST 'number' [[ = a + b ]] TEST 'table*' [[ () ]] TEST 'string' [[ = _VERSION ]] TEST 'function' [[ return ('x'). ]] TEST 'function' [[ = ('x').sub ]] TEST 'function' [[ = _VERSION.sub ]] TEST 'table' [[ = setmetatable({}) ]] TEST 'integer' [[ local function x() return 1 end = x() ]] TEST 'integer|nil' [[ local function x() return 1 return nil end = x() ]] TEST 'any' [[ local function x() return a return nil end = x() ]] TEST 'integer' [[ local function x() return 1 end _, = pcall(x) ]] TEST 'integer' [[ function x() return 1 end _, = pcall(x) ]] TEST 'os*' [[ local = require 'os' ]] TEST 'string|table' [[ local y = # ]] TEST 'integer' [[ local y = << 0 ]] TEST 'integer' [[ local function f(, b) return a << b end ]] TEST 'string' [[ local function f() return string.sub() end local = f() ]] -- 不根据调用者的输入参数来推测 --TEST 'number' [[ --local function x(a) -- return --end --x(1) --]] --TEST 'table' [[ --setmetatable() --]] TEST 'function' [[ string.() ]] TEST 'function' [[ (''):() ]] -- 不根据对方函数内的使用情况来推测 TEST 'any' [[ local function x(a) _ = a + 1 end local b x() ]] TEST 'any' [[ local function x(a, ...) local _, , _ = ... end x(nil, 'xx', 1, true) ]] -- 引用不跨越参数 TEST 'any' [[ local function x(a, ...) return true, 'ss', ... end local _, _, _, , _ = x(nil, true, 1, 'yy') ]] -- TODO 暂不支持这些特殊情况,之后用其他语法定义 --TEST 'integer' [[ --for in ipairs(t) do --end --]] TEST 'any' [[ local = next() ]] TEST 'any' [[ local a, b function a() return b() end function b() return a() end local = a() ]] TEST 'class' [[ ---@class class local ]] TEST 'string' [[ ---@type string local ]] TEST 'string[]' [[ ---@type string[] local ]] TEST 'string|table' [[ ---@type string | table local ]] TEST '"enum1"|"enum2"' [[ ---@type '"enum1"' | '"enum2"' local ]] TEST 'function' [[ ---@type fun() local ]] TEST 'table' [[ ---@type table local ]] TEST 'table' [[ self.[#self.t+1] = {} ]] TEST 'string' [[ ---@type string[] local x local = x[1] ]] TEST 'string' [[ ---@return string[] local function f() end local x = f() local = x[1] ]] TEST 'table' [[ local print(t.sub()) ]] TEST 'string|table' [[ local print(t:sub()) ]] TEST 'string' [[ local print(t:sub()) print(t .. 'a') ]] TEST 'string' [[ local print(#t) print(t .. 'a') ]]