Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions crates/codegraph-core/src/extractors/lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,23 @@ fn handle_lua_function_call(node: &Node, source: &[u8], symbols: &mut FileSymbol
}
if let Some(k) = key {
if k.kind() == "string" || k.kind() == "string_literal" {
// t["handler"]() — literal-key bracket index; statically
// resolvable to the same target as t.handler(), so it still
// resolves to a normal edge. Tagged `computed-literal` (ADR-002
// Track A) to match JS's extractSubscriptCallInfo convention for
// obj["method"]() and make the call visible to dynamic_kind-based
// queries — see issue #2042. Lua table indexing has no structural
// difference from JS member access here: both `t.handler()` and
// `t["handler"]()` are equally subject to metatable `__index`
// redirection, so there's no Lua-specific reason the literal-key
// form should be treated as more "static" than JS's equivalent.
let raw = node_text(&k, source);
let call_name = raw.trim_matches(|c| c == '\'' || c == '"').to_string();
symbols.calls.push(Call {
name: call_name,
line: start_line(node),
dynamic: Some(true),
dynamic_kind: Some("computed-literal".to_string()),
receiver: table.map(|t| node_text(&t, source).to_string()),
..Default::default()
});
Expand Down Expand Up @@ -464,20 +476,23 @@ mod tests {

#[test]
fn resolves_bracket_index_call_with_string_literal_key_directly() {
// Resolves to the same target as `t.handler()`, but tagged Track A
// `computed-literal` (#2042) to match JS's `obj["method"]()` convention
// instead of looking identical to a static `t.handler()` call.
let s = parse_lua("t[\"handler\"]()");
assert!(s
.calls
.iter()
.any(|c| c.name == "handler" && c.receiver.as_deref() == Some("t") && c.dynamic.is_none()));
assert!(s.calls.iter().any(|c| c.name == "handler"
&& c.receiver.as_deref() == Some("t")
&& c.dynamic == Some(true)
&& c.dynamic_kind.as_deref() == Some("computed-literal")));
}

#[test]
fn resolves_bracket_index_call_with_single_quoted_string_literal_key_directly() {
let s = parse_lua("t['handler']()");
assert!(s
.calls
.iter()
.any(|c| c.name == "handler" && c.receiver.as_deref() == Some("t")));
assert!(s.calls.iter().any(|c| c.name == "handler"
&& c.receiver.as_deref() == Some("t")
&& c.dynamic == Some(true)
&& c.dynamic_kind.as_deref() == Some("computed-literal")));
}

#[test]
Expand Down
11 changes: 11 additions & 0 deletions src/extractors/lua.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,22 @@ function handleLuaFunctionCall(node: TreeSitterNode, ctx: ExtractorOutput): void
// (undecidable statically — flagged `computed-key`). Mirrors
// handle_lua_function_call's `bracket_index_expression` arm in
// crates/codegraph-core/src/extractors/lua.rs.
//
// The literal-key case (`t["handler"]()`) is tagged `computed-literal`
// (ADR-002 Track A — resolvable) to match JS's extractSubscriptCallInfo
// convention for `obj["method"]()`, making it visible to dynamic_kind-
// based queries instead of looking identical to a static `t.handler()`
// call (issue #2042). Lua table indexing is not structurally different
// from JS member access here — both `t.handler()` and `t["handler"]()`
// are equally subject to metatable `__index` redirection — so there is
// no Lua-specific reason to diverge from JS's convention.
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, '');
call.dynamic = true;
call.dynamicKind = 'computed-literal';
if (table) call.receiver = table.text;
} else {
const dynamicCall: Call = {
Expand Down
21 changes: 17 additions & 4 deletions tests/parsers/lua.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,18 +216,31 @@ string.format("%s", name)`);
);
});

it('resolves a bracket-index call with a string-literal key directly', () => {
it('resolves a bracket-index call with a string-literal key directly, tagged computed-literal', () => {
// Resolves to the same target as `t.handler()`, but tagged Track A
// `computed-literal` (#2042) to match JS's `obj["method"]()` convention
// instead of looking identical to a static `t.handler()` call.
const symbols = parseLua(`t["handler"]()`);
expect(symbols.calls).toContainEqual(
expect.objectContaining({ name: 'handler', receiver: 't' }),
expect.objectContaining({
name: 'handler',
receiver: 't',
dynamic: true,
dynamicKind: 'computed-literal',
}),
);
expect(symbols.calls.some((c) => c.dynamicKind === 'computed-key')).toBe(false);
});

it('resolves a bracket-index call with a single-quoted string-literal key directly', () => {
it('resolves a bracket-index call with a single-quoted string-literal key directly, tagged computed-literal', () => {
const symbols = parseLua(`t['handler']()`);
expect(symbols.calls).toContainEqual(
expect.objectContaining({ name: 'handler', receiver: 't' }),
expect.objectContaining({
name: 'handler',
receiver: 't',
dynamic: true,
dynamicKind: 'computed-literal',
}),
);
});

Expand Down
Loading