Skip to content

Commit

Permalink
Fix regression causing incomplete nullish coalesce in some cases (#610)
Browse files Browse the repository at this point in the history
Fixes #609

The original implementation of the Jest transform (#540) needed to remove
regions of code, but ran into issues with the optional chaining and nullish
coalescing transforms, since those transforms would still emit function calls
around tokens even if they were removed. The implementation fixed those issues
by disabling the optional chaining and nullish coalescing code emit in
`removeToken` and `removeInitialToken`. Unfortunately, this broke other cases,
like a nullish coalescing call immediately followed by a type token. The nullish
coalescing implementation expected `appendTokenSuffix` to be called on the last
token even though it was a type token.

The changes to `TokenProcessor` actually became unnecessary as of #608 since we
no longer are deleting a region of code, so I reverted the two methods back to
their original implementation, which fixes the issue.
  • Loading branch information
alangpierce authored Apr 12, 2021
1 parent de00b66 commit beabcc5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/TokenProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,11 @@ export default class TokenProcessor {
}

removeInitialToken(): void {
this.resultCode += this.previousWhitespaceAndComments();
this.tokenIndex++;
this.replaceToken("");
}

removeToken(): void {
this.resultCode += this.previousWhitespaceAndComments().replace(/[^\r\n]/g, "");
this.tokenIndex++;
this.replaceTokenTrimmingLeftWhitespace("");
}

copyExpectedToken(tokenType: TokenType): void {
Expand Down
26 changes: 26 additions & 0 deletions test/sucrase-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,32 @@ describe("sucrase", () => {
);
});

it("does not crash with nullish coalescing followed by TS `as`", () => {
assertOutput(
`
setOutput(1 ?? 2 as any);
`,
1,
{transforms: ["typescript"]},
);
});

it("correctly transforms nullish coalescing followed by TS `as`", () => {
assertResult(
`
const foo = {
bar: baz ?? null as any
}
`,
`${NULLISH_COALESCE_PREFIX}
const foo = {
bar: _nullishCoalesce(baz, () => ( null ))
}
`,
{transforms: ["typescript"]},
);
});

it("correctly transforms async functions using await in an optional chain", () => {
assertResult(
`
Expand Down

0 comments on commit beabcc5

Please sign in to comment.