From d7ab2ae858f846210fa138ceb1b6f6d834b81fc5 Mon Sep 17 00:00:00 2001 From: Alan Pierce Date: Thu, 28 Jun 2018 18:39:57 -0700 Subject: [PATCH] Fix ugly emitted comments when removing code between tokens (#286) Fixes #281 Previously, it seemed best to remove all whitespace, but that makes comments look really weird, since all spaces are removed in the comment. Instead, for now, we remove whitespace and comments in their entirety (except newlines). --- src/TokenProcessor.ts | 10 +++++----- test/sucrase-test.ts | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/TokenProcessor.ts b/src/TokenProcessor.ts index 75852b2f..21f275f3 100644 --- a/src/TokenProcessor.ts +++ b/src/TokenProcessor.ts @@ -138,7 +138,7 @@ export default class TokenProcessor { return this.matches1(type) && this.currentToken().contextId === contextId; } - previousWhitespace(): string { + previousWhitespaceAndComments(): string { let whitespaceAndComments = this.code.slice( this.tokenIndex > 0 ? this.tokens[this.tokenIndex - 1].end : 0, this.tokenIndex < this.tokens.length ? this.tokens[this.tokenIndex].start : this.code.length, @@ -150,13 +150,13 @@ export default class TokenProcessor { } replaceToken(newCode: string): void { - this.resultCode += this.previousWhitespace(); + this.resultCode += this.previousWhitespaceAndComments(); this.resultCode += newCode; this.tokenIndex++; } replaceTokenTrimmingLeftWhitespace(newCode: string): void { - this.resultCode += this.previousWhitespace().replace(/[\t ]/g, ""); + this.resultCode += this.previousWhitespaceAndComments().replace(/[^\r\n]/g, ""); this.resultCode += newCode; this.tokenIndex++; } @@ -177,7 +177,7 @@ export default class TokenProcessor { } copyToken(): void { - this.resultCode += this.previousWhitespace(); + this.resultCode += this.previousWhitespaceAndComments(); this.resultCode += this.code.slice( this.tokens[this.tokenIndex].start, this.tokens[this.tokenIndex].end, @@ -225,7 +225,7 @@ export default class TokenProcessor { if (this.tokenIndex !== this.tokens.length) { throw new Error("Tried to finish processing tokens before reaching the end."); } - this.resultCode += this.previousWhitespace(); + this.resultCode += this.previousWhitespaceAndComments(); return this.resultCode; } diff --git a/test/sucrase-test.ts b/test/sucrase-test.ts index 75f48843..bdf65b1e 100644 --- a/test/sucrase-test.ts +++ b/test/sucrase-test.ts @@ -567,4 +567,20 @@ describe("sucrase", () => { {transforms: ["imports", "typescript"]}, ); }); + + it("removes comments within removed ranges rather than removing all whitespace", () => { + assertResult( + ` + interface A { + // This is a comment. + } + `, + `"use strict"; + + + + `, + {transforms: ["imports", "typescript"]}, + ); + }); });