@@ -2747,7 +2747,7 @@ namespace ts {
2747
2747
function emitYieldExpression ( node : YieldExpression ) {
2748
2748
emitTokenWithComment ( SyntaxKind . YieldKeyword , node . pos , writeKeyword , node ) ;
2749
2749
emit ( node . asteriskToken ) ;
2750
- emitExpressionWithLeadingSpace ( node . expression , parenthesizer . parenthesizeExpressionForDisallowedComma ) ;
2750
+ emitExpressionWithLeadingSpace ( node . expression && parenthesizeExpressionForNoAsi ( node . expression ) , parenthesizeExpressionForNoAsiAndDisallowedComma ) ;
2751
2751
}
2752
2752
2753
2753
function emitSpreadElement ( node : SpreadElement ) {
@@ -2971,9 +2971,49 @@ namespace ts {
2971
2971
return pos ;
2972
2972
}
2973
2973
2974
+ function commentWillEmitNewLine ( node : CommentRange ) {
2975
+ return node . kind === SyntaxKind . SingleLineCommentTrivia || ! ! node . hasTrailingNewLine ;
2976
+ }
2977
+
2978
+ function willEmitLeadingNewLine ( node : Expression ) : boolean {
2979
+ if ( ! currentSourceFile ) return false ;
2980
+ if ( some ( getLeadingCommentRanges ( currentSourceFile . text , node . pos ) , commentWillEmitNewLine ) ) return true ;
2981
+ if ( some ( getSyntheticLeadingComments ( node ) , commentWillEmitNewLine ) ) return true ;
2982
+ if ( isPartiallyEmittedExpression ( node ) ) {
2983
+ if ( node . pos !== node . expression . pos ) {
2984
+ if ( some ( getTrailingCommentRanges ( currentSourceFile . text , node . expression . pos ) , commentWillEmitNewLine ) ) return true ;
2985
+ }
2986
+ return willEmitLeadingNewLine ( node . expression ) ;
2987
+ }
2988
+ return false ;
2989
+ }
2990
+
2991
+ /**
2992
+ * Wraps an expression in parens if we would emit a leading comment that would introduce a line separator
2993
+ * between the node and its parent.
2994
+ */
2995
+ function parenthesizeExpressionForNoAsi ( node : Expression ) {
2996
+ if ( ! commentsDisabled && isPartiallyEmittedExpression ( node ) && willEmitLeadingNewLine ( node ) ) {
2997
+ const parseNode = getParseTreeNode ( node ) ;
2998
+ if ( parseNode && isParenthesizedExpression ( parseNode ) ) {
2999
+ // If the original node was a parenthesized expression, restore it to preserve comment and source map emit
3000
+ const parens = factory . createParenthesizedExpression ( node . expression ) ;
3001
+ setOriginalNode ( parens , node ) ;
3002
+ setTextRange ( parens , parseNode ) ;
3003
+ return parens ;
3004
+ }
3005
+ return factory . createParenthesizedExpression ( node ) ;
3006
+ }
3007
+ return node ;
3008
+ }
3009
+
3010
+ function parenthesizeExpressionForNoAsiAndDisallowedComma ( node : Expression ) {
3011
+ return parenthesizeExpressionForNoAsi ( parenthesizer . parenthesizeExpressionForDisallowedComma ( node ) ) ;
3012
+ }
3013
+
2974
3014
function emitReturnStatement ( node : ReturnStatement ) {
2975
3015
emitTokenWithComment ( SyntaxKind . ReturnKeyword , node . pos , writeKeyword , /*contextNode*/ node ) ;
2976
- emitExpressionWithLeadingSpace ( node . expression ) ;
3016
+ emitExpressionWithLeadingSpace ( node . expression && parenthesizeExpressionForNoAsi ( node . expression ) , parenthesizeExpressionForNoAsi ) ;
2977
3017
writeTrailingSemicolon ( ) ;
2978
3018
}
2979
3019
@@ -3005,7 +3045,7 @@ namespace ts {
3005
3045
3006
3046
function emitThrowStatement ( node : ThrowStatement ) {
3007
3047
emitTokenWithComment ( SyntaxKind . ThrowKeyword , node . pos , writeKeyword , node ) ;
3008
- emitExpressionWithLeadingSpace ( node . expression ) ;
3048
+ emitExpressionWithLeadingSpace ( parenthesizeExpressionForNoAsi ( node . expression ) , parenthesizeExpressionForNoAsi ) ;
3009
3049
writeTrailingSemicolon ( ) ;
3010
3050
}
3011
3051
0 commit comments