@@ -3,11 +3,14 @@ local guide = require 'parser.guide'
33local helper = require ' plugins.astHelper'
44
55--- @diagnostic disable : await-in-sync
6- local function TestPlugin (script , plugin , checker )
6+ --- @param ... function checkers
7+ local function TestPlugin (script , plugin , ...)
78 local state = parser .compile (script , " Lua" , " Lua 5.4" )
89 state .ast = plugin (TESTURI , state .ast ) or state .ast
910 parser .luadoc (state )
10- checker (state )
11+ for i = 1 , select (' #' , ... ) do
12+ select (i , ... )(state )
13+ end
1114end
1215
1316local function isDocClass (ast )
@@ -62,6 +65,47 @@ local function TestSelfIsClass(state, next)
6265 end )
6366end
6467
68+ local function TestAHasField (state , next )
69+ local foundFields = false
70+ local cb = function (source )
71+ if not source .bindDocs or not source .bindDocs [1 ].class or source .bindDocs [1 ].class [1 ] ~= " A" then
72+ return
73+ end
74+ assert (# source .bindDocs == 1 )
75+ assert (# source .bindDocs [1 ].fields >= 2 )
76+ assert (source .bindDocs [1 ].fields [1 ].field [1 ] == " x" )
77+ assert (source .bindDocs [1 ].fields [2 ].field [1 ] == " y" )
78+ foundFields = true
79+ end
80+ guide .eachSourceType (state .ast , " local" , cb )
81+ guide .eachSourceType (state .ast , " setglobal" , cb )
82+ assert (foundFields )
83+ end
84+
85+ local function plugin_AddMultipleDocs (uri , ast )
86+ guide .eachSourceType (ast , " call" , function (source )
87+ local node = source .node
88+ if guide .getKeyName (node ) ~= ' Class' then
89+ return
90+ end
91+ local wants = {
92+ [' local' ] = true ,
93+ [' setglobal' ] = true
94+ }
95+ local classnameNode = guide .getParentTypes (source , wants )
96+ if not classnameNode then
97+ return
98+ end
99+ local classname = guide .getKeyName (classnameNode )
100+ if classname then
101+ local group = {}
102+ helper .addClassDoc (ast , classnameNode , classname , group )
103+ helper .addDoc (ast , classnameNode , " field" , " x number" , group )
104+ helper .addDoc (ast , classnameNode , " field" , " y string" , group )
105+ end
106+ end )
107+ end
108+
65109local function TestPlugin1 (script )
66110 TestPlugin (script , plugin_AddClass , TestAIsClass )
67111end
@@ -70,6 +114,10 @@ local function TestPlugin2(script)
70114 TestPlugin (script , plugin_AddClassAtParam , TestSelfIsClass )
71115end
72116
117+ local function TestPlugin3 (script )
118+ TestPlugin (script , plugin_AddMultipleDocs , TestAIsClass , TestAHasField )
119+ end
120+
73121TestPlugin1 [[
74122 local A = Class(function() end)
75123]]
@@ -86,4 +134,12 @@ TestPlugin2 [[
86134 function ctor(self) end
87135]]
88136
137+ TestPlugin3 [[
138+ local A = Class()
139+ ]]
140+
141+ TestPlugin3 [[
142+ A = Class()
143+ ]]
144+
89145require ' plugins.ast.helper'
0 commit comments