diff --git a/src/luaParser.ts b/src/luaParser.ts index 8efd294..f60b9b0 100644 --- a/src/luaParser.ts +++ b/src/luaParser.ts @@ -2,8 +2,8 @@ import { parse, Node } from 'luaparse' import { expandLua } from './util' export function parseLua (content: string): string[][] { - const result: string[] = [] - parse(content, { + const luaFiles: string[] = [] + const ast = parse(content, { luaVersion: '5.3', onCreateNode (node: Node) { if (node.type === 'CallExpression') { @@ -14,11 +14,20 @@ export function parseLua (content: string): string[][] { const arg = args[0] if (arg.type === 'StringLiteral' && arg.raw.match(/'[_a-zA-Z0-9./]+'|"[_a-zA-Z0-9./]+"/)) { const module = arg.raw.slice(1, -1) - result.push(`lua/${module.replaceAll('.', '/')}.lua`) + luaFiles.push(`lua/${module.replaceAll('.', '/')}.lua`) } } } } }) - return result.map(expandLua) + const result: string[][] = luaFiles.map(expandLua) + for (const comment of ast.comments || []) { + const m = (comment as any).raw.match(/@dependency +(.*)/) + if (!m) { + continue + } + const filename = m[1] + result.push([filename]) + } + return result } diff --git a/test/assets/lua/segmentors/segmentor.lua b/test/assets/lua/segmentors/segmentor.lua index e02af08..4c8b2a1 100644 --- a/test/assets/lua/segmentors/segmentor.lua +++ b/test/assets/lua/segmentors/segmentor.lua @@ -1,2 +1,3 @@ +--@dependency lua/extra.txt dofile('external') util = require('utils/util') diff --git a/test/test-loader.spec.ts b/test/test-loader.spec.ts index c9a9316..9696109 100644 --- a/test/test-loader.spec.ts +++ b/test/test-loader.spec.ts @@ -142,6 +142,7 @@ it('Load recipe', async () => { 'child.dict.yaml', 'lua/external.lua', 'lua/external/init.lua', + 'lua/extra.txt', 'lua/processor.lua', 'lua/processor/init.lua', 'lua/segmentors/segmentor.lua', diff --git a/test/test-parser.spec.ts b/test/test-parser.spec.ts index 91e4867..30b5bae 100644 --- a/test/test-parser.spec.ts +++ b/test/test-parser.spec.ts @@ -77,7 +77,8 @@ const luaCases = { ], 'lua/segmentors/segmentor.lua': [ ['lua/external.lua', 'lua/external/init.lua'], - ['lua/utils/util.lua', 'lua/utils/util/init.lua'] + ['lua/utils/util.lua', 'lua/utils/util/init.lua'], + ['lua/extra.txt'] ] }