From ff741df641bbe99a418bb856e14e3ec30bbc08aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Fri, 13 Jan 2023 16:59:39 +0800 Subject: fix --- script/vm/compiler.lua | 8 ++-- test/crossfile/infer.lua | 112 +++++++++++++++++++++++++++++++++++++++++++++++ test/crossfile/init.lua | 1 + 3 files changed, 117 insertions(+), 4 deletions(-) create mode 100644 test/crossfile/infer.lua diff --git a/script/vm/compiler.lua b/script/vm/compiler.lua index a2c5c228..5a078e70 100644 --- a/script/vm/compiler.lua +++ b/script/vm/compiler.lua @@ -1236,10 +1236,10 @@ local compilerSwitch = util.switch() : case 'getmethod' : case 'getindex' : call(function (source) - if vm.bindDocs(source) then + if guide.isGet(source) and vm.bindAs(source) then return end - if guide.isGet(source) and vm.bindAs(source) then + if vm.bindDocs(source) then return end ---@type (string|vm.node)? @@ -1270,8 +1270,8 @@ local compilerSwitch = util.switch() ---@cast key string vm.compileByParentNode(source.node, key, function (src) vm.setNode(source, vm.compileNode(src)) - if src.value then - vm.setNode(source, vm.compileNode(src.value)) + if src == source and source.value then + vm.setNode(source, vm.compileNode(source.value)) end end) end diff --git a/test/crossfile/infer.lua b/test/crossfile/infer.lua new file mode 100644 index 00000000..2f2c35ad --- /dev/null +++ b/test/crossfile/infer.lua @@ -0,0 +1,112 @@ +local files = require 'files' +local furi = require 'file-uri' +local vm = require 'vm' +local guide = require 'parser.guide' +local catch = require 'catch' + +rawset(_G, 'TEST', true) + +local function getSource(uri, pos) + local state = files.getState(uri) + if not state then + return + end + local result + guide.eachSourceContain(state.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' + or source.type == 'function' + or source.type == 'table' + or source.type == 'doc.type.name' then + result = source + end + end) + return result +end + +local EXISTS = {} + +local function eq(a, b) + if a == EXISTS and b ~= nil then + return true + end + if b == EXISTS and a ~= nil then + return true + end + local tp1, tp2 = type(a), type(b) + if tp1 ~= tp2 then + return false + end + if tp1 == 'table' then + local mark = {} + for k in pairs(a) do + if not eq(a[k], b[k]) then + return false + end + mark[k] = true + end + for k in pairs(b) do + if not mark[k] then + return false + end + end + return true + end + return a == b +end + +---@diagnostic disable: await-in-sync +function TEST(expect) + local sourcePos, sourceUri + for _, file in ipairs(expect) do + local script, list = catch(file.content, '?') + local uri = furi.encode(file.path) + files.setText(uri, script) + files.compileState(uri) + if #list['?'] > 0 then + sourceUri = uri + sourcePos = (list['?'][1][1] + list['?'][1][2]) // 2 + end + end + + local _ = function () + for _, info in ipairs(expect) do + files.remove(furi.encode(info.path)) + end + end + + local source = getSource(sourceUri, sourcePos) + assert(source) + local view = vm.getInfer(source):view(sourceUri) + assert(eq(view, expect.infer)) +end + +TEST { + { + path = 'a.lua', + content = [[ +---@class T +local x + +---@class V +x.y = 1 +]], + }, + { + path = 'b.lua', + content = [[ +---@type T +local x + +if x.y then + print(x.) +end + ]], + }, + infer = 'V', +} diff --git a/test/crossfile/init.lua b/test/crossfile/init.lua index aec9a044..d4cef46a 100644 --- a/test/crossfile/init.lua +++ b/test/crossfile/init.lua @@ -1,4 +1,5 @@ require 'crossfile.definition' +require 'crossfile.infer' require 'crossfile.references' require 'crossfile.hover' require 'crossfile.completion' -- cgit v1.2.3