@@ -3088,71 +3088,21 @@ module ts {
30883088
30893089 write ( tokenToString ( node . operatorToken . kind ) ) ;
30903090
3091- // We'd like to preserve newlines found in the original binary expression. i.e. if a user has:
3092- //
3093- // Foo() ||
3094- // Bar();
3095- //
3096- // Then we'd like to emit it as such. It seems like we'd only need to check for a newline and
3097- // then just indent and emit. However, that will lead to a problem with deeply nested code.
3098- // i.e. if you have:
3099- //
3100- // Foo() ||
3101- // Bar() ||
3102- // Baz();
3103- //
3104- // Then we don't want to emit it as:
3105- //
3106- // Foo() ||
3107- // Bar() ||
3108- // Baz();
3109- //
3110- // So we only indent if the right side of the binary expression starts further in on the line
3111- // versus the left.
3112- var operatorEnd = getLineAndCharacterOfPosition ( currentSourceFile , node . operatorToken . end ) ;
3113- var rightStart = getLineAndCharacterOfPosition ( currentSourceFile , skipTrivia ( currentSourceFile . text , node . right . pos ) ) ;
3114-
31153091 // Check if the right expression is on a different line versus the operator itself. If so,
31163092 // we'll emit newline.
3117- var onDifferentLine = operatorEnd . line !== rightStart . line ;
3118- if ( onDifferentLine ) {
3119- // Also, if the right expression starts further in on the line than the left, then we'll indent.
3120- var exprStart = getLineAndCharacterOfPosition ( currentSourceFile , skipTrivia ( currentSourceFile . text , node . pos ) ) ;
3121- var firstCharOfExpr = getFirstNonWhitespaceCharacterIndexOnLine ( exprStart . line ) ;
3122- var shouldIndent = rightStart . character > firstCharOfExpr ;
3123-
3124- if ( shouldIndent ) {
3125- increaseIndent ( ) ;
3126- }
3127-
3093+ if ( ! nodeEndIsOnSameLineAsNodeStart ( node . operatorToken , node . right ) ) {
3094+ increaseIndent ( ) ;
31283095 writeLine ( ) ;
3096+ emit ( node . right ) ;
3097+ decreaseIndent ( ) ;
31293098 }
31303099 else {
31313100 write ( " " ) ;
3132- }
3133-
3134- emit ( node . right ) ;
3135-
3136- if ( shouldIndent ) {
3137- decreaseIndent ( ) ;
3101+ emit ( node . right ) ;
31383102 }
31393103 }
31403104 }
31413105
3142- function getFirstNonWhitespaceCharacterIndexOnLine ( line : number ) : number {
3143- var lineStart = getLineStarts ( currentSourceFile ) [ line ] ;
3144- var text = currentSourceFile . text ;
3145-
3146- for ( var i = lineStart ; i < text . length ; i ++ ) {
3147- var ch = text . charCodeAt ( i ) ;
3148- if ( ! isWhiteSpace ( text . charCodeAt ( i ) ) || isLineBreak ( ch ) ) {
3149- break ;
3150- }
3151- }
3152-
3153- return i - lineStart ;
3154- }
3155-
31563106 function emitConditionalExpression ( node : ConditionalExpression ) {
31573107 emit ( node . condition ) ;
31583108 write ( " ? " ) ;
@@ -3992,58 +3942,21 @@ module ts {
39923942 }
39933943
39943944 function emitBlockFunctionBody ( node : FunctionLikeDeclaration , body : Block ) {
3995- // If the body has no statements, and we know there's no code that would cause any
3996- // prologue to be emitted, then just do a simple emit if the empty block.
3997- if ( body . statements . length === 0 && ! anyParameterHasBindingPatternOrInitializer ( node ) ) {
3998- emitFunctionBodyWithNoStatements ( node , body ) ;
3999- }
4000- else {
4001- emitFunctionBodyWithStatements ( node , body ) ;
4002- }
4003- }
4004-
4005- function anyParameterHasBindingPatternOrInitializer ( func : FunctionLikeDeclaration ) {
4006- return forEach ( func . parameters , hasBindingPatternOrInitializer ) ;
4007- }
4008-
4009- function hasBindingPatternOrInitializer ( parameter : ParameterDeclaration ) {
4010- return parameter . initializer || isBindingPattern ( parameter . name ) ;
4011- }
4012-
4013- function emitFunctionBodyWithNoStatements ( node : FunctionLikeDeclaration , body : Block ) {
4014- var singleLine = isSingleLineEmptyBlock ( node . body ) ;
4015-
4016- write ( " {" ) ;
4017- if ( singleLine ) {
4018- write ( " " ) ;
4019- }
4020- else {
4021- increaseIndent ( ) ;
4022- writeLine ( ) ;
4023- }
4024-
4025- emitLeadingCommentsOfPosition ( body . statements . end ) ;
4026-
4027- if ( ! singleLine ) {
4028- decreaseIndent ( ) ;
4029- }
4030-
4031- emitToken ( SyntaxKind . CloseBraceToken , body . statements . end ) ;
4032- }
4033-
4034- function emitFunctionBodyWithStatements ( node : FunctionLikeDeclaration , body : Block ) {
40353945 write ( " {" ) ;
40363946 scopeEmitStart ( node ) ;
40373947
4038- var outPos = writer . getTextPos ( ) ;
3948+ var initialTextPos = writer . getTextPos ( ) ;
40393949
40403950 increaseIndent ( ) ;
40413951 emitDetachedComments ( body . statements ) ;
3952+
3953+ // Emit all the directive prologues (like "use strict"). These have to come before
3954+ // any other preamble code we write (like parameter initializers).
40423955 var startIndex = emitDirectivePrologues ( body . statements , /*startWithNewLine*/ true ) ;
40433956 emitFunctionBodyPreamble ( node ) ;
40443957 decreaseIndent ( ) ;
40453958
4046- var preambleEmitted = writer . getTextPos ( ) !== outPos ;
3959+ var preambleEmitted = writer . getTextPos ( ) !== initialTextPos ;
40473960
40483961 if ( ! preambleEmitted && nodeEndIsOnSameLineAsNodeStart ( body , body ) ) {
40493962 for ( var i = 0 , n = body . statements . length ; i < n ; i ++ ) {
0 commit comments