Skip to content

JS/TS: rest-pattern identifier never extracted in dynamic-import/CJS-require name collection (wrong child index) #2037

Description

@carlos-alm

Summary

Both engines' dynamic-import()/CJS-require() name-collection helpers have a rest_pattern/rest_element branch that reads the identifier from the wrong child index, so it never actually extracts the rest-bound name.

  • Rust (crates/codegraph-core/src/extractors/javascript.rs, extract_rest_identifier, used by collect_array_pattern_names and collect_object_pattern_names): reads rest_node.child(0) and checks .kind() == "identifier".
  • WASM/TS (src/extractors/javascript.ts, extractDynamicImportNames's array_pattern branch): reads child.child(0) || child.childForFieldName('name'), then checks .type === 'identifier'.

Evidence

Direct AST inspection of const [a, ...rest] = fn(); (tree-sitter-javascript grammar):

array_pattern ["[a, ...rest]"]
  identifier ["a"]
  rest_pattern ["...rest"]
    ... ["..."]          <- child(0)
    identifier ["rest"]  <- child(1)

child(0) of a rest_pattern is always the ... token itself, not the bound identifier — the identifier is at child(1). So both helpers' identifier check always fails and the rest binding is silently dropped.

The correct pattern already exists elsewhere in the same file and is proven working: extractParameters's own rest_pattern branch (src/extractors/javascript.ts line ~1833) uses child.child(1) || child.childForFieldName('name'), and the Rust parameter-extraction equivalent (crates/codegraph-core/src/extractors/javascript.rs ~line 2295-2307) scans all children for an identifier kind instead of assuming index 0.

Impact

  • const [a, ...rest] = await import('./mod.js') never registers rest as an imported/destructured name — rest's properties/usages can't be attributed back to the dynamic import.
  • const { a, ...rest } = require('./mod') (Rust collect_object_pattern_names) has the same issue for CJS require rest bindings.

Suggested fix

Fix extract_rest_identifier (Rust) to scan the rest node's children for the identifier kind (mirroring the working parameter-extraction code), and fix the array_pattern/rest branch in extractDynamicImportNames (TS) to use child.child(1) (mirroring extractParameters's own rest_pattern branch), for both array and object pattern rest bindings, in both engines.

Discovered while investigating #1901 (array-pattern destructuring Definition extraction) — unrelated code path, out of scope there.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions