@@ -2783,6 +2783,68 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
2783
2783
}
2784
2784
}
2785
2785
2786
+ /**
2787
+ * Emit ES7 exponentiation operator downlevel using Math.pow
2788
+ * @param node a binary expression node containing exponentiationOperator (**, **=)
2789
+ */
2790
+ function emitExponentiationOperator ( node : BinaryExpression ) {
2791
+ let leftHandSideExpression = node . left ;
2792
+ if ( node . operatorToken . kind === SyntaxKind . AsteriskAsteriskEqualsToken ) {
2793
+ let synthesizedLHS : ElementAccessExpression | PropertyAccessExpression ;
2794
+ let shouldEmitParentheses = false ;
2795
+ if ( isElementAccessExpression ( leftHandSideExpression ) ) {
2796
+ shouldEmitParentheses = true ;
2797
+ write ( "(" ) ;
2798
+
2799
+ synthesizedLHS = < ElementAccessExpression > createSynthesizedNode ( SyntaxKind . ElementAccessExpression , /*startsOnNewLine*/ false ) ;
2800
+
2801
+ let identifier = emitTempVariableAssignment ( leftHandSideExpression . expression , /*canDefinedTempVariablesInPlaces*/ false , /*shouldEmitCommaBeforeAssignment*/ false ) ;
2802
+ synthesizedLHS . expression = identifier ;
2803
+
2804
+ if ( leftHandSideExpression . argumentExpression . kind !== SyntaxKind . NumericLiteral &&
2805
+ leftHandSideExpression . argumentExpression . kind !== SyntaxKind . StringLiteral ) {
2806
+ let tempArgumentExpression = createAndRecordTempVariable ( TempFlags . _i ) ;
2807
+ ( < ElementAccessExpression > synthesizedLHS ) . argumentExpression = tempArgumentExpression ;
2808
+ emitAssignment ( tempArgumentExpression , leftHandSideExpression . argumentExpression , /*shouldEmitCommaBeforeAssignment*/ true ) ;
2809
+ }
2810
+ else {
2811
+ ( < ElementAccessExpression > synthesizedLHS ) . argumentExpression = leftHandSideExpression . argumentExpression ;
2812
+ }
2813
+ write ( ", " ) ;
2814
+ }
2815
+ else if ( isPropertyAccessExpression ( leftHandSideExpression ) ) {
2816
+ shouldEmitParentheses = true ;
2817
+ write ( "(" ) ;
2818
+ synthesizedLHS = < PropertyAccessExpression > createSynthesizedNode ( SyntaxKind . PropertyAccessExpression , /*startsOnNewLine*/ false ) ;
2819
+
2820
+ let identifier = emitTempVariableAssignment ( leftHandSideExpression . expression , /*canDefinedTempVariablesInPlaces*/ false , /*shouldemitCommaBeforeAssignment*/ false ) ;
2821
+ synthesizedLHS . expression = identifier ;
2822
+
2823
+ ( < PropertyAccessExpression > synthesizedLHS ) . dotToken = leftHandSideExpression . dotToken ;
2824
+ ( < PropertyAccessExpression > synthesizedLHS ) . name = leftHandSideExpression . name ;
2825
+ write ( ", " ) ;
2826
+ }
2827
+
2828
+ emit ( synthesizedLHS || leftHandSideExpression ) ;
2829
+ write ( " = " ) ;
2830
+ write ( "Math.pow(" ) ;
2831
+ emit ( synthesizedLHS || leftHandSideExpression ) ;
2832
+ write ( ", " ) ;
2833
+ emit ( node . right ) ;
2834
+ write ( ")" ) ;
2835
+ if ( shouldEmitParentheses ) {
2836
+ write ( ")" ) ;
2837
+ }
2838
+ }
2839
+ else {
2840
+ write ( "Math.pow(" ) ;
2841
+ emit ( leftHandSideExpression ) ;
2842
+ write ( ", " ) ;
2843
+ emit ( node . right ) ;
2844
+ write ( ")" ) ;
2845
+ }
2846
+ }
2847
+
2786
2848
function emitBinaryExpression ( node : BinaryExpression ) {
2787
2849
if ( languageVersion < ScriptTarget . ES6 && node . operatorToken . kind === SyntaxKind . EqualsToken &&
2788
2850
( node . left . kind === SyntaxKind . ObjectLiteralExpression || node . left . kind === SyntaxKind . ArrayLiteralExpression ) ) {
@@ -2800,12 +2862,27 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
2800
2862
emitNodeWithoutSourceMap ( node . left ) ;
2801
2863
write ( `", ` ) ;
2802
2864
}
2803
- emit ( node . left ) ;
2804
- let indentedBeforeOperator = indentIfOnDifferentLines ( node , node . left , node . operatorToken , node . operatorToken . kind !== SyntaxKind . CommaToken ? " " : undefined ) ;
2805
- write ( tokenToString ( node . operatorToken . kind ) ) ;
2806
- let indentedAfterOperator = indentIfOnDifferentLines ( node , node . operatorToken , node . right , " " ) ;
2807
- emit ( node . right ) ;
2808
- decreaseIndentIf ( indentedBeforeOperator , indentedAfterOperator ) ;
2865
+
2866
+ if ( node . operatorToken . kind === SyntaxKind . AsteriskAsteriskToken || node . operatorToken . kind === SyntaxKind . AsteriskAsteriskEqualsToken ) {
2867
+ // Downleveled emit exponentiation operator using Math.pow
2868
+ emitExponentiationOperator ( node ) ;
2869
+ }
2870
+ else {
2871
+ emit ( node . left ) ;
2872
+ // Add indentation before emit the operator if the operator is on different line
2873
+ // For example:
2874
+ // 3
2875
+ // + 2;
2876
+ // emitted as
2877
+ // 3
2878
+ // + 2;
2879
+ let indentedBeforeOperator = indentIfOnDifferentLines ( node , node . left , node . operatorToken , node . operatorToken . kind !== SyntaxKind . CommaToken ? " " : undefined ) ;
2880
+ write ( tokenToString ( node . operatorToken . kind ) ) ;
2881
+ let indentedAfterOperator = indentIfOnDifferentLines ( node , node . operatorToken , node . right , " " ) ;
2882
+ emit ( node . right ) ;
2883
+ decreaseIndentIf ( indentedBeforeOperator , indentedAfterOperator ) ;
2884
+ }
2885
+
2809
2886
if ( exportChanged ) {
2810
2887
write ( ")" ) ;
2811
2888
}
@@ -3442,6 +3519,58 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
3442
3519
write ( ";" ) ;
3443
3520
}
3444
3521
3522
+ /**
3523
+ * Emit an assignment to a given identifier, 'name', with a given expression, 'value'.
3524
+ * @param name an identifier as a left-hand-side operand of the assignment
3525
+ * @param value an expression as a right-hand-side operand of the assignment
3526
+ * @param shouldEmitCommaBeforeAssignment a boolean indicating whether to prefix an assignment with comma
3527
+ */
3528
+ function emitAssignment ( name : Identifier , value : Expression , shouldEmitCommaBeforeAssignment : boolean ) {
3529
+ if ( shouldEmitCommaBeforeAssignment ) {
3530
+ write ( ", " ) ;
3531
+ }
3532
+
3533
+ let exportChanged = isNameOfExportedSourceLevelDeclarationInSystemExternalModule ( name ) ;
3534
+
3535
+ if ( exportChanged ) {
3536
+ write ( `${ exportFunctionForFile } ("` ) ;
3537
+ emitNodeWithCommentsAndWithoutSourcemap ( name ) ;
3538
+ write ( `", ` ) ;
3539
+ }
3540
+
3541
+ const isVariableDeclarationOrBindingElement =
3542
+ name . parent && ( name . parent . kind === SyntaxKind . VariableDeclaration || name . parent . kind === SyntaxKind . BindingElement ) ;
3543
+
3544
+ if ( isVariableDeclarationOrBindingElement ) {
3545
+ emitModuleMemberName ( < Declaration > name . parent ) ;
3546
+ }
3547
+ else {
3548
+ emit ( name ) ;
3549
+ }
3550
+
3551
+ write ( " = " ) ;
3552
+ emit ( value ) ;
3553
+
3554
+ if ( exportChanged ) {
3555
+ write ( ")" ) ;
3556
+ }
3557
+ }
3558
+
3559
+ /**
3560
+ * Create temporary variable, emit an assignment of the variable the given expression
3561
+ * @param expression an expression to assign to the newly created temporary variable
3562
+ * @param canDefineTempVariablesInPlace a boolean indicating whether you can define the temporary variable at an assignment location
3563
+ * @param shouldEmitCommaBeforeAssignment a boolean indicating whether an assignment should prefix with comma
3564
+ */
3565
+ function emitTempVariableAssignment ( expression : Expression , canDefineTempVariablesInPlace : boolean , shouldEmitCommaBeforeAssignment : boolean ) : Identifier {
3566
+ let identifier = createTempVariable ( TempFlags . Auto ) ;
3567
+ if ( ! canDefineTempVariablesInPlace ) {
3568
+ recordTempDeclaration ( identifier ) ;
3569
+ }
3570
+ emitAssignment ( identifier , expression , shouldEmitCommaBeforeAssignment ) ;
3571
+ return identifier ;
3572
+ }
3573
+
3445
3574
function emitDestructuring ( root : BinaryExpression | VariableDeclaration | ParameterDeclaration , isAssignmentExpressionStatement : boolean , value ?: Expression ) {
3446
3575
let emitCount = 0 ;
3447
3576
@@ -3467,36 +3596,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
3467
3596
emitBindingElement ( < BindingElement > root , value ) ;
3468
3597
}
3469
3598
3470
- function emitAssignment ( name : Identifier , value : Expression ) {
3471
- if ( emitCount ++ ) {
3472
- write ( ", " ) ;
3473
- }
3474
-
3475
- const isVariableDeclarationOrBindingElement =
3476
- name . parent && ( name . parent . kind === SyntaxKind . VariableDeclaration || name . parent . kind === SyntaxKind . BindingElement ) ;
3477
-
3478
- let exportChanged = isNameOfExportedSourceLevelDeclarationInSystemExternalModule ( name ) ;
3479
-
3480
- if ( exportChanged ) {
3481
- write ( `${ exportFunctionForFile } ("` ) ;
3482
- emitNodeWithCommentsAndWithoutSourcemap ( name ) ;
3483
- write ( `", ` ) ;
3484
- }
3485
-
3486
- if ( isVariableDeclarationOrBindingElement ) {
3487
- emitModuleMemberName ( < Declaration > name . parent ) ;
3488
- }
3489
- else {
3490
- emit ( name ) ;
3491
- }
3492
-
3493
- write ( " = " ) ;
3494
- emit ( value ) ;
3495
-
3496
- if ( exportChanged ) {
3497
- write ( ")" ) ;
3498
- }
3499
- }
3500
3599
3501
3600
/**
3502
3601
* Ensures that there exists a declared identifier whose value holds the given expression.
@@ -3512,11 +3611,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
3512
3611
return expr ;
3513
3612
}
3514
3613
3515
- let identifier = createTempVariable ( TempFlags . Auto ) ;
3516
- if ( ! canDefineTempVariablesInPlace ) {
3517
- recordTempDeclaration ( identifier ) ;
3518
- }
3519
- emitAssignment ( identifier , expr ) ;
3614
+ let identifier = emitTempVariableAssignment ( expr , canDefineTempVariablesInPlace , emitCount > 0 ) ;
3615
+ emitCount ++ ;
3520
3616
return identifier ;
3521
3617
}
3522
3618
@@ -3623,7 +3719,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
3623
3719
emitArrayLiteralAssignment ( < ArrayLiteralExpression > target , value ) ;
3624
3720
}
3625
3721
else {
3626
- emitAssignment ( < Identifier > target , value ) ;
3722
+ emitAssignment ( < Identifier > target , value , /*shouldEmitCommaBeforeAssignment*/ emitCount > 0 ) ;
3723
+ emitCount ++ ;
3627
3724
}
3628
3725
}
3629
3726
@@ -3692,7 +3789,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
3692
3789
}
3693
3790
}
3694
3791
else {
3695
- emitAssignment ( < Identifier > target . name , value ) ;
3792
+ emitAssignment ( < Identifier > target . name , value , /*shouldEmitCommaBeforeAssignment*/ emitCount > 0 ) ;
3793
+ emitCount ++ ;
3696
3794
}
3697
3795
}
3698
3796
}
0 commit comments