From 8dbbf1174459305c2c85f59fc952c77d52935ddc Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Wed, 14 Aug 2019 16:30:20 -0700 Subject: [PATCH 1/2] Fix semicolon-omitting writer --- src/compiler/checker.ts | 2 +- src/compiler/emitter.ts | 2 +- src/compiler/utilities.ts | 20 ++++++++++++++++++- src/services/textChanges.ts | 2 +- ...ToEs6Class_objectLiteralInArrowFunction.ts | 2 +- tests/cases/fourslash/extract-method25.ts | 2 +- 6 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 9d6fa60306c03..0bcc8cf699fef 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -3506,7 +3506,7 @@ namespace ts { const sig = nodeBuilder.signatureToSignatureDeclaration(signature, sigOutput, enclosingDeclaration, toNodeBuilderFlags(flags) | NodeBuilderFlags.IgnoreErrors | NodeBuilderFlags.WriteTypeParametersInQualifiedName); const printer = createPrinter({ removeComments: true, omitTrailingSemicolon: true }); const sourceFile = enclosingDeclaration && getSourceFileOfNode(enclosingDeclaration); - printer.writeNode(EmitHint.Unspecified, sig!, /*sourceFile*/ sourceFile, getTrailingSemicolonOmittingWriter(writer)); // TODO: GH#18217 + printer.writeNode(EmitHint.Unspecified, sig!, /*sourceFile*/ sourceFile, getTrailingSemicolonDeferringWriter(writer)); // TODO: GH#18217 return writer; } } diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index a70973825d1a0..fbdf000cf240a 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -1056,7 +1056,7 @@ namespace ts { function setWriter(_writer: EmitTextWriter | undefined, _sourceMapGenerator: SourceMapGenerator | undefined) { if (_writer && printerOptions.omitTrailingSemicolon) { - _writer = getTrailingSemicolonOmittingWriter(_writer); + _writer = getTrailingSemicolonDeferringWriter(_writer); } writer = _writer!; // TODO: GH#18217 diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 0bb3c799af5fd..806c533792408 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -3369,7 +3369,11 @@ namespace ts { }; } - export function getTrailingSemicolonOmittingWriter(writer: EmitTextWriter): EmitTextWriter { + export interface TrailingSemicolonDeferringWriter extends EmitTextWriter { + resetPendingTrailingSemicolon(): void; + } + + export function getTrailingSemicolonDeferringWriter(writer: EmitTextWriter): TrailingSemicolonDeferringWriter { let pendingTrailingSemicolon = false; function commitPendingTrailingSemicolon() { @@ -3435,10 +3439,24 @@ namespace ts { decreaseIndent() { commitPendingTrailingSemicolon(); writer.decreaseIndent(); + }, + resetPendingTrailingSemicolon() { + pendingTrailingSemicolon = false; } }; } + export function getTrailingSemicolonOmittingWriter(writer: EmitTextWriter): EmitTextWriter { + const deferringWriter = getTrailingSemicolonDeferringWriter(writer); + return { + ...deferringWriter, + writeLine() { + deferringWriter.resetPendingTrailingSemicolon(); + writer.writeLine(); + }, + }; + } + export function getResolvedExternalModuleName(host: EmitHost, file: SourceFile, referenceFile?: SourceFile): string { return file.moduleName || getExternalModuleNameFromPath(host, file.fileName, referenceFile && referenceFile.fileName); } diff --git a/src/services/textChanges.ts b/src/services/textChanges.ts index f3e5bf9bfb57d..31c9f9f26717b 100644 --- a/src/services/textChanges.ts +++ b/src/services/textChanges.ts @@ -854,7 +854,7 @@ namespace ts.textChanges { const omitTrailingSemicolon = !!sourceFile && !probablyUsesSemicolons(sourceFile); const writer = createWriter(newLineCharacter, omitTrailingSemicolon); const newLine = newLineCharacter === "\n" ? NewLineKind.LineFeed : NewLineKind.CarriageReturnLineFeed; - createPrinter({ newLine, neverAsciiEscape: true, omitTrailingSemicolon }, writer).writeNode(EmitHint.Unspecified, node, sourceFile, writer); + createPrinter({ newLine, neverAsciiEscape: true }, writer).writeNode(EmitHint.Unspecified, node, sourceFile, writer); return { text: writer.getText(), node: assignPositionsToNode(node) }; } } diff --git a/tests/cases/fourslash/convertFunctionToEs6Class_objectLiteralInArrowFunction.ts b/tests/cases/fourslash/convertFunctionToEs6Class_objectLiteralInArrowFunction.ts index a86af1e6d3d29..93bbd707dde14 100644 --- a/tests/cases/fourslash/convertFunctionToEs6Class_objectLiteralInArrowFunction.ts +++ b/tests/cases/fourslash/convertFunctionToEs6Class_objectLiteralInArrowFunction.ts @@ -15,7 +15,7 @@ verify.codeFix({ constructor() { } foo() { - ({ bar: () => { } }); + ({ bar: () => { } }) } } `, diff --git a/tests/cases/fourslash/extract-method25.ts b/tests/cases/fourslash/extract-method25.ts index a06262ca47510..3aecc128e6efc 100644 --- a/tests/cases/fourslash/extract-method25.ts +++ b/tests/cases/fourslash/extract-method25.ts @@ -18,7 +18,7 @@ edit.applyRefactor({ q[0]++ function newFunction() { - return [0]; + return [0] } }` }); From 62bfb060e757b5494248b29641d65567aee49e18 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Wed, 28 Aug 2019 13:36:44 -0700 Subject: [PATCH 2/2] Use writeTrailingSemicolon for do statements --- src/compiler/emitter.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index fbdf000cf240a..5766eba18668f 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -2514,7 +2514,7 @@ namespace ts { } emitWhileClause(node, node.statement.end); - writePunctuation(";"); + writeTrailingSemicolon(); } function emitWhileStatement(node: WhileStatement) {