Skip to content

Commit 34c64bf

Browse files
committed
feat(extract): record JS/TS receiver binding facts for imports and AMD
The same-module guard for #1128 needs to know, from the caller's own source, whether a member call's receiver is bound to another module. Two facts were missing: - A JS/TS import statement that binds no local name (side-effect `import './x'`, a re-export, a bare or destructured `require('x')`, `require('x').y`) still records the path's last segment as its local_name, so `require('node:stream').Readable` made every `stream` parameter look import-bound. CBMImport.local_name_synthesized marks those stand-ins. A lazy `x = require('x')` and `const x = require('x').member` do bind that very name and stay unmarked. local_name itself is unchanged, so IMPORTS edges and the import map are untouched. - AMD `define([...], function (dep) {...})` / `require([...], fn)` bind each factory parameter to the dependency at the same position. That binding is local to the factory and is not an import (no IMPORTS edge), so the unified walk carries it like the Python parameter bindings: the loader call notes its factory, the factory's frame binds the dependency parameters, the frame's pop unwinds them. The CommonJS-wrapper pseudo dependencies (`exports`, `module`, `require`) stay unbound. O(1) per node, no ancestor walk. The JS member-call branch reads it into CBMCall.receiver_is_amd_dependency. Nothing reads either fact yet; the graph is unchanged by this commit. Signed-off-by: Martin Vogel <[email protected]>
1 parent f3bcb25 commit 34c64bf

6 files changed

Lines changed: 364 additions & 4 deletions

File tree

‎internal/cbm/cbm.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,12 @@ typedef struct {
376376
// parameter of an enclosing function, so it cannot be the
377377
// module-level foo. Python only today. Read by the
378378
// weak-local-binding guard. Default false.
379+
bool receiver_is_amd_dependency; // JS/TS member call x.foo() whose receiver root x is a
380+
// parameter the factory of an enclosing AMD
381+
// define([...], fn) / require([...], fn) binds to a
382+
// dependency (#1128). Not an import (no IMPORTS
383+
// edge): read by the same-module bound-receiver
384+
// guard. Default false.
379385
bool receiver_is_self_attribute; // Python member call whose receiver is an attribute
380386
// chain rooted at self/cls but not self/cls itself
381387
// (self.compiler.apply_converters()). An object the
@@ -393,6 +399,10 @@ typedef enum {
393399
} CBMImportKind;
394400

395401
typedef struct {
402+
/* JS/TS: the statement binds no local name (side-effect `import './x'`,
403+
* re-export, bare `require('x').y`), so local_name is the path's last segment,
404+
* not a binding the file can call through (#1128). Default false. */
405+
bool local_name_synthesized;
396406
const char *local_name; // local alias or name
397407
const char *module_path; // resolved module path / QN
398408
const char *member; // ES named import: the exported name the local binds (`get` in

‎internal/cbm/extract_calls.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4418,6 +4418,16 @@ static CBMInvocationDescriptor describe_emitted_primary_call(
44184418
return descriptor;
44194419
}
44204420

4421+
/* #1128: the node that roots a JS/TS member receiver (`a` in `a.b.c()`). The
4422+
* walk is over this one callee's own member chain, so it stays linear in the
4423+
* file; a chain rooted in a call result stops at that call. */
4424+
static TSNode js_receiver_root(TSNode obj) {
4425+
while (!ts_node_is_null(obj) && strcmp(ts_node_type(obj), "member_expression") == 0) {
4426+
obj = ts_node_child_by_field_name(obj, TS_FIELD("object"));
4427+
}
4428+
return obj;
4429+
}
4430+
44214431
CBMInvocationDescriptor handle_calls(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec,
44224432
WalkState *state) {
44234433
CBMInvocationDescriptor invocation = {0};
@@ -4489,6 +4499,8 @@ CBMInvocationDescriptor handle_calls(CBMExtractCtx *ctx, TSNode node, const CBML
44894499
const char *ok = ts_node_type(obj);
44904500
if (strcmp(ok, "this") != 0 && strcmp(ok, "super") != 0) {
44914501
call.is_method = true;
4502+
call.receiver_is_amd_dependency =
4503+
cbm_walk_amd_dependency_binds(ctx, state, js_receiver_root(obj));
44924504
}
44934505
}
44944506
}

‎internal/cbm/extract_imports.c‎

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -941,12 +941,38 @@ static bool process_es_import_statement(CBMExtractCtx *ctx, TSNode node) {
941941
}
942942
}
943943
if (!found) {
944-
CBMImport imp = {.local_name = path_last(a, path), .module_path = path};
944+
/* A side-effect import (`import './polyfills'`) binds no name. */
945+
CBMImport imp = {
946+
.local_name = path_last(a, path), .module_path = path, .local_name_synthesized = true};
945947
cbm_imports_push(&ctx->result->imports, a, imp);
946948
}
947949
return true;
948950
}
949951

