@@ -2713,7 +2713,21 @@ module ts {
2713
2713
emit ( node . whenFalse ) ;
2714
2714
}
2715
2715
2716
+ function isSingleLineBlock ( node : Node ) {
2717
+ if ( node && node . kind === SyntaxKind . Block ) {
2718
+ var block = < Block > node ;
2719
+ return block . statements . length === 0 && nodeEndIsOnSameLineAsNodeStart ( block , block ) ;
2720
+ }
2721
+ }
2722
+
2716
2723
function emitBlock ( node : Block ) {
2724
+ if ( isSingleLineBlock ( node ) ) {
2725
+ emitToken ( SyntaxKind . OpenBraceToken , node . pos ) ;
2726
+ write ( " " ) ;
2727
+ emitToken ( SyntaxKind . CloseBraceToken , node . statements . end ) ;
2728
+ return ;
2729
+ }
2730
+
2717
2731
emitToken ( SyntaxKind . OpenBraceToken , node . pos ) ;
2718
2732
increaseIndent ( ) ;
2719
2733
scopeEmitStart ( node . parent ) ;
@@ -2886,6 +2900,11 @@ module ts {
2886
2900
getLineOfLocalPosition ( currentSourceFile , skipTrivia ( currentSourceFile . text , node2 . pos ) ) ;
2887
2901
}
2888
2902
2903
+ function nodeEndIsOnSameLineAsNodeStart ( node1 : Node , node2 : Node ) {
2904
+ return getLineOfLocalPosition ( currentSourceFile , node1 . end ) ===
2905
+ getLineOfLocalPosition ( currentSourceFile , skipTrivia ( currentSourceFile . text , node2 . pos ) ) ;
2906
+ }
2907
+
2889
2908
function emitCaseOrDefaultClause ( node : CaseOrDefaultClause ) {
2890
2909
if ( node . kind === SyntaxKind . CaseClause ) {
2891
2910
write ( "case " ) ;
@@ -3385,73 +3404,79 @@ module ts {
3385
3404
emitSignatureParameters ( node ) ;
3386
3405
}
3387
3406
3388
- write ( " {" ) ;
3389
- scopeEmitStart ( node ) ;
3390
-
3391
- if ( ! node . body ) {
3392
- writeLine ( ) ;
3393
- write ( "}" ) ;
3407
+ if ( isSingleLineBlock ( node . body ) ) {
3408
+ write ( " { }" ) ;
3394
3409
}
3395
3410
else {
3396
- increaseIndent ( ) ;
3397
-
3398
- emitDetachedComments ( node . body . kind === SyntaxKind . Block ? ( < Block > node . body ) . statements : node . body ) ;
3399
-
3400
- var startIndex = 0 ;
3401
- if ( node . body . kind === SyntaxKind . Block ) {
3402
- startIndex = emitDirectivePrologues ( ( < Block > node . body ) . statements , /*startWithNewLine*/ true ) ;
3403
- }
3404
- var outPos = writer . getTextPos ( ) ;
3405
-
3406
- emitCaptureThisForNodeIfNecessary ( node ) ;
3407
- emitDefaultValueAssignments ( node ) ;
3408
- emitRestParameter ( node ) ;
3409
- if ( node . body . kind !== SyntaxKind . Block && outPos === writer . getTextPos ( ) ) {
3410
- decreaseIndent ( ) ;
3411
- write ( " " ) ;
3412
- emitStart ( node . body ) ;
3413
- write ( "return " ) ;
3411
+ write ( " {" ) ;
3412
+ scopeEmitStart ( node ) ;
3414
3413
3415
- // Don't emit comments on this body. We'll have already taken care of it above
3416
- // when we called emitDetachedComments.
3417
- emitNode ( node . body , /*disableComments:*/ true ) ;
3418
- emitEnd ( node . body ) ;
3419
- write ( ";" ) ;
3420
- emitTempDeclarations ( /*newLine*/ false ) ;
3421
- write ( " " ) ;
3422
- emitStart ( node . body ) ;
3414
+ if ( ! node . body ) {
3415
+ writeLine ( ) ;
3423
3416
write ( "}" ) ;
3424
- emitEnd ( node . body ) ;
3425
3417
}
3426
3418
else {
3419
+ increaseIndent ( ) ;
3420
+
3421
+ emitDetachedComments ( node . body . kind === SyntaxKind . Block ? ( < Block > node . body ) . statements : node . body ) ;
3422
+
3423
+ var startIndex = 0 ;
3427
3424
if ( node . body . kind === SyntaxKind . Block ) {
3428
- emitLinesStartingAt ( ( < Block > node . body ) . statements , startIndex ) ;
3425
+ startIndex = emitDirectivePrologues ( ( < Block > node . body ) . statements , /*startWithNewLine*/ true ) ;
3429
3426
}
3430
- else {
3431
- writeLine ( ) ;
3432
- emitLeadingComments ( node . body ) ;
3427
+ var outPos = writer . getTextPos ( ) ;
3428
+
3429
+ emitCaptureThisForNodeIfNecessary ( node ) ;
3430
+ emitDefaultValueAssignments ( node ) ;
3431
+ emitRestParameter ( node ) ;
3432
+ if ( node . body . kind !== SyntaxKind . Block && outPos === writer . getTextPos ( ) ) {
3433
+ decreaseIndent ( ) ;
3434
+ write ( " " ) ;
3435
+ emitStart ( node . body ) ;
3433
3436
write ( "return " ) ;
3434
- emit ( node . body , /*disableComments:*/ true ) ;
3437
+
3438
+ // Don't emit comments on this body. We'll have already taken care of it above
3439
+ // when we called emitDetachedComments.
3440
+ emitNode ( node . body , /*disableComments:*/ true ) ;
3441
+ emitEnd ( node . body ) ;
3435
3442
write ( ";" ) ;
3436
- emitTrailingComments ( node . body ) ;
3437
- }
3438
- emitTempDeclarations ( /*newLine*/ true ) ;
3439
- writeLine ( ) ;
3440
- if ( node . body . kind === SyntaxKind . Block ) {
3441
- emitLeadingCommentsOfPosition ( ( < Block > node . body ) . statements . end ) ;
3442
- decreaseIndent ( ) ;
3443
- emitToken ( SyntaxKind . CloseBraceToken , ( < Block > node . body ) . statements . end ) ;
3444
- }
3445
- else {
3446
- decreaseIndent ( ) ;
3443
+ emitTempDeclarations ( /*newLine*/ false ) ;
3444
+ write ( " " ) ;
3447
3445
emitStart ( node . body ) ;
3448
3446
write ( "}" ) ;
3449
3447
emitEnd ( node . body ) ;
3450
3448
}
3449
+ else {
3450
+ if ( node . body . kind === SyntaxKind . Block ) {
3451
+ emitLinesStartingAt ( ( < Block > node . body ) . statements , startIndex ) ;
3452
+ }
3453
+ else {
3454
+ writeLine ( ) ;
3455
+ emitLeadingComments ( node . body ) ;
3456
+ write ( "return " ) ;
3457
+ emit ( node . body , /*disableComments:*/ true ) ;
3458
+ write ( ";" ) ;
3459
+ emitTrailingComments ( node . body ) ;
3460
+ }
3461
+ emitTempDeclarations ( /*newLine*/ true ) ;
3462
+ writeLine ( ) ;
3463
+ if ( node . body . kind === SyntaxKind . Block ) {
3464
+ emitLeadingCommentsOfPosition ( ( < Block > node . body ) . statements . end ) ;
3465
+ decreaseIndent ( ) ;
3466
+ emitToken ( SyntaxKind . CloseBraceToken , ( < Block > node . body ) . statements . end ) ;
3467
+ }
3468
+ else {
3469
+ decreaseIndent ( ) ;
3470
+ emitStart ( node . body ) ;
3471
+ write ( "}" ) ;
3472
+ emitEnd ( node . body ) ;
3473
+ }
3474
+ }
3451
3475
}
3476
+
3477
+ scopeEmitEnd ( ) ;
3452
3478
}
3453
3479
3454
- scopeEmitEnd ( ) ;
3455
3480
if ( node . flags & NodeFlags . Export ) {
3456
3481
writeLine ( ) ;
3457
3482
emitStart ( node ) ;
0 commit comments