fix(extractors): extract rest/default bindings from dynamic import() destructures#2052
Open
carlos-alm wants to merge 1 commit into
Conversation
…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
Contributor
Contributor
Codegraph Impact Analysis2 functions changed → 12 callers affected across 1 files
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
extractDynamicImportNames(WASM/TS) only recognizedshorthand_property_identifier_patternandpair_patternchildren when collecting names from an object-pattern destructure of a dynamicimport()result, so a rest element (...rest) was silently dropped entirely and a shorthand default ({ a = 1 }) produced no name at all.cargo testbefore writing the fix (removed afterward): native'sobject_assignment_pattern(default-value) handling was correct, but its sharedextract_rest_identifierhelper 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.object_assignment_patternandrest_pattern/rest_elementbranches toextractDynamicImportNames's object_pattern handling. Extracted a sharedextractRestPatternIdentifierhelper that scans all children for the actualidentifiernode (rather than assuming a fixed index) and reused it to fix the array_pattern branch's identical pre-existing bug.extract_rest_identifierto scan all children for theidentifiernode instead of assumingchild(0), mirroring the already-correct pattern used byextract_array_pattern_bindingselsewhere in the same file. This one shared helper backs bothcollect_object_pattern_namesandcollect_array_pattern_names, so the fix covers object- and array-pattern destructures in one change.collectDynamicImportand query-basedhandleDynamicImportCall) call the same sharedextractDynamicImportNamesfunction, so both are fixed by this one change (confirmed viacodegraph 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 failednpx napi build --platform --release+codesign --sign - --force) so the full suite could exercise the real native engine rather than silently skipping itnpm test— full suite: 254 files, 4023 passed, 30 skipped, 2 todo, 0 failednpm run lint— clean on touched filescodegraph 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 extractorFiled #2051 for an out-of-scope finding discovered while investigating:
extractDestructuredBindings/extract_destructured_bindings(the generic object-destructuring-to-Definitionextractor used for plainconst { a, ...rest } = xdeclarations, 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.