@@ -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+
309474static 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 );
0 commit comments