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

Port babel-parser changes from 2021-08-20 to 2021-12-30 #714

Merged
merged 1 commit into from
Jul 1, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/parser/plugins/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ function tsTryParseType(): void {
*/
function tsParseTypePredicateOrAssertsPrefix(): boolean {
const snapshot = state.snapshot();
if (isContextual(ContextualKeyword._asserts) && !hasPrecedingLineBreak()) {
if (isContextual(ContextualKeyword._asserts)) {
// Normally this is `asserts x is T`, but at this point, it might be `asserts is T` (a user-
// defined type guard on the `asserts` variable) or just a type called `asserts`.
next();
Expand Down Expand Up @@ -1453,7 +1453,9 @@ export function tsStartParseFunctionParams(): void {
// `let x: number;`
export function tsAfterParseVarHead(): void {
const oldIsType = pushTypeContext(0);
eat(tt.bang);
if (!hasPrecedingLineBreak()) {
eat(tt.bang);
}
tsTryParseTypeAnnotation();
popTypeContext(oldIsType);
}
Expand Down
32 changes: 32 additions & 0 deletions test/sucrase-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1513,4 +1513,36 @@ describe("sucrase", () => {
`,
);
});

it("allows static blocks with a line break after the static keyword", () => {
assertResult(
`
class A {
static
{
console.log("hi");
}
}
`,
`"use strict";
class A {
static
{
console.log("hi");
}
}
`,
);
});

it("allows arrow functions with parameter object assignment", () => {
assertResult(
`
({x = 1}) => null
`,
`"use strict";
({x = 1}) => null
`,
);
});
});
28 changes: 28 additions & 0 deletions test/typescript-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3003,4 +3003,32 @@ describe("typescript transform", () => {
{transforms: ["typescript"]},
);
});

it("allows a line break before an `asserts` clause", () => {
assertResult(
`
function assert(condition: any):
asserts condition {}
`,
`
function assert(condition)
{}
`,
{transforms: ["typescript"]},
);
});

it("properly handles ASI for variable followed by exclamation point", () => {
assertResult(
`
let a
!function(){}()
`,
`
let a
!function(){}()
`,
{transforms: ["typescript"]},
);
});
});