Skip to content

fix(extractors): extract rest/default bindings from dynamic import() destructures#2052

Open
carlos-alm wants to merge 1 commit into
fix/issue-1914-c-c-dynamic-tracer-trace-c-cpp-never-producesfrom
fix/issue-1920-wasm-dynamic-import-destructure-extraction
Open

fix(extractors): extract rest/default bindings from dynamic import() destructures#2052
carlos-alm wants to merge 1 commit into
fix/issue-1914-c-c-dynamic-tracer-trace-c-cpp-never-producesfrom
fix/issue-1920-wasm-dynamic-import-destructure-extraction

Conversation

@carlos-alm

Copy link
Copy Markdown
Contributor

Summary

  • extractDynamicImportNames (WASM/TS) only recognized shorthand_property_identifier_pattern and pair_pattern children when collecting names from an object-pattern destructure of a dynamic import() result, so a rest element (...rest) was silently dropped entirely and a shorthand default ({ a = 1 }) produced no name at all.
  • The issue assumed the native engine "already handles both cases correctly." That turned out to be only half true — verified empirically with a temporary cargo test before writing the fix (removed afterward): native's object_assignment_pattern (default-value) handling was correct, but its shared extract_rest_identifier helper indexed into a fixed child slot (child(0)) that is actually the ... token, not the bound identifier — so native also silently dropped rest bindings, for both object-pattern and array-pattern dynamic-import destructures.
  • Fixed both engines so they now agree and are both correct:
    • TS: added object_assignment_pattern and rest_pattern/rest_element branches to extractDynamicImportNames's object_pattern handling. Extracted a shared extractRestPatternIdentifier helper that scans all children for the actual identifier node (rather than assuming a fixed index) and reused it to fix the array_pattern branch's identical pre-existing bug.
    • Rust: fixed extract_rest_identifier to scan all children for the identifier node instead of assuming child(0), mirroring the already-correct pattern used by extract_array_pattern_bindings elsewhere in the same file. This one shared helper backs both collect_object_pattern_names and collect_array_pattern_names, so the fix covers object- and array-pattern destructures in one change.
  • Both TS extraction code paths (walk-based collectDynamicImport and query-based handleDynamicImportCall) call the same shared extractDynamicImportNames function, so both are fixed by this one change (confirmed via codegraph fn-impact).

Closes #1920

Test plan

  • npx vitest run tests/parsers/javascript.test.ts — 254 passed (4 new, covering rest/default/mixed bindings in object patterns and rest in array patterns)
  • cargo test --lib javascript (crates/codegraph-core) — 187 passed (4 new, mirroring the TS cases)
  • cargo test (crates/codegraph-core, full crate) — 603 passed, 0 failed
  • Built and codesigned the native addon locally (npx napi build --platform --release + codesign --sign - --force) so the full suite could exercise the real native engine rather than silently skipping it
  • npm test — full suite: 254 files, 4023 passed, 30 skipped, 2 todo, 0 failed
  • npm run lint — clean on touched files
  • codegraph diff-impact --staged -T — confirms the change is scoped to exactly the two functions touched (extractRestPatternIdentifier, extractDynamicImportNames), 12 transitive callers, all within the JS/TS extractor

Filed #2051 for an out-of-scope finding discovered while investigating: extractDestructuredBindings/extract_destructured_bindings (the generic object-destructuring-to-Definition extractor used for plain const { a, ...rest } = x declarations, unrelated to dynamic imports) has the same rest/default gap in both engines.

Stacked on #1914 per the current PR chain — only the extractor/test diff is this issue's change.

…destructures

extractDynamicImportNames (WASM/TS) only recognized shorthand and pair_pattern
children in an object-pattern destructure of a dynamic import() result, so a
rest element (...rest) was silently dropped and a shorthand default (a = 1)
produced no name at all.

Investigating the native engine — which was assumed to already handle both
cases correctly — showed that assumption was only half right: its
object_assignment_pattern (default value) handling was correct, but its
shared extract_rest_identifier helper indexed into a fixed child slot (0)
that is actually the `...` token, not the bound identifier, so rest bindings
were silently dropped in native too, for both object- and array-pattern
destructures.

Fixed both engines:
- TS: added object_assignment_pattern and rest_pattern/rest_element branches
  to the object_pattern case, and extracted extractRestPatternIdentifier
  (scans all children for the actual identifier node) to fix the array_pattern
  branch's identical pre-existing indexing bug.
- Rust: fixed extract_rest_identifier to scan all children instead of
  assuming child(0), mirroring the already-correct pattern used by
  extract_array_pattern_bindings elsewhere in the same file. This one shared
  helper backs both collect_object_pattern_names and
  collect_array_pattern_names, so both destructure shapes are fixed.

Closes #1920
@greptile-apps

