summaryrefslogtreecommitdiff
path: root/test/plugins
diff options
context:
space:
mode:
authorfesily <fesil@foxmail.com>2024-01-09 14:00:49 +0800
committerfesily <fesil@foxmail.com>2024-01-09 14:00:49 +0800
commit281ba5f8c4a8cb15e53acc5f3f0d4f43872ea941 (patch)
tree5f0ee208d1a1187e40db410851d3ff84abb8dc79 /test/plugins
parent7e895e9f9e9d81b6e78400976b5148a3d106dc74 (diff)
downloadlua-language-server-281ba5f8c4a8cb15e53acc5f3f0d4f43872ea941.zip
plugin: add OnTransformAst interface
Diffstat (limited to 'test/plugins')
-rw-r--r--test/plugins/ast/init.lua92
1 files changed, 92 insertions, 0 deletions
diff --git a/test/plugins/ast/init.lua b/test/plugins/ast/init.lua
new file mode 100644
index 00000000..ac2f7079
--- /dev/null
+++ b/test/plugins/ast/init.lua
@@ -0,0 +1,92 @@
+local config = require 'config'
+local utility = require 'utility'
+local parser = require 'parser'
+local luadoc = require 'parser.luadoc'
+local guide = require 'parser.guide'
+local vm = require 'vm'
+local helper = require 'plugins.astHelper'
+
+---@diagnostic disable: await-in-sync
+local function TestPlugin(script, plugin, checker)
+ config.set(TESTURI, 'Lua.workspace.preloadFileSize', 1000000000)
+ 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
+]]