From e3ef42743a99333a15d3cb357125c67aa13f2d03 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Wed, 19 Feb 2020 09:20:51 -0800 Subject: [PATCH] Restore some monomorphism --- src/compiler/emitter.ts | 35 ++++++++++++++++++++++------------- src/compiler/types.ts | 3 ++- src/compiler/utilities.ts | 19 ++++++++++++------- 3 files changed, 36 insertions(+), 21 deletions(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index ae4a3796ddf..df146d9dbbb 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -2297,7 +2297,7 @@ namespace ts { emitTokenWithComment(token.kind, node.expression.end, writePunctuation, node); writeLinesAndIndent(linesAfterDot, /*writeSpaceIfNotIndenting*/ false); emit(node.name); - decreaseIndentIf(linesBeforeDot, linesAfterDot); + decreaseIndentIf(linesBeforeDot > 0, linesAfterDot > 0); } // 1..toString is a valid property access, emit a dot after the literal @@ -2475,7 +2475,7 @@ namespace ts { case EmitBinaryExpressionState.FinishEmit: { const linesBeforeOperator = getLinesBetweenNodes(node, node.left, node.operatorToken); const linesAfterOperator = getLinesBetweenNodes(node, node.operatorToken, node.right); - decreaseIndentIf(linesBeforeOperator, linesAfterOperator); + decreaseIndentIf(linesBeforeOperator > 0, linesAfterOperator > 0); stackIndex--; break; } @@ -2529,13 +2529,13 @@ namespace ts { emit(node.questionToken); writeLinesAndIndent(linesAfterQuestion, /*writeSpaceIfNotIndenting*/ true); emitExpression(node.whenTrue); - decreaseIndentIf(linesBeforeQuestion, linesAfterQuestion); + decreaseIndentIf(linesBeforeQuestion > 0, linesAfterQuestion > 0); writeLinesAndIndent(linesBeforeColon, /*writeSpaceIfNotIndenting*/ true); emit(node.colonToken); writeLinesAndIndent(linesAfterColon, /*writeSpaceIfNotIndenting*/ true); emitExpression(node.whenFalse); - decreaseIndentIf(linesBeforeColon, linesAfterColon); + decreaseIndentIf(linesBeforeColon > 0, linesAfterColon > 0); } function emitTemplateExpression(node: TemplateExpression) { @@ -4010,7 +4010,7 @@ namespace ts { let shouldEmitInterveningComments = mayEmitInterveningComments; const leadingLineTerminatorCount = getLeadingLineTerminatorCount(parentNode, children!, format); // TODO: GH#18217 if (leadingLineTerminatorCount) { - writeLine(leadingLineTerminatorCount); + writeBlankLines(leadingLineTerminatorCount); shouldEmitInterveningComments = false; } else if (format & ListFormat.SpaceBetweenBraces) { @@ -4058,7 +4058,7 @@ namespace ts { shouldDecreaseIndentAfterEmit = true; } - writeLine(separatingLineTerminatorCount); + writeBlankLines(separatingLineTerminatorCount); shouldEmitInterveningComments = false; } else if (previousSibling && format & ListFormat.SpaceBetweenSiblings) { @@ -4115,7 +4115,7 @@ namespace ts { // Write the closing line terminator or closing whitespace. const closingLineTerminatorCount = getClosingLineTerminatorCount(parentNode, children!, format); if (closingLineTerminatorCount) { - writeLine(closingLineTerminatorCount); + writeBlankLines(closingLineTerminatorCount); } else if (format & ListFormat.SpaceBetweenBraces) { writeSpace(); @@ -4185,10 +4185,8 @@ namespace ts { writer.writeProperty(s); } - function writeLine(count = 1) { - for (let i = 0; i < count; i++) { - writer.writeLine(i > 0); - } + function writeLine() { + writer.writeLine(); } function increaseIndent() { @@ -4244,10 +4242,21 @@ namespace ts { } } + function writeBlankLines(lineCount: number) { + for (let i = 0; i < lineCount; i++) { + if (i === 0) { + writer.writeLine(); + } + else { + writer.forceWriteLine(); + } + } + } + function writeLinesAndIndent(lineCount: number, writeSpaceIfNotIndenting: boolean) { if (lineCount) { increaseIndent(); - writeLine(lineCount); + writeBlankLines(lineCount); } else if (writeSpaceIfNotIndenting) { writeSpace(); @@ -4258,7 +4267,7 @@ namespace ts { // previous indent values to be considered at a time. This also allows caller to just // call this once, passing in all their appropriate indent values, instead of needing // to call this helper function multiple times. - function decreaseIndentIf(value1: boolean | number, value2: boolean | number) { + function decreaseIndentIf(value1: boolean, value2: boolean) { if (value1) { decreaseIndent(); } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index a6a16eebeac..d988b77ced0 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -3753,7 +3753,7 @@ namespace ts { writeParameter(text: string): void; writeProperty(text: string): void; writeSymbol(text: string, symbol: Symbol): void; - writeLine(force?: boolean): void; + writeLine(): void; increaseIndent(): void; decreaseIndent(): void; clear(): void; @@ -6328,6 +6328,7 @@ namespace ts { getText(): string; rawWrite(s: string): void; writeLiteral(s: string): void; + forceWriteLine(): void; getTextPos(): number; getLine(): number; getColumn(): number; diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index dc9dc0db524..5242711efea 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -3569,13 +3569,17 @@ namespace ts { } } - function writeLine(force?: boolean) { - if (!lineStart || force) { - output += newLine; - lineCount++; - linePos = output.length; - lineStart = true; - hasTrailingComment = false; + function forceWriteLine() { + output += newLine; + lineCount++; + linePos = output.length; + lineStart = true; + hasTrailingComment = false; + } + + function writeLine() { + if (!lineStart) { + forceWriteLine(); } } @@ -3590,6 +3594,7 @@ namespace ts { rawWrite, writeLiteral, writeLine, + forceWriteLine, increaseIndent: () => { indent++; }, decreaseIndent: () => { indent--; }, getIndent: () => indent,