952+
/* The identifier a require call is bound to when no declarator holds the call
953+
* itself: a lazy assignment `winston = require('winston')`, or a declarator
954+
* over one member `const dns = require('dns').promises`. NULL otherwise. */
955+
static const char *commonjs_require_bound_name(CBMExtractCtx *ctx, TSNode parent) {
956+
if (ts_node_is_null(parent)) {
957+
return NULL;
958+
}
959+
TSNode name = {0};
960+
const char *kind = ts_node_type(parent);
961+
if (strcmp(kind, "assignment_expression") == 0) {
962+
name = ts_node_child_by_field_name(parent, TS_FIELD("left"));
963+
} else if (strcmp(kind, "member_expression") == 0) {
964+
TSNode declarator = ts_node_parent(parent);
965+
if (!ts_node_is_null(declarator) &&
966+
strcmp(ts_node_type(declarator), "variable_declarator") == 0) {
967+
name = ts_node_child_by_field_name(declarator, TS_FIELD("name"));
968+
}
969+
}
970+
if (ts_node_is_null(name) || strcmp(ts_node_type(name), "identifier") != 0) {
971+
return NULL;
972+
}
973+
return cbm_node_text(ctx->arena, name, ctx->source);
974+
}
975+
950976
/* Handle CommonJS `require("path")` call_expression nodes. The local name
951977
* is derived from the enclosing variable_declarator / assignment when
952978
* possible (so `const foo = require('./foo')` emits local_name="foo"),
@@ -1004,11 +1030,19 @@ static bool process_commonjs_require(CBMExtractCtx *ctx, TSNode call) {
10041030
local_name = cbm_node_text(a, name_node, ctx->source);
10051031
}
10061032
}
1007-
if (!local_name) {
1033+
/* No declarator binds the module (`require('x').y`, a destructuring, a
1034+
* lazy assignment or a bare side-effect require): the path's last segment
1035+
* stands in. It is still a real binding when the statement binds exactly
1036+
* that name (#1128). */
1037+
bool synthesized = local_name == NULL;
1038+
if (synthesized) {
10081039
local_name = path_last(a, path);
1040+
const char *bound = commonjs_require_bound_name(ctx, parent);
1041+
synthesized = !(bound && local_name && strcmp(bound, local_name) == 0);
10091042
}
10101043

1011-
CBMImport imp = {.local_name = local_name, .module_path = path};
1044+
CBMImport imp = {
1045+
.local_name = local_name, .module_path = path, .local_name_synthesized = synthesized};
10121046
cbm_imports_push(&ctx->result->imports, a, imp);
10131047
return true;
10141048
}
@@ -1035,8 +1069,10 @@ static void walk_es_imports(CBMExtractCtx *ctx, TSNode root) {
10351069
if (!ts_node_is_null(src)) {
10361070
char *path = strip_quotes(ctx->arena, cbm_node_text(ctx->arena, src, ctx->source));
10371071
if (path && path[0]) {
1072+
/* A re-export binds no local name in this file. */
10381073
CBMImport imp = {.local_name = path_last(ctx->arena, path),
1039-
.module_path = path};
1074+
.module_path = path,
1075+
.local_name_synthesized = true};
10401076
cbm_imports_push(&ctx->result->imports, ctx->arena, imp);
10411077
}
10421078
}

