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..5766eba18668f 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 @@ -2514,7 +2514,7 @@ namespace ts { } emitWhileClause(node, node.statement.end); - writePunctuation(";"); + writeTrailingSemicolon(); } function emitWhileStatement(node: WhileStatement) { 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] } }` });