-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathlua.ts
More file actions
336 lines (311 loc) · 11.1 KB
/
Copy pathlua.ts
File metadata and controls
336 lines (311 loc) · 11.1 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import type {
Call,
ExtractorOutput,
SubDeclaration,
TreeSitterNode,
TreeSitterTree,
} from '../types.js';
import { findChild, nodeEndLine, nodeStartLine } from './helpers.js';
/**
* Lua base-library global function names and standard-library module
* tables. A plain `identifier = identifier` assignment whose LHS is one of
* these escapes local/lexical scoping entirely: the LHS names a language
* builtin rather than a locally declared variable that scope-based tracking
* could follow, so a function assigned here (`require = tracedRequire`, the
* monkey-patch pattern from issue #1776) becomes reachable through every
* later unqualified use of the builtin name — anywhere in the codebase,
* since it's a genuine global, not just within this file's call graph.
* Mirrors `LUA_BUILTIN_GLOBALS` in `crates/codegraph-core/src/extractors/lua.rs`.
*/
export const LUA_BUILTIN_GLOBALS: Set<string> = new Set([
'assert',
'collectgarbage',
'dofile',
'error',
'getfenv',
'getmetatable',
'ipairs',
'load',
'loadfile',
'loadstring',
'module',
'next',
'pairs',
'pcall',
'print',
'rawequal',
'rawget',
'rawlen',
'rawset',
'require',
'select',
'setfenv',
'setmetatable',
'tonumber',
'tostring',
'type',
'unpack',
'xpcall',
// Standard-library module tables — wholesale replacement (e.g. sandboxing)
// is the same "escapes local scope" shape as a single builtin function.
'string',
'table',
'math',
'io',
'os',
'coroutine',
'debug',
'utf8',
'bit32',
]);
/**
* Extract symbols from Lua files.
*/
export function extractLuaSymbols(tree: TreeSitterTree, _filePath: string): ExtractorOutput {
const ctx: ExtractorOutput = {
definitions: [],
calls: [],
imports: [],
classes: [],
exports: [],
typeMap: new Map(),
};
walkLuaNode(tree.rootNode, ctx);
return ctx;
}
function walkLuaNode(node: TreeSitterNode, ctx: ExtractorOutput): void {
switch (node.type) {
case 'function_declaration':
handleLuaFunctionDecl(node, ctx);
break;
case 'variable_declaration':
handleLuaVariableDecl(node, ctx);
break;
case 'function_call':
handleLuaFunctionCall(node, ctx);
break;
case 'assignment_statement':
handleLuaAssignmentStatement(node, ctx);
break;
}
for (let i = 0; i < node.childCount; i++) {
const child = node.child(i);
if (child) walkLuaNode(child, ctx);
}
}
function handleLuaFunctionDecl(node: TreeSitterNode, ctx: ExtractorOutput): void {
const nameNode = node.childForFieldName('name');
if (!nameNode) return;
let name: string;
let kind: 'function' | 'method' = 'function';
if (nameNode.type === 'method_index_expression') {
const table = nameNode.childForFieldName('table');
const method = nameNode.childForFieldName('method');
if (table && method) {
name = `${table.text}.${method.text}`;
kind = 'method';
} else {
name = nameNode.text;
}
} else if (nameNode.type === 'dot_index_expression') {
const table = nameNode.childForFieldName('table');
const field = nameNode.childForFieldName('field');
if (table && field) {
name = `${table.text}.${field.text}`;
kind = 'method';
} else {
name = nameNode.text;
}
} else {
name = nameNode.text;
}
const params = extractLuaParams(node);
ctx.definitions.push({
name,
kind,
line: node.startPosition.row + 1,
endLine: nodeEndLine(node),
children: params.length > 0 ? params : undefined,
});
}
function extractLuaParams(funcNode: TreeSitterNode): SubDeclaration[] {
const params: SubDeclaration[] = [];
const paramList = funcNode.childForFieldName('parameters');
if (!paramList) return params;
for (let i = 0; i < paramList.childCount; i++) {
const param = paramList.child(i);
if (param?.type !== 'identifier') continue;
params.push({ name: param.text, kind: 'parameter', line: param.startPosition.row + 1 });
}
return params;
}
function handleLuaVariableDecl(node: TreeSitterNode, ctx: ExtractorOutput): void {
// Check for require calls in the assignment
const assignment = findChild(node, 'assignment_statement');
if (assignment) {
checkForRequire(assignment, ctx);
}
}
function checkForRequire(node: TreeSitterNode, ctx: ExtractorOutput): void {
for (let i = 0; i < node.childCount; i++) {
const child = node.child(i);
if (!child) continue;
if (child.type === 'function_call') {
const nameNode = child.childForFieldName('name');
if (nameNode && nameNode.type === 'identifier' && nameNode.text === 'require') {
const args = child.childForFieldName('arguments');
if (args) {
const strArg = findChild(args, 'string');
if (strArg) {
const source = strArg.text.replace(/^['"]|['"]$/g, '');
ctx.imports.push({
source,
names: ['require'],
line: child.startPosition.row + 1,
});
}
}
}
}
}
}
/**
* Detect `<builtin> = <identifier>` assignments — a locally declared
* function bound to a Lua global/builtin identifier (e.g.
* `require = tracedRequire`), the monkey-patch pattern from issue #1776.
* Every later unqualified call to the builtin name (`require(...)`)
* anywhere in the codebase actually invokes the RHS function, but that call
* site names the builtin, never the RHS function directly — so without
* this, the RHS function has no inbound edge at all and is misclassified
* dead-unresolved.
*
* Emits a dynamic `value-ref` call for the RHS identifier — the same
* classification #1771 uses for bare identifiers referenced as
* object-literal property values: a bare identifier used in a value
* position, not a call site. Resolution downstream (build-edges.ts /
* incremental.ts / build_edges.rs) already restricts `value-ref` calls to
* function/method-kind targets only, so a builtin reassigned to a
* non-function value (`unpack = someTable`) is silently dropped rather than
* fabricating a nonsensical edge — no further changes needed there.
*
* Scoped narrowly to plain `identifier = identifier` pairs where the LHS
* matches a known Lua builtin/stdlib-module name (`LUA_BUILTIN_GLOBALS`).
* General local-to-local variable aliasing (`local a = someFunc; a()`) is a
* much larger points-to/alias-tracking problem this fix does not attempt to
* solve — see #1776 for the scoping rationale.
*
* Handles both the bare top-level form (`require = tracedRequire`, a
* standalone `assignment_statement`) and the `local`-declared form
* (`local require = tracedRequire`, an `assignment_statement` nested inside
* `variable_declaration`) identically: shadowing a builtin name with
* `local` is the same "redirect every later unqualified use" idiom, just
* lexically scoped rather than truly global.
*
* Multi-assignment (`a, b = f, g`) is handled positionally, matching Lua's
* own assignment semantics — mixed variable kinds (`t.b, a = f, g`) do not
* shift the pairing, since each side is indexed independently by position
* rather than pre-filtered to identifiers first.
*/
function handleLuaAssignmentStatement(node: TreeSitterNode, ctx: ExtractorOutput): void {
const variableList = findChild(node, 'variable_list');
const expressionList = findChild(node, 'expression_list');
if (!variableList || !expressionList) return;
const variables = variableList.namedChildren;
const expressions = expressionList.namedChildren;
const pairCount = Math.min(variables.length, expressions.length);
for (let i = 0; i < pairCount; i++) {
const lhs = variables[i];
const rhs = expressions[i];
if (!lhs || !rhs) continue;
if (lhs.type !== 'identifier' || rhs.type !== 'identifier') continue;
if (!LUA_BUILTIN_GLOBALS.has(lhs.text) || LUA_BUILTIN_GLOBALS.has(rhs.text)) continue;
ctx.calls.push({
name: rhs.text,
line: nodeStartLine(rhs),
dynamic: true,
dynamicKind: 'value-ref',
});
}
}
/**
* Lua string node types across grammar variants — `string` is the only kind
* the current `@tree-sitter-grammars/tree-sitter-lua` grammar (npm, used by
* this WASM extractor) produces; `string_literal` is included so this stays
* in lockstep with the native Rust extractor's own (equally defensive) check.
*/
const LUA_STRING_NODE_TYPES = new Set(['string', 'string_literal']);
function handleLuaFunctionCall(node: TreeSitterNode, ctx: ExtractorOutput): void {
const nameNode = node.childForFieldName('name');
if (!nameNode) return;
// load(chunk) / loadstring(chunk) / loadfile(...) / dofile(...) — dynamic
// code execution; undecidable statically. Mirrors handle_lua_function_call's
// `load` arm in crates/codegraph-core/src/extractors/lua.rs.
if (nameNode.type === 'identifier') {
const ident = nameNode.text;
if (ident === 'load' || ident === 'loadstring' || ident === 'loadfile' || ident === 'dofile') {
ctx.calls.push({
name: '<dynamic:eval>',
line: node.startPosition.row + 1,
dynamic: true,
dynamicKind: 'eval',
});
return;
}
}
// Check for require() as import
if (nameNode.type === 'identifier' && nameNode.text === 'require') {
const args = node.childForFieldName('arguments');
if (args) {
const strArg = findChild(args, 'string');
if (strArg) {
const source = strArg.text.replace(/^['"]|['"]$/g, '');
ctx.imports.push({
source,
names: ['require'],
line: node.startPosition.row + 1,
});
return;
}
}
}
const call: Call = { name: '', line: node.startPosition.row + 1 };
if (nameNode.type === 'method_index_expression') {
const table = nameNode.childForFieldName('table');
const method = nameNode.childForFieldName('method');
if (method) call.name = method.text;
if (table) call.receiver = table.text;
} else if (nameNode.type === 'dot_index_expression') {
const table = nameNode.childForFieldName('table');
const field = nameNode.childForFieldName('field');
if (field) call.name = field.text;
if (table) call.receiver = table.text;
} else if (nameNode.type === 'bracket_index_expression') {
// t[k]() — bracket-index call; key may be a string literal (resolvable
// directly, same as a `.field`/`:method` call) or a variable/expression
// (undecidable statically — flagged `computed-key`). Mirrors
// handle_lua_function_call's `bracket_index_expression` arm in
// crates/codegraph-core/src/extractors/lua.rs.
const table = nameNode.childForFieldName('table');
const key = nameNode.childForFieldName('field');
if (key) {
if (LUA_STRING_NODE_TYPES.has(key.type)) {
call.name = key.text.replace(/^['"]|['"]$/g, '');
if (table) call.receiver = table.text;
} else {
const dynamicCall: Call = {
name: '<dynamic:computed-key>',
line: node.startPosition.row + 1,
dynamic: true,
dynamicKind: 'computed-key',
keyExpr: key.text,
};
if (table) dynamicCall.receiver = table.text;
ctx.calls.push(dynamicCall);
return;
}
}
} else {
call.name = nameNode.text;
}
if (call.name) ctx.calls.push(call);
}