‎internal/cbm/extract_unified.c‎

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,171 @@ static void py_bind_scope_parameters(CBMExtractCtx *ctx, TSNode node, WalkState
306306
}
307307
}
308308

309+
/* ── AMD dependency bindings (#1128) ──────────────────────────────────────
310+
* `define(['axios', './util'], function (axios, util) {...})` and
311+
* `require([...], function (...) {...})` bind each factory parameter to the
312+
* module at the same position of the dependency array. That binding is local
313+
* to the factory and is not an import of the file (it mints no IMPORTS edge),
314+
* so it rides the walk like the Python parameters above: the loader call notes
315+
* the factory it passes, and when the walk opens that factory's frame its
316+
* dependency parameters are bound into the same per-frame map and unwound with
317+
* the frame. A JS/TS walk binds nothing else there. O(1) per node and no
318+
* ancestor walk (the CBMWalkScope note says why that would be quadratic). */
319+
enum { AMD_LOADER_MAX_ARGS = 3 }; /* define(id?, dependencies?, factory) */
320+
321+
static bool amd_js_walk(CBMLanguage lang) {
322+
return lang == CBM_LANG_JAVASCRIPT || lang == CBM_LANG_TYPESCRIPT || lang == CBM_LANG_TSX ||
323+
lang == CBM_LANG_ARKTS;
324+
}
325+
326+
static bool amd_node_text_is(const CBMExtractCtx *ctx, TSNode node, const char *text) {
327+
uint32_t start = ts_node_start_byte(node);
328+
uint32_t end = ts_node_end_byte(node);
329+
size_t len = strlen(text);
330+
return end >= start && (size_t)(end - start) == len && end <= (uint32_t)ctx->source_len &&
331+
memcmp(ctx->source + start, text, len) == 0;
332+
}
333+
334+
static bool amd_factory_kind(const char *kind) {
335+
return strcmp(kind, "function_expression") == 0 || strcmp(kind, "function") == 0 ||
336+
strcmp(kind, "arrow_function") == 0;
337+
}
338+
339+
/* At a loader call `define(...)` / `require(...)` / `requirejs(...)` with a
340+
* dependency array followed by an inline factory, note that factory. */
341+
static void amd_note_factory(const CBMExtractCtx *ctx, TSNode node, WalkState *state) {
342+
if (strcmp(ts_node_type(node), "call_expression") != 0) {
343+
return;
344+
}
345+
TSNode fn = ts_node_child_by_field_name(node, TS_FIELD("function"));
346+
if (ts_node_is_null(fn) || strcmp(ts_node_type(fn), "identifier") != 0 ||
347+
!(amd_node_text_is(ctx, fn, "define") || amd_node_text_is(ctx, fn, "require") ||
348+
amd_node_text_is(ctx, fn, "requirejs"))) {
349+
return;
350+
}
351+
TSNode args = ts_node_child_by_field_name(node, TS_FIELD("arguments"));
352+
uint32_t count = ts_node_is_null(args) ? 0 : ts_node_named_child_count(args);
353+
if (count == 0 || count > AMD_LOADER_MAX_ARGS) {
354+
return; /* not the loader's signature */
355+
}
356+
TSNode deps = {0};
357+
for (uint32_t i = 0; i < count; i++) {
358+
TSNode arg = ts_node_named_child(args, i);
359+
const char *kind = ts_node_type(arg);
360+
if (strcmp(kind, "array") == 0) {
361+
deps = arg;
362+
} else if (amd_factory_kind(kind) && !ts_node_is_null(deps)) {
363+
state->amd_factory_start_byte = ts_node_start_byte(arg);
364+
state->amd_factory_end_byte = ts_node_end_byte(arg);
365+
state->amd_dependencies = deps;
366+
return;
367+
}
368+
}
369+
}
370+
371+
/* `require`, `exports` and `module` are the CommonJS-wrapper pseudo
372+
* dependencies: `exports` and `module` are THIS module, so their parameters
373+
* stay unbound. */
374+
static bool amd_pseudo_dependency(const CBMExtractCtx *ctx, TSNode dep) {
375+
static const char *const pseudo[] = {"'require'", "\"require\"", "'exports'",
376+
"\"exports\"", "'module'", "\"module\""};
377+
for (size_t i = 0; i < sizeof(pseudo) / sizeof(pseudo[0]); i++) {
378+
if (amd_node_text_is(ctx, dep, pseudo[i])) {
379+
return true;
380+
}
381+
}
382+
return false;
383+
}
384+
385+
/* The identifier a JS/TS factory parameter binds, or NULL for a destructuring
386+
* or rest pattern (those bind no dependency module as a whole). */
387+
static const char *amd_parameter_name(CBMExtractCtx *ctx, TSNode param) {
388+
const char *kind = ts_node_type(param);
389+
TSNode ident = param;
390+
if (strcmp(kind, "required_parameter") == 0 || strcmp(kind, "optional_parameter") == 0) {
391+
ident = ts_node_child_by_field_name(param, TS_FIELD("pattern"));
392+
} else if (strcmp(kind, "assignment_pattern") == 0) {
393+
ident = ts_node_child_by_field_name(param, TS_FIELD("left"));
394+
}
395+
if (ts_node_is_null(ident) || strcmp(ts_node_type(ident), "identifier") != 0) {
396+
return NULL;
397+
}
398+
return cbm_node_text(ctx->arena, ident, ctx->source);
399+
}
400+
401+
/* Step a cursor to the next named, non-comment sibling; false at the end. The
402+
* first call must pass `first` = true to enter the children. Cursor steps are
403+
* O(1), so pairing a dependency list with a parameter list stays linear. */
404+
static bool amd_next_item(TSTreeCursor *cursor, bool first) {
405+
bool moved =
406+
first ? ts_tree_cursor_goto_first_child(cursor) : ts_tree_cursor_goto_next_sibling(cursor);
407+
while (moved) {
408+
TSNode node = ts_tree_cursor_current_node(cursor);
409+
if (ts_node_is_named(node) && strcmp(ts_node_type(node), "comment") != 0) {
410+
return true;
411+
}
412+
moved = ts_tree_cursor_goto_next_sibling(cursor);
413+
}
414+
return false;
415+
}
416+
417+
/* When the walk opens the noted factory's frame (pushed at `depth`), bind each
418+
* parameter whose dependency is a module string. */
419+
static void amd_bind_factory_parameters(CBMExtractCtx *ctx, TSNode node, WalkState *state,
420+
uint32_t depth) {
421+
if (state->amd_factory_end_byte == 0 ||
422+
ts_node_start_byte(node) != state->amd_factory_start_byte ||
423+
ts_node_end_byte(node) != state->amd_factory_end_byte ||
424+
!amd_factory_kind(ts_node_type(node))) {
425+
return;
426+
}
427+
TSNode deps = state->amd_dependencies;
428+
state->amd_factory_start_byte = 0;
429+
state->amd_factory_end_byte = 0;
430+
state->amd_dependencies = (TSNode){0};
431+
/* Bind only into the factory's own frame, or the bindings would outlive it. */
432+
if (state->scope_top == 0 || state->scopes[state->scope_top - SKIP_ONE].depth != depth) {
433+
return;
434+
}
435+
TSNode single = ts_node_child_by_field_name(node, TS_FIELD("parameter")); /* `dep => ...` */
436+
TSNode params = ts_node_child_by_field_name(node, TS_FIELD("parameters"));
437+
if (ts_node_is_null(single) && ts_node_is_null(params)) {
438+
return;
439+
}
440+
TSTreeCursor dep_cursor = ts_tree_cursor_new(deps);
441+
TSTreeCursor param_cursor = ts_tree_cursor_new(ts_node_is_null(single) ? params : node);
442+
bool have_dep = amd_next_item(&dep_cursor, true);
443+
bool have_param = ts_node_is_null(single) ? amd_next_item(&param_cursor, true) : true;
444+
while (have_dep && have_param) {
445+
TSNode dep = ts_tree_cursor_current_node(&dep_cursor);
446+
TSNode param =
447+
ts_node_is_null(single) ? ts_tree_cursor_current_node(&param_cursor) : single;
448+
if (strcmp(ts_node_type(dep), "string") == 0 && !amd_pseudo_dependency(ctx, dep)) {
449+
py_param_bind(state, amd_parameter_name(ctx, param));
450+
}
451+
have_dep = amd_next_item(&dep_cursor, false);
452+
have_param = ts_node_is_null(single) && amd_next_item(&param_cursor, false);
453+
}
454+
ts_tree_cursor_delete(&dep_cursor);
455+
ts_tree_cursor_delete(&param_cursor);
456+
}
457+
458+
static void amd_track_bindings(CBMExtractCtx *ctx, TSNode node, WalkState *state, uint32_t depth) {
459+
if (!amd_js_walk(ctx->language)) {
460+
return;
461+
}
462+
amd_bind_factory_parameters(ctx, node, state, depth);
463+
amd_note_factory(ctx, node, state);
464+
}
465+
466+
bool cbm_walk_amd_dependency_binds(CBMExtractCtx *ctx, const WalkState *state, TSNode ident) {
467+
if (!ctx || !state || !amd_js_walk(state->language) || state->py_param_stack_count == 0 ||
468+
ts_node_is_null(ident) || strcmp(ts_node_type(ident), "identifier") != 0) {
469+
return false;
470+
}
471+
return cbm_walk_python_param_is_bound(state, cbm_node_text(ctx->arena, ident, ctx->source));
472+
}
473+
309474
static bool push_scope(WalkState *state, uint8_t kind, uint32_t depth, const char *qn) {
310475
if (!ensure_scope_capacity(state)) {
311476
return false;
@@ -2564,6 +2729,7 @@ static void push_boundary_scopes(CBMExtractCtx *ctx, TSNode node, const CBMLangS
25642729

25652730
push_lexical_boundary(node, state, depth);
25662731
py_bind_scope_parameters(ctx, node, state);
2732+
amd_track_bindings(ctx, node, state, depth);
25672733
push_call_scope(state, depth, invocation);
25682734
if (is_actual_import_boundary(ctx, node, spec) && !is_export_of_declaration(node)) {
25692735
push_scope(state, SCOPE_IMPORT, depth, NULL);

‎internal/cbm/extract_unified.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,14 @@ typedef struct {
198198
/* Allocation failure: stop tracking and answer "not bound" forever after,
199199
* which can only cost a suppression, never a true edge. */
200200
bool py_param_tracking_failed;
201+
/* #1128: the factory the AMD define/require call visited last passes (its
202+
* exact span; end 0 = none) and that call's dependency array. The walk
203+
* reaches the factory right after the array, and when it opens the
204+
* factory's frame the dependency parameters are bound into the parameter
205+
* map above, which a JS/TS walk uses for nothing else. */
206+
uint32_t amd_factory_start_byte;
207+
uint32_t amd_factory_end_byte;
208+
TSNode amd_dependencies;
201209

202210
CBMLanguage language;
203211

@@ -209,6 +217,12 @@ typedef struct {
209217
* caller can only ever lose a suppression, never a true edge. */
210218
bool cbm_walk_python_param_is_bound(const WalkState *state, const char *name);
211219

220+
/* #1128: is identifier `ident` a parameter that the factory of an enclosing AMD
221+
* define([...], fn) / require([...], fn) binds to a dependency module? JS/TS
222+
* walks only. O(1); false on any failure, so a caller can only lose a
223+
* suppression, never a true edge. */
224+
bool cbm_walk_amd_dependency_binds(CBMExtractCtx *ctx, const WalkState *state, TSNode ident);
225+
212226
// Per-node handler prototypes. Each is called once per node during the
213227
// unified cursor walk, replacing the old recursive walk_* functions.
214228
CBMInvocationDescriptor handle_calls(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec,

0 commit comments

Comments
 (0)