@@ -3160,71 +3160,21 @@ module ts {
31603160
31613161 write ( tokenToString ( node . operatorToken . kind ) ) ;
31623162
3163- // We'd like to preserve newlines found in the original binary expression. i.e. if a user has:
3164- //
3165- // Foo() ||
3166- // Bar();
3167- //
3168- // Then we'd like to emit it as such. It seems like we'd only need to check for a newline and
3169- // then just indent and emit. However, that will lead to a problem with deeply nested code.
3170- // i.e. if you have:
3171- //
3172- // Foo() ||
3173- // Bar() ||
3174- // Baz();
3175- //
3176- // Then we don't want to emit it as:
3177- //
3178- // Foo() ||
3179- // Bar() ||
3180- // Baz();
3181- //
3182- // So we only indent if the right side of the binary expression starts further in on the line
3183- // versus the left.
3184- var operatorEnd = getLineAndCharacterOfPosition ( currentSourceFile , node . operatorToken . end ) ;
3185- var rightStart = getLineAndCharacterOfPosition ( currentSourceFile , skipTrivia ( currentSourceFile . text , node . right . pos ) ) ;
3186-
31873163 // Check if the right expression is on a different line versus the operator itself. If so,
31883164 // we'll emit newline.
3189- var onDifferentLine = operatorEnd . line !== rightStart . line ;
3190- if ( onDifferentLine ) {
3191- // Also, if the right expression starts further in on the line than the left, then we'll indent.
3192- var exprStart = getLineAndCharacterOfPosition ( currentSourceFile , skipTrivia ( currentSourceFile . text , node . pos ) ) ;
3193- var firstCharOfExpr = getFirstNonWhitespaceCharacterIndexOnLine ( exprStart . line ) ;
3194- var shouldIndent = rightStart . character > firstCharOfExpr ;
3195-
3196- if ( shouldIndent ) {
3197- increaseIndent ( ) ;
3198- }
3199-
3165+ if ( ! nodeEndIsOnSameLineAsNodeStart ( node . operatorToken , node . right ) ) {
3166+ increaseIndent ( ) ;
32003167 writeLine ( ) ;
3168+ emit ( node . right ) ;
3169+ decreaseIndent ( ) ;
32013170 }
32023171 else {
32033172 write ( " " ) ;
3204- }
3205-
3206- emit ( node . right ) ;
3207-
3208- if ( shouldIndent ) {
3209- decreaseIndent ( ) ;
3173+ emit ( node . right ) ;
32103174 }
32113175 }
32123176 }
32133177
3214- function getFirstNonWhitespaceCharacterIndexOnLine ( line : number ) : number {
3215- var lineStart = getLineStarts ( currentSourceFile ) [ line ] ;
3216- var text = currentSourceFile . text ;
3217-
3218- for ( var i = lineStart ; i < text . length ; i ++ ) {
3219- var ch = text . charCodeAt ( i ) ;
3220- if ( ! isWhiteSpace ( text . charCodeAt ( i ) ) || isLineBreak ( ch ) ) {
3221- break ;
3222- }
3223- }
3224-
3225- return i - lineStart ;
3226- }
3227-
32283178 function emitConditionalExpression ( node : ConditionalExpression ) {
32293179 emit ( node . condition ) ;
32303180 write ( " ? " ) ;
@@ -4064,58 +4014,21 @@ module ts {
40644014 }
40654015
40664016 function emitBlockFunctionBody ( node : FunctionLikeDeclaration , body : Block ) {
4067- // If the body has no statements, and we know there's no code that would cause any
4068- // prologue to be emitted, then just do a simple emit if the empty block.
4069- if ( body . statements . length === 0 && ! anyParameterHasBindingPatternOrInitializer ( node ) ) {
4070- emitFunctionBodyWithNoStatements ( node , body ) ;
4071- }
4072- else {
4073- emitFunctionBodyWithStatements ( node , body ) ;
4074- }
4075- }
4076-
4077- function anyParameterHasBindingPatternOrInitializer ( func : FunctionLikeDeclaration ) {
4078- return forEach ( func . parameters , hasBindingPatternOrInitializer ) ;
4079- }
4080-
4081- function hasBindingPatternOrInitializer ( parameter : ParameterDeclaration ) {
4082- return parameter . initializer || isBindingPattern ( parameter . name ) ;
4083- }
4084-
4085- function emitFunctionBodyWithNoStatements ( node : FunctionLikeDeclaration , body : Block ) {
4086- var singleLine = isSingleLineEmptyBlock ( node . body ) ;
4087-
4088- write ( " {" ) ;
4089- if ( singleLine ) {
4090- write ( " " ) ;
4091- }
4092- else {
4093- increaseIndent ( ) ;
4094- writeLine ( ) ;
4095- }
4096-
4097- emitLeadingCommentsOfPosition ( body . statements . end ) ;
4098-
4099- if ( ! singleLine ) {
4100- decreaseIndent ( ) ;
4101- }
4102-
4103- emitToken ( SyntaxKind . CloseBraceToken , body . statements . end ) ;
4104- }
4105-
4106- function emitFunctionBodyWithStatements ( node : FunctionLikeDeclaration , body : Block ) {
41074017 write ( " {" ) ;
41084018 scopeEmitStart ( node ) ;
41094019
4110- var outPos = writer . getTextPos ( ) ;
4020+ var initialTextPos = writer . getTextPos ( ) ;
41114021
41124022 increaseIndent ( ) ;
41134023 emitDetachedComments ( body . statements ) ;
4024+
4025+ // Emit all the directive prologues (like "use strict"). These have to come before
4026+ // any other preamble code we write (like parameter initializers).
41144027 var startIndex = emitDirectivePrologues ( body . statements , /*startWithNewLine*/ true ) ;
41154028 emitFunctionBodyPreamble ( node ) ;
41164029 decreaseIndent ( ) ;
41174030
4118- var preambleEmitted = writer . getTextPos ( ) !== outPos ;
4031+ var preambleEmitted = writer . getTextPos ( ) !== initialTextPos ;
41194032
41204033 if ( ! preambleEmitted && nodeEndIsOnSameLineAsNodeStart ( body , body ) ) {
41214034 for ( var i = 0 , n = body . statements . length ; i < n ; i ++ ) {
0 commit comments