greptile-apps Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a silent data-loss bug in dynamic import() destructure extraction for both the TypeScript (WASM) and Rust (native) engines: rest bindings (...rest) were dropped entirely and shorthand default bindings ({ a = 1 }) produced no name at all because both implementations assumed a fixed child index that actually pointed at the ... punctuation token rather than the identifier.

  • TypeScript engine: introduces extractRestPatternIdentifier (scans all children for identifier type), wires it into the object_pattern branch of extractDynamicImportNames alongside a new object_assignment_pattern arm for shorthand defaults, and replaces the broken fixed-index fallback in the array_pattern branch.
  • Rust engine: replaces child(0) in extract_rest_identifier with an identical child-scanning loop, fixing both collect_object_pattern_names and collect_array_pattern_names through the shared helper.
  • Tests: 4 new tests per engine (object rest, shorthand default, mixed, array rest) mirror each other across TS and Rust.

Confidence Score: 5/5

Safe to merge — both engines now agree and the fix is narrow and well-tested.

The change is a targeted two-line root cause fix in each engine (replace fixed-index child(0) access with a child-type scan) plus the new object_assignment_pattern arm in the TS extractor. Logic is symmetric across TS and Rust, the new extractRestPatternIdentifier helper matches the comment-documented intent of its Rust counterpart, and four regression tests per engine cover every affected code path. No existing behavior is altered for the already-correct cases.

No files require special attention.

Important Files Changed

Filename Overview
crates/codegraph-core/src/extractors/javascript.rs Fixes extract_rest_identifier to scan children for identifier rather than using child(0) (the ... token); adds 4 regression tests.
src/extractors/javascript.ts Adds extractRestPatternIdentifier helper and handles object_assignment_pattern / rest_pattern / rest_element in extractDynamicImportNames; fixes the same fixed-index bug in the array_pattern branch.
tests/parsers/javascript.test.ts Adds 4 new regression tests covering object rest, shorthand default, mixed pattern, and array rest destructuring of dynamic imports.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["dynamic import() call node"] --> B["Walk up AST through wrappers\nawait / parenthesized / as_expression"]
    B --> C{"variable_declarator?"}
    C -- No --> Z["return []"]
    C -- Yes --> D["childForFieldName('name')"]
    D --> E{"nameNode.type"}
    E -- identifier --> F["return [nameNode.text]"]
    E -- object_pattern --> G["iterate children"]
    E -- array_pattern --> H["iterate children"]
    G --> G1{"child.type"}
    G1 -- shorthand_property_identifier_pattern --> G2["push child.text"]
    G1 -- pair_pattern --> G3["extract local/key\npush local, record rename"]
    G1 -- object_assignment_pattern NEW --> G4["childForFieldName('left')\npush left.text"]
    G1 -- rest_pattern/rest_element NEW --> G5["extractRestPatternIdentifier\nscan children for identifier"]
    H --> H1{"child.type"}
    H1 -- identifier --> H2["push child.text"]
    H1 -- rest_pattern/rest_element FIXED --> H3["extractRestPatternIdentifier\n(was: child(0) = ... token)"]
    G5 --> R["return names"]
    G2 --> R
    G3 --> R
    G4 --> R
    H2 --> R
    H3 --> R
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
flowchart TD
    A["dynamic import() call node"] --> B["Walk up AST through wrappers\nawait / parenthesized / as_expression"]
    B --> C{"variable_declarator?"}
    C -- No --> Z["return []"]
    C -- Yes --> D["childForFieldName('name')"]
    D --> E{"nameNode.type"}
    E -- identifier --> F["return [nameNode.text]"]
    E -- object_pattern --> G["iterate children"]
    E -- array_pattern --> H["iterate children"]
    G --> G1{"child.type"}
    G1 -- shorthand_property_identifier_pattern --> G2["push child.text"]
    G1 -- pair_pattern --> G3["extract local/key\npush local, record rename"]
    G1 -- object_assignment_pattern NEW --> G4["childForFieldName('left')\npush left.text"]
    G1 -- rest_pattern/rest_element NEW --> G5["extractRestPatternIdentifier\nscan children for identifier"]
    H --> H1{"child.type"}
    H1 -- identifier --> H2["push child.text"]
    H1 -- rest_pattern/rest_element FIXED --> H3["extractRestPatternIdentifier\n(was: child(0) = ... token)"]
    G5 --> R["return names"]
    G2 --> R
    G3 --> R
    G4 --> R
    H2 --> R
    H3 --> R
Loading

Reviews (1): Last reviewed commit: "fix(extractors): extract rest/default bi..." | Re-trigger Greptile

@github-actions

github-actions Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Codegraph Impact Analysis

2 functions changed12 callers affected across 1 files

  • extractRestPatternIdentifier in src/extractors/javascript.ts:4696 (5 transitive callers)
  • extractDynamicImportNames in src/extractors/javascript.ts:4725 (16 transitive callers)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant