@@ -1411,30 +1411,41 @@ const _super = (function (geti, seti) {
14111411 }
14121412
14131413 function shouldEmitBlockFunctionBodyOnSingleLine ( parentNode : Node , body : Block ) {
1414+ // We must emit a function body as a single-line body in the following case:
1415+ // * The body has NodeEmitFlags.SingleLine specified.
1416+
1417+ // We must emit a function body as a multi-line body in the following cases:
1418+ // * The body is explicitly marked as multi-line.
1419+ // * A non-synthesized body's start and end position are on different lines.
1420+ // * Any statement in the body starts on a new line.
1421+
1422+ if ( getNodeEmitFlags ( body ) & NodeEmitFlags . SingleLine ) {
1423+ return true ;
1424+ }
1425+
14141426 if ( body . multiLine ) {
14151427 return false ;
14161428 }
14171429
1418- const originalNode = getOriginalNode ( parentNode ) ;
1419- if ( isFunctionLike ( originalNode ) && ! nodeIsSynthesized ( originalNode ) ) {
1420- const body = originalNode . body ;
1421- if ( isBlock ( body ) ) {
1422- if ( rangeEndIsOnSameLineAsRangeStart ( body , body ) ) {
1423- for ( const statement of body . statements ) {
1424- if ( synthesizedNodeStartsOnNewLine ( statement ) ) {
1425- return false ;
1426- }
1427- }
1430+ if ( ! nodeIsSynthesized ( body ) && ! rangeIsOnSingleLine ( body , currentSourceFile ) ) {
1431+ return false ;
1432+ }
14281433
1429- return true ;
1430- }
1431- }
1432- else {
1433- return rangeEndIsOnSameLineAsRangeStart ( ( < ArrowFunction > originalNode ) . equalsGreaterThanToken , originalNode . body ) ;
1434+ if ( shouldWriteLeadingLineTerminator ( body , body . statements , ListFormat . PreserveLines )
1435+ || shouldWriteClosingLineTerminator ( body , body . statements , ListFormat . PreserveLines ) ) {
1436+ return false ;
1437+ }
1438+
1439+ let previousStatement : Statement ;
1440+ for ( const statement of body . statements ) {
1441+ if ( shouldWriteSeparatingLineTerminator ( previousStatement , statement , ListFormat . PreserveLines ) ) {
1442+ return false ;
14341443 }
1444+
1445+ previousStatement = statement ;
14351446 }
14361447
1437- return false ;
1448+ return true ;
14381449 }
14391450
14401451 function emitBlockFunctionBody ( parentNode : Node , body : Block ) {
@@ -1457,7 +1468,7 @@ const _super = (function (geti, seti) {
14571468
14581469 const endingLine = writer . getLine ( ) ;
14591470 emitLexicalEnvironment ( endLexicalEnvironment ( ) , /*newLine*/ startingLine !== endingLine ) ;
1460- emitLeadingComments ( collapseTextRange ( body . statements , TextRangeCollapse . CollapseToEnd ) ) ;
1471+ emitLeadingComments ( collapseRangeToEnd ( body . statements ) ) ;
14611472 decreaseIndent ( ) ;
14621473 }
14631474
@@ -1738,7 +1749,7 @@ const _super = (function (geti, seti) {
17381749 }
17391750
17401751 function emitCaseOrDefaultClauseStatements ( parentNode : Node , statements : NodeArray < Statement > ) {
1741- if ( statements . length === 1 && rangeStartPositionsAreOnSameLine ( parentNode , statements [ 0 ] ) ) {
1752+ if ( statements . length === 1 && rangeStartPositionsAreOnSameLine ( parentNode , statements [ 0 ] , currentSourceFile ) ) {
17421753 write ( " " ) ;
17431754 emit ( statements [ 0 ] ) ;
17441755 }
@@ -1778,7 +1789,7 @@ const _super = (function (geti, seti) {
17781789 // }
17791790 // "comment1" is not considered to be leading comment for node.initializer
17801791 // but rather a trailing comment on the previous node.
1781- emitLeadingComments ( node . initializer , getTrailingComments ( collapseTextRange ( node . initializer , TextRangeCollapse . CollapseToStart ) ) ) ;
1792+ emitLeadingComments ( node . initializer , getTrailingComments ( collapseRangeToStart ( node . initializer ) ) ) ;
17821793 emitExpression ( node . initializer ) ;
17831794 }
17841795
@@ -2236,13 +2247,13 @@ const _super = (function (geti, seti) {
22362247
22372248 const firstChild = children [ 0 ] ;
22382249 if ( firstChild === undefined ) {
2239- return ! positionsAreOnSameLine ( getStartPos ( parentNode ) , parentNode . end ) ;
2250+ return ! rangeIsOnSingleLine ( parentNode , currentSourceFile ) ;
22402251 }
22412252 else if ( positionIsSynthesized ( parentNode . pos ) || nodeIsSynthesized ( firstChild ) ) {
22422253 return synthesizedNodeStartsOnNewLine ( firstChild , format ) ;
22432254 }
22442255 else {
2245- return ! rangeStartPositionsAreOnSameLine ( parentNode , firstChild ) ;
2256+ return ! rangeStartPositionsAreOnSameLine ( parentNode , firstChild , currentSourceFile ) ;
22462257 }
22472258 }
22482259 else {
@@ -2262,7 +2273,7 @@ const _super = (function (geti, seti) {
22622273 return synthesizedNodeStartsOnNewLine ( previousNode , format ) || synthesizedNodeStartsOnNewLine ( nextNode , format ) ;
22632274 }
22642275 else {
2265- return ! rangeEndIsOnSameLineAsRangeStart ( previousNode , nextNode ) ;
2276+ return ! rangeEndIsOnSameLineAsRangeStart ( previousNode , nextNode , currentSourceFile ) ;
22662277 }
22672278 }
22682279 else {
@@ -2281,13 +2292,13 @@ const _super = (function (geti, seti) {
22812292
22822293 const lastChild = lastOrUndefined ( children ) ;
22832294 if ( lastChild === undefined ) {
2284- return ! positionsAreOnSameLine ( getStartPos ( parentNode ) , parentNode . end ) ;
2295+ return ! rangeIsOnSingleLine ( parentNode , currentSourceFile ) ;
22852296 }
22862297 else if ( positionIsSynthesized ( parentNode . pos ) || nodeIsSynthesized ( lastChild ) ) {
22872298 return synthesizedNodeStartsOnNewLine ( lastChild , format ) ;
22882299 }
22892300 else {
2290- return ! rangeEndPositionsAreOnSameLine ( parentNode , lastChild ) ;
2301+ return ! rangeEndPositionsAreOnSameLine ( parentNode , lastChild , currentSourceFile ) ;
22912302 }
22922303 }
22932304 else {
@@ -2304,28 +2315,8 @@ const _super = (function (geti, seti) {
23042315
23052316 return startsOnNewLine ;
23062317 }
2307- return ( format & ListFormat . PreferNewLine ) !== 0 ;
2308- }
2309-
2310- function rangeStartPositionsAreOnSameLine ( range1 : TextRange , range2 : TextRange ) {
2311- return positionsAreOnSameLine ( getStartPos ( range1 ) , getStartPos ( range2 ) ) ;
2312- }
23132318
2314- function rangeEndPositionsAreOnSameLine ( range1 : TextRange , range2 : TextRange ) {
2315- return positionsAreOnSameLine ( range1 . end , range2 . end ) ;
2316- }
2317-
2318- function rangeEndIsOnSameLineAsRangeStart ( range1 : TextRange , range2 : TextRange ) {
2319- return positionsAreOnSameLine ( range1 . end , getStartPos ( range2 ) ) ;
2320- }
2321-
2322- function positionsAreOnSameLine ( pos1 : number , pos2 : number ) {
2323- return pos1 === pos2 ||
2324- getLineOfLocalPosition ( currentSourceFile , pos1 ) === getLineOfLocalPosition ( currentSourceFile , pos2 ) ;
2325- }
2326-
2327- function getStartPos ( range : TextRange ) {
2328- return range . pos === - 1 ? - 1 : skipTrivia ( currentText , range . pos ) ;
2319+ return ( format & ListFormat . PreferNewLine ) !== 0 ;
23292320 }
23302321
23312322 function needsIndentation ( parent : Node , node1 : Node , node2 : Node ) : boolean {
@@ -2341,7 +2332,7 @@ const _super = (function (geti, seti) {
23412332 return ! nodeIsSynthesized ( parent )
23422333 && ! nodeIsSynthesized ( node1 )
23432334 && ! nodeIsSynthesized ( node2 )
2344- && ! rangeEndIsOnSameLineAsRangeStart ( node1 , node2 ) ;
2335+ && ! rangeEndIsOnSameLineAsRangeStart ( node1 , node2 , currentSourceFile ) ;
23452336 }
23462337
23472338 function skipSynthesizedParentheses ( node : Node ) {
@@ -2381,7 +2372,7 @@ const _super = (function (geti, seti) {
23812372 function isSingleLineEmptyBlock ( block : Block ) {
23822373 return ! block . multiLine
23832374 && block . statements . length === 0
2384- && rangeEndIsOnSameLineAsRangeStart ( block , block ) ;
2375+ && rangeEndIsOnSameLineAsRangeStart ( block , block , currentSourceFile ) ;
23852376 }
23862377
23872378 function getNotEmittedParent ( node : Node ) : Node {
0 commit comments