@@ -758,20 +758,105 @@ namespace ts {
758
758
return visitEachChild ( node , visitor , context ) ;
759
759
}
760
760
761
+ function wrapPrivateNameForDestructuringTarget ( node : PrivateNamedPropertyAccess ) {
762
+ return createPropertyAccess (
763
+ createObjectLiteral ( [
764
+ createSetAccessor (
765
+ /*decorators*/ undefined ,
766
+ /*modifiers*/ undefined ,
767
+ "value" ,
768
+ [ createParameter (
769
+ /*decorators*/ undefined ,
770
+ /*modifiers*/ undefined ,
771
+ /*dotDotDotToken*/ undefined , "x" ,
772
+ /*questionToken*/ undefined ,
773
+ /*type*/ undefined ,
774
+ /*initializer*/ undefined
775
+ ) ] ,
776
+ createBlock (
777
+ [ createExpressionStatement (
778
+ createAssignment (
779
+ visitNode ( node , visitor ) ,
780
+ createIdentifier ( "x" )
781
+ )
782
+ ) ]
783
+ )
784
+ )
785
+ ] ) ,
786
+ "value"
787
+ ) ;
788
+ }
789
+
790
+ function transformDestructuringAssignmentTarget ( node : ArrayLiteralExpression | ObjectLiteralExpression ) {
791
+ const hasPrivateNames = isArrayLiteralExpression ( node ) ?
792
+ forEach ( node . elements , isPrivateNamedPropertyAccess ) :
793
+ forEach ( node . properties , property => isPropertyAssignment ( property ) && isPrivateNamedPropertyAccess ( property . initializer ) ) ;
794
+ if ( ! hasPrivateNames ) {
795
+ return node ;
796
+ }
797
+ if ( isArrayLiteralExpression ( node ) ) {
798
+ // Transforms private names in destructuring assignment array bindings.
799
+ //
800
+ // Source:
801
+ // ([ this.#myProp ] = [ "hello" ]);
802
+ //
803
+ // Transformation:
804
+ // [ { set value(x) { this.#myProp = x; } }.value ] = [ "hello" ];
805
+ return updateArrayLiteral (
806
+ node ,
807
+ node . elements . map (
808
+ expr => isPrivateNamedPropertyAccess ( expr ) ?
809
+ wrapPrivateNameForDestructuringTarget ( expr ) :
810
+ expr
811
+ )
812
+ ) ;
813
+ }
814
+ else {
815
+ // Transforms private names in destructuring assignment object bindings.
816
+ //
817
+ // Source:
818
+ // ({ stringProperty: this.#myProp } = { stringProperty: "hello" });
819
+ //
820
+ // Transformation:
821
+ // ({ stringProperty: { set value(x) { this.#myProp = x; } }.value }) = { stringProperty: "hello" };
822
+ return updateObjectLiteral (
823
+ node ,
824
+ node . properties . map (
825
+ prop => isPropertyAssignment ( prop ) && isPrivateNamedPropertyAccess ( prop . initializer ) ?
826
+ updatePropertyAssignment (
827
+ prop ,
828
+ prop . name ,
829
+ wrapPrivateNameForDestructuringTarget ( prop . initializer )
830
+ ) :
831
+ prop
832
+ )
833
+ ) ;
834
+ }
835
+ }
836
+
761
837
/**
762
838
* Visits a BinaryExpression that contains a destructuring assignment.
763
839
*
764
840
* @param node A BinaryExpression node.
765
841
*/
766
842
function visitBinaryExpression ( node : BinaryExpression , noDestructuringValue : boolean ) : Expression {
767
- if ( isDestructuringAssignment ( node ) && node . left . transformFlags & TransformFlags . ContainsObjectRestOrSpread ) {
768
- return flattenDestructuringAssignment (
769
- node ,
770
- visitor ,
771
- context ,
772
- FlattenLevel . ObjectRest ,
773
- ! noDestructuringValue
774
- ) ;
843
+ if ( isDestructuringAssignment ( node ) ) {
844
+ const left = transformDestructuringAssignmentTarget ( node . left ) ;
845
+ if ( left !== node . left || node . left . transformFlags & TransformFlags . ContainsObjectRestOrSpread ) {
846
+ return flattenDestructuringAssignment (
847
+ left === node . left ? node : updateBinary (
848
+ node ,
849
+ left ,
850
+ node . right ,
851
+ node . operatorToken . kind
852
+ ) as DestructuringAssignment ,
853
+ visitor ,
854
+ context ,
855
+ node . left . transformFlags & TransformFlags . ContainsObjectRestOrSpread
856
+ ? FlattenLevel . ObjectRest : FlattenLevel . All ,
857
+ ! noDestructuringValue
858
+ ) ;
859
+ }
775
860
}
776
861
else if ( node . operatorToken . kind === SyntaxKind . CommaToken ) {
777
862
return updateBinary (
0 commit comments