@@ -522,6 +522,10 @@ class JavascriptParser extends Parser {
522522varDeclarationVar : new HookMap ( ( ) => new SyncBailHook ( [ "declaration" ] ) ) ,
523523/** @type {HookMap<SyncBailHook<[Identifier], boolean | void>> } */
524524pattern : new HookMap ( ( ) => new SyncBailHook ( [ "pattern" ] ) ) ,
525+ /** @type {SyncBailHook<[Expression], boolean | void> } */
526+ collectDestructuringAssignmentProperties : new SyncBailHook ( [
527+ "expression"
528+ ] ) ,
525529/** @type {HookMap<SyncBailHook<[Expression], boolean | void>> } */
526530canRename : new HookMap ( ( ) => new SyncBailHook ( [ "initExpression" ] ) ) ,
527531/** @type {HookMap<SyncBailHook<[Expression], boolean | void>> } */
@@ -2607,34 +2611,48 @@ class JavascriptParser extends Parser {
26072611 * @param {AssignmentExpression } expression assignment expression
26082612 */
26092613preWalkAssignmentExpression ( expression ) {
2614+ this . enterDestructuringAssignment ( expression . left , expression . right ) ;
2615+ }
2616+
2617+ /**
2618+ * @param {Pattern } pattern pattern
2619+ * @param {Expression } expression assignment expression
2620+ * @returns {Expression | undefined } destructuring expression
2621+ */
2622+ enterDestructuringAssignment ( pattern , expression ) {
26102623if (
2611- expression . left . type !== "ObjectPattern" ||
2624+ pattern . type !== "ObjectPattern" ||
26122625! this . destructuringAssignmentProperties
26132626) {
26142627return ;
26152628}
2616- const keys = this . _preWalkObjectPattern ( expression . left ) ;
2617- if ( ! keys ) return ;
2618-
2619- // check multiple assignments
2620- if ( this . destructuringAssignmentProperties . has ( expression ) ) {
2621- const set =
2622- /** @type {Set<DestructuringAssignmentProperty> } */
2623- ( this . destructuringAssignmentProperties . get ( expression ) ) ;
2624- this . destructuringAssignmentProperties . delete ( expression ) ;
2625- for ( const id of set ) keys . add ( id ) ;
2626- }
26272629
2628- this . destructuringAssignmentProperties . set (
2629- expression . right . type === "AwaitExpression"
2630- ? expression . right . argument
2631- : expression . right ,
2632- keys
2633- ) ;
2630+ const expr =
2631+ expression . type === "AwaitExpression" ? expression . argument : expression ;
2632+
2633+ const destructuring =
2634+ expr . type === "AssignmentExpression"
2635+ ? this . enterDestructuringAssignment ( expr . left , expr . right )
2636+ : this . hooks . collectDestructuringAssignmentProperties . call ( expr )
2637+ ? expr
2638+ : undefined ;
2639+
2640+ if ( destructuring ) {
2641+ const keys = this . _preWalkObjectPattern ( pattern ) ;
2642+ if ( ! keys ) return ;
26342643
2635- if ( expression . right . type === "AssignmentExpression" ) {
2636- this . preWalkAssignmentExpression ( expression . right ) ;
2644+ // check multiple assignments
2645+ if ( this . destructuringAssignmentProperties . has ( destructuring ) ) {
2646+ const set =
2647+ /** @type {Set<DestructuringAssignmentProperty> } */
2648+ ( this . destructuringAssignmentProperties . get ( destructuring ) ) ;
2649+ for ( const id of keys ) set . add ( id ) ;
2650+ } else {
2651+ this . destructuringAssignmentProperties . set ( destructuring , keys ) ;
2652+ }
26372653}
2654+
2655+ return destructuring ;
26382656}
26392657
26402658/**
@@ -2995,25 +3013,8 @@ class JavascriptParser extends Parser {
29953013 * @param {VariableDeclarator } declarator variable declarator
29963014 */
29973015preWalkVariableDeclarator ( declarator ) {
2998- if (
2999- ! declarator . init ||
3000- declarator . id . type !== "ObjectPattern" ||
3001- ! this . destructuringAssignmentProperties
3002- ) {
3003- return ;
3004- }
3005- const keys = this . _preWalkObjectPattern ( declarator . id ) ;
3006-
3007- if ( ! keys ) return ;
3008- this . destructuringAssignmentProperties . set (
3009- declarator . init . type === "AwaitExpression"
3010- ? declarator . init . argument
3011- : declarator . init ,
3012- keys
3013- ) ;
3014-
3015- if ( declarator . init . type === "AssignmentExpression" ) {
3016- this . preWalkAssignmentExpression ( declarator . init ) ;
3016+ if ( declarator . init ) {
3017+ this . enterDestructuringAssignment ( declarator . id , declarator . init ) ;
30173018}
30183019}
30193020
@@ -5179,7 +5180,7 @@ class JavascriptParser extends Parser {
51795180}
51805181
51815182/**
5182- * @param {MemberExpression } expression an expression
5183+ * @param {Expression } expression an expression
51835184 * @returns {{ name: string, rootInfo: ExportedVariableInfo, getMembers: () => string[]} | undefined } name info
51845185 */
51855186getNameForExpression ( expression ) {
0 commit comments