File tree 15 files changed +88
-9
lines changed
test/function/samples/object-expression-treeshaking
deconflict-destructured-array-pattern
deconflict-destructured-for-default-side-effects
deconflict-destructured-for-default-side-effects-array
deconflict-destructured-for-key-side-effects 15 files changed +88
-9
lines changed Original file line number Diff line number Diff line change @@ -87,8 +87,21 @@ export default class ArrayPattern extends NodeBase implements DeclarationPattern
87
87
let included = false ;
88
88
const includedPatternPath = getIncludedPatternPath ( destructuredInitPath ) ;
89
89
for ( const element of this . elements ) {
90
- included =
91
- element ?. includeDestructuredIfNecessary ( context , includedPatternPath , init ) || included ;
90
+ if ( element ) {
91
+ element . included ||= included ;
92
+ included =
93
+ element . includeDestructuredIfNecessary ( context , includedPatternPath , init ) || included ;
94
+ }
95
+ }
96
+ if ( included ) {
97
+ // This is necessary so that if any pattern element is included, all are
98
+ // included for proper deconflicting
99
+ for ( const element of this . elements ) {
100
+ if ( element && ! element . included ) {
101
+ element . included = true ;
102
+ element . includeDestructuredIfNecessary ( context , includedPatternPath , init ) ;
103
+ }
104
+ }
92
105
}
93
106
return ( this . included ||= included ) ;
94
107
}
Original file line number Diff line number Diff line change @@ -70,6 +70,12 @@ export default class AssignmentPattern extends NodeBase implements DeclarationPa
70
70
this . included ;
71
71
if ( ( included ||= this . right . shouldBeIncluded ( context ) ) ) {
72
72
this . right . includePath ( UNKNOWN_PATH , context , false ) ;
73
+ if ( ! this . left . included ) {
74
+ this . left . included = true ;
75
+ // Unfortunately, we need to include the left side again now, so that
76
+ // any declared variables are properly included.
77
+ this . left . includeDestructuredIfNecessary ( context , destructuredInitPath , init ) ;
78
+ }
73
79
}
74
80
return ( this . included = included ) ;
75
81
}
Original file line number Diff line number Diff line change @@ -77,15 +77,18 @@ export default class Property extends MethodBase implements DeclarationPatternNo
77
77
destructuredInitPath : ObjectPath ,
78
78
init : ExpressionEntity
79
79
) : boolean {
80
+ const path = this . getPathInProperty ( destructuredInitPath ) ;
80
81
let included =
81
- ( this . value as PatternNode ) . includeDestructuredIfNecessary (
82
- context ,
83
- this . getPathInProperty ( destructuredInitPath ) ,
84
- init
85
- ) || this . included ;
86
- included ||= this . key . hasEffects ( createHasEffectsContext ( ) ) ;
87
- if ( included ) {
82
+ ( this . value as PatternNode ) . includeDestructuredIfNecessary ( context , path , init ) ||
83
+ this . included ;
84
+ if ( ( included ||= this . key . hasEffects ( createHasEffectsContext ( ) ) ) ) {
88
85
this . key . includePath ( EMPTY_PATH , context , false ) ;
86
+ if ( ! this . value . included ) {
87
+ this . value . included = true ;
88
+ // Unfortunately, we need to include the value again now, so that any
89
+ // declared variables are properly included.
90
+ ( this . value as PatternNode ) . includeDestructuredIfNecessary ( context , path , init ) ;
91
+ }
89
92
}
90
93
return ( this . included = included ) ;
91
94
}
Original file line number Diff line number Diff line change
1
+ module . exports = defineTest ( {
2
+ description : 'makes sure to deconflict all variables in an array pattern if included'
3
+ } ) ;
Original file line number Diff line number Diff line change
1
+ const Foo = { ok : true } ;
2
+ export { Foo as default } ;
Original file line number Diff line number Diff line change
1
+ import bar from './dep.js' ;
2
+
3
+ const [ Foo , Bar ] = [ 1 , 2 ] ;
4
+
5
+ assert . deepStrictEqual ( bar , { ok : true } ) ;
6
+ assert . deepStrictEqual ( Bar , 2 ) ;
Original file line number Diff line number Diff line change
1
+ module . exports = defineTest ( {
2
+ description :
3
+ 'makes sure to deconflict variables that are destructured for side effects in their array pattern default values only'
4
+ } ) ;
Original file line number Diff line number Diff line change
1
+ const Foo = { ok : true } ;
2
+ export { Foo as default } ;
Original file line number Diff line number Diff line change
1
+ import bar from './dep.js' ;
2
+ let mutated = false ;
3
+ const [
4
+ Foo = ( ( ) => {
5
+ mutated = true ;
6
+ } ) ( )
7
+ ] = [ ] ;
8
+ assert . ok ( mutated ) ;
9
+ assert . deepStrictEqual ( bar , { ok : true } ) ;
Original file line number Diff line number Diff line change
1
+ module . exports = defineTest ( {
2
+ description :
3
+ 'makes sure to deconflict variables that are destructured for side effects in their default values only'
4
+ } ) ;
Original file line number Diff line number Diff line change
1
+ const Foo = { ok : true } ;
2
+ export { Foo as default } ;
Original file line number Diff line number Diff line change
1
+ import bar from './dep.js' ;
2
+ let mutated = false ;
3
+ const {
4
+ Foo = ( ( ) => {
5
+ mutated = true ;
6
+ } ) ( )
7
+ } = { } ;
8
+ assert . ok ( mutated ) ;
9
+ assert . deepStrictEqual ( bar , { ok : true } ) ;
Original file line number Diff line number Diff line change
1
+ module . exports = defineTest ( {
2
+ description :
3
+ 'makes sure to deconflict variables that are destructured for side effects in their key only'
4
+ } ) ;
Original file line number Diff line number Diff line change
1
+ const Foo = { ok : true } ;
2
+ export { Foo as default } ;
Original file line number Diff line number Diff line change
1
+ import bar from './dep.js' ;
2
+ let mutated = false ;
3
+ const {
4
+ [ ( ( ) => {
5
+ mutated = true ;
6
+ return 'Foo' ;
7
+ } ) ( ) ] : Foo
8
+ } = { Foo : true } ;
9
+ assert . ok ( mutated ) ;
10
+ assert . deepStrictEqual ( bar , { ok : true } ) ;
You can’t perform that action at this time.
0 commit comments