Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ugly emitted comments when removing code between tokens #286

Merged
merged 1 commit into from
Jun 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/TokenProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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++;
}
Expand All @@ -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,
Expand Down Expand Up @@ -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;
}

Expand Down
16 changes: 16 additions & 0 deletions test/sucrase-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"]},
);
});
});