summaryrefslogtreecommitdiff
path: root/test/plugins/ast/test.lua
blob: f13b33a289f17135237e3652674b1ff6ec7e3168 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
local parser  = require 'parser'
local guide   = require 'parser.guide'
local helper  = require 'plugins.astHelper'

---@diagnostic disable: await-in-sync
local function TestPlugin(script, plugin, checker)
    local state = parser.compile(script, "Lua", "Lua 5.4")
    state.ast = plugin(TESTURI, state.ast) or state.ast
    parser.luadoc(state)
    checker(state)
end

local function isDocClass(ast)
    return ast.bindDocs[1].type == 'doc.class'
end

local function TestAIsClass(state, next)
    assert(isDocClass(state.ast[1]))
end

--- when call Class
local function plugin_AddClass(uri, ast)
    guide.eachSourceType(ast, "call", function (source)
        local node = source.node
        if not guide.isGet(node) then
            return
        end
        if not guide.isGlobal(node) then
            return
        end
        if guide.getKeyName(node) ~= 'Class' then
            return
        end
        local wants = {
            ['local'] = true,
            ['setglobal'] = true
        }
        local classnameNode = guide.getParentTypes(source, wants)
        if not classnameNode then
            return
        end
        local classname = guide.getKeyName(classnameNode)
        if classname then
            helper.addClassDoc(ast, classnameNode, classname)
        end
    end)
end

local function plugin_AddClassAtParam(uri, ast)
    guide.eachSourceType(ast, "function", function (src)
        helper.addClassDocAtParam(ast, "A", src, 1)
    end)
end

local function TestSelfIsClass(state, next)
    guide.eachSourceType(state.ast, "local", function (source)
        if source[1] == 'self' then
            assert(source.bindDocs)
            assert(source.parent.type == 'function')
            assert(#source.parent.args == 0)
        end
    end)
end

local function TestPlugin1(script)
    TestPlugin(script, plugin_AddClass, TestAIsClass)
end

local function TestPlugin2(script)
    TestPlugin(script, plugin_AddClassAtParam, TestSelfIsClass)
end

TestPlugin1 [[
    local A = Class(function() end)
]]

TestPlugin1 [[
    A = Class(function() end)
]]

TestPlugin2 [[
    local function ctor(self) end
]]

TestPlugin2 [[
    function ctor(self) end
]]

require 'plugins.ast.helper'