Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(extract): flag TS/JS member calls with non-this receivers as method calls

Hitoshi Okazaki60d0855541ec
diff --git a/internal/cbm/cbm.h b/internal/cbm/cbm.h
index b40305af9..5665039c1 100644
--- a/internal/cbm/cbm.h
+++ b/internal/cbm/cbm.h
@@ -236,7 +236,9 @@ typedef struct {
int loop_depth; // enclosing loop nesting at the call site
int branch_depth; // enclosing branch nesting at the call site
int start_line; // 1-based source line of the call (for def range-match)
- bool is_method; // Perl-only: arrow/method call ($obj->m). Default false.
+ bool is_method; // method/member call with a non-self receiver. Perl:
+ // arrow/method call ($obj->m). TS/JS/TSX: member call
+ // x.foo() whose receiver is not this/super. Default false.
} CBMCall;
typedef struct {
diff --git a/internal/cbm/extract_calls.c b/internal/cbm/extract_calls.c
index a7f4286ef..afe2938be 100644
--- a/internal/cbm/extract_calls.c
+++ b/internal/cbm/extract_calls.c
@@ -1892,6 +1892,29 @@ void handle_calls(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec, Walk
strcmp(ts_node_type(node), "method_call_expression") == 0) {
call.is_method = true;
}
+ // TS/JS/TSX receiver-aware guard (#592/#606 direction; same intent
+ // as the Perl flag above). Flag a member call x.foo() whose receiver
+ // is NOT `this`/`super`. When the TS-LSP cannot resolve the receiver
+ // type, the call-resolution pass suppresses weak short-name matches
+ // for these (so `re.test()` cannot fabricate an edge to a project
+ // `test`). this/super receivers stay unflagged — their target is the
+ // enclosing class, where a namespace-proximity weak match is usually
+ // right. Bare calls (helper()) and new_expression have no member
+ // receiver, so they keep is_method=false (struct is zero-init).
+ if ((ctx->language == CBM_LANG_JAVASCRIPT || ctx->language == CBM_LANG_TYPESCRIPT ||
+ ctx->language == CBM_LANG_TSX) &&
+ strcmp(ts_node_type(node), "call_expression") == 0) {
+ TSNode fn = ts_node_child_by_field_name(node, TS_FIELD("function"));
+ if (!ts_node_is_null(fn) && strcmp(ts_node_type(fn), "member_expression") == 0) {
+ TSNode obj = ts_node_child_by_field_name(fn, TS_FIELD("object"));
+ if (!ts_node_is_null(obj)) {
+ const char *ok = ts_node_type(obj);
+ if (strcmp(ok, "this") != 0 && strcmp(ok, "super") != 0) {
+ call.is_method = true;
+ }
+ }
+ }
+ }
TSNode args = ts_node_child_by_field_name(node, TS_FIELD("arguments"));
if (!ts_node_is_null(args)) {
diff --git a/tests/test_extraction.c b/tests/test_extraction.c
index e78265b5b..3dd54acb4 100644
--- a/tests/test_extraction.c
+++ b/tests/test_extraction.c
@@ -3224,16 +3224,97 @@ TEST(extract_perl_method_call_flags_is_method) {
PASS();
}
-/* Other languages must be unaffected: a JS method call never sets is_method
- * (the flag is Perl-only). */
-TEST(extract_non_perl_method_call_not_flagged_is_method) {
+/* Languages OUTSIDE the is_method flag set (only Perl and TS/JS/TSX set it) must
+ * be unaffected: a Go method call never sets is_method. */
+TEST(extract_flag_exempt_method_call_not_flagged_is_method) {
+ CBMFileResult *r = extract("package m\n"
+ "func run(o Obj) { o.Commit(); helper() }\n",
+ CBM_LANG_GO, "t", "x.go");
+ ASSERT_NOT_NULL(r);
+ ASSERT_FALSE(r->has_error);
+ for (int i = 0; i < r->calls.count; i++) {
+ ASSERT_FALSE(r->calls.items[i].is_method);
+ }
+ cbm_free_result(r);
+ PASS();
+}
+
+/* TS/JS/TSX receiver-aware flag (#592/#606; same intent as the Perl flag above).
+ * A member call x.foo() with a non-this/super receiver is flagged is_method so
+ * the resolver can suppress a weak short-name match (`re.test()` must not bind a
+ * project `test`); a bare call is not flagged. */
+TEST(extract_ts_member_call_flags_is_method) {
+ CBMFileResult *r = extract("const re = /^a+$/;\n"
+ "export function checkFormat(s: string) { return re.test(s); }\n"
+ "function helper() { return 1; }\n"
+ "export function run() { return helper(); }\n",
+ CBM_LANG_TYPESCRIPT, "t", "a.ts");
+ ASSERT_NOT_NULL(r);
+ ASSERT_FALSE(r->has_error);
+ int member = 0;
+ int bare = 0;
+ for (int i = 0; i < r->calls.count; i++) {
+ const char *cn = r->calls.items[i].callee_name;
+ if (strcmp(cn, "re.test") == 0) {
+ member++;
+ ASSERT_TRUE(r->calls.items[i].is_method);
+ }
+ if (strcmp(cn, "helper") == 0) {
+ bare++;
+ ASSERT_FALSE(r->calls.items[i].is_method);
+ }
+ }
+ ASSERT_TRUE(member >= 1); /* re.test() flagged */
+ ASSERT_TRUE(bare >= 1); /* helper() not flagged */
+ cbm_free_result(r);
+ PASS();
+}
+
+/* this/super receivers keep the enclosing-class target, where a weak
+ * namespace-proximity match is usually correct — so they are NOT flagged. A
+ * new_expression has no member receiver and is never flagged either. */
+TEST(extract_ts_this_super_receiver_not_flagged) {
+ CBMFileResult *r = extract("class A extends B {\n"
+ " m() { this.helper(); super.render(); return new A(); }\n"
+ " helper() {}\n"
+ "}\n",
+ CBM_LANG_TYPESCRIPT, "t", "b.ts");
+ ASSERT_NOT_NULL(r);
+ ASSERT_FALSE(r->has_error);
+ /* Every call here has a self receiver (this/super) or is a new-expression,
+ * so NONE may be flagged. */
+ for (int i = 0; i < r->calls.count; i++) {
+ ASSERT_FALSE(r->calls.items[i].is_method);
+ }
+ /* Sanity: the this/super member calls were actually extracted. */
+ ASSERT_TRUE(has_call(r, "helper")); /* this.helper */
+ ASSERT_TRUE(has_call(r, "render")); /* super.render */
+ cbm_free_result(r);
+ PASS();
+}
+
+/* JS dialect behaves like TS (the flag is gated on the language set, not the
+ * dialect). Replaces the pre-#592 test that asserted JS never flags is_method. */
+TEST(extract_js_member_call_flags_is_method) {
CBMFileResult *r =
extract("function run(o){ o.commit(); helper(); }\n", CBM_LANG_JAVASCRIPT, "t", "x.js");
ASSERT_NOT_NULL(r);
ASSERT_FALSE(r->has_error);
+ int member = 0;
+ int bare = 0;
for (int i = 0; i < r->calls.count; i++) {
- ASSERT_FALSE(r->calls.items[i].is_method);
+ const char *cn = r->calls.items[i].callee_name;
+ if (strcmp(cn, "o.commit") == 0) {
+ member++;
+ ASSERT_TRUE(r->calls.items[i].is_method);
+ }
+ if (strcmp(cn, "helper") == 0) {
+ bare++;
+ ASSERT_FALSE(r->calls.items[i].is_method);
+ }
}
+ ASSERT_TRUE(member >= 1); /* o.commit() flagged */
+ ASSERT_TRUE(bare >= 1); /* helper() not flagged */
cbm_free_result(r);
PASS();
}
@@ -3277,7 +3358,10 @@ SUITE(extraction) {
RUN_TEST(extract_perl_config_string_not_a_callee);
RUN_TEST(extract_perl_builtin_call_is_function_not_method);
RUN_TEST(extract_perl_method_call_flags_is_method);
- RUN_TEST(extract_non_perl_method_call_not_flagged_is_method);
+ RUN_TEST(extract_flag_exempt_method_call_not_flagged_is_method);
+ RUN_TEST(extract_ts_member_call_flags_is_method);
+ RUN_TEST(extract_ts_this_super_receiver_not_flagged);
+ RUN_TEST(extract_js_member_call_flags_is_method);
/* R box-module imports + member calls */
RUN_TEST(extract_r_box_use_imports_issue218);