Skip to content

Commit

Permalink
Support @dependency annotation in lua files (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksqsf authored Jul 17, 2024
1 parent 9458b51 commit 1fec7ee
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
17 changes: 13 additions & 4 deletions src/luaParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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
}
1 change: 1 addition & 0 deletions test/assets/lua/segmentors/segmentor.lua
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
--@dependency lua/extra.txt
dofile('external')
util = require('utils/util')
1 change: 1 addition & 0 deletions test/test-loader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 2 additions & 1 deletion test/test-parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']
]
}

Expand Down

0 comments on commit 1fec7ee

Please sign in to comment.