summaryrefslogtreecommitdiff
path: root/test/plugins/node/test.lua
blob: 15e4d16c31955f1318fecb556d5d5c5e238f4eee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
local files      = require 'files'
local scope      = require 'workspace.scope'
local nodeHelper = require 'plugins.nodeHelper'
local vm         = require 'vm'
local guide      = require 'parser.guide'


local pattern, msg = nodeHelper.createFieldPattern("*.components")
assert(pattern, msg)

---@param source parser.object
function OnCompileFunctionParam(next, func, source)
    if next(func, source) then
        return true
    end
    --从该参数的使用模式来推导该类型
    if nodeHelper.matchPattern(source, pattern) then
        local type = vm.declareGlobal('type', 'TestClass', TESTURI)
        vm.setNode(source, vm.createNode(type, source))
        return true
    end
end

local myplugin = { OnCompileFunctionParam = OnCompileFunctionParam }

---@diagnostic disable: await-in-sync
local function TestPlugin(script)
    local prefix = [[
        ---@class TestClass
        ---@field b string
    ]]
    ---@param checker fun(state:parser.state)
    return function (plugin, checker)
        files.open(TESTURI)
        files.setText(TESTURI, prefix .. script, true)
        scope.getScope(TESTURI):set('pluginInterfaces', plugin)
        local state = files.getState(TESTURI)
        assert(state)
        checker(state)
        files.remove(TESTURI)
    end
end

TestPlugin [[
    local function t(a)
        a.components:test()
    end
]](myplugin, function (state)
    guide.eachSourceType(state.ast, 'local', function (src)
        if guide.getKeyName(src) == 'a' then
            local node = vm.compileNode(src)
            assert(node)
            assert(not vm.isUnknown(node))
        end
    end)
end)