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 2019-09-06 to 2019-12-22 #487

Merged
merged 1 commit into from
Dec 25, 2019
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
1 change: 1 addition & 0 deletions generator/generateReadWordTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const KEYWORDS = [
const CONTEXTUAL_KEYWORDS = [
"abstract",
"as",
"asserts",
"async",
"await",
"checks",
Expand Down
35 changes: 27 additions & 8 deletions src/parser/plugins/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,9 +575,9 @@ function tsIsUnambiguouslyStartOfFunctionType(): boolean {
function tsParseTypeOrTypePredicateAnnotation(returnToken: TokenType): void {
const oldIsType = pushTypeContext(0);
expect(returnToken);
tsParseTypePredicatePrefix();
// Regardless of whether we found an "is" token, there's now just a regular type in front of
// us.
tsParseTypePredicateOrAssertsPrefix();
// Regardless of whether we found an "asserts" or "is" token, there's now just a regular type in
// front of us.
tsParseType();
popTypeContext(oldIsType);
}
Expand All @@ -600,13 +600,32 @@ function tsTryParseType(): void {
}
}

function tsParseTypePredicatePrefix(): void {
function tsParseTypePredicateOrAssertsPrefix(): void {
const snapshot = state.snapshot();
parseIdentifier();
if (isContextual(ContextualKeyword._is) && !hasPrecedingLineBreak()) {
if (isContextual(ContextualKeyword._asserts) && !hasPrecedingLineBreak()) {
// 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();
} else {
state.restoreFromSnapshot(snapshot);
if (isContextual(ContextualKeyword._is)) {
// If we see `asserts is`, then this must be of the form `asserts is T`, since
// `asserts is is T` isn't valid.
next();
} else if (tsIsIdentifier() || match(tt._this)) {
next();
expectContextual(ContextualKeyword._is);
} else {
// Regular type, so bail out and start type parsing from scratch.
state.restoreFromSnapshot(snapshot);
}
} else if (tsIsIdentifier() || match(tt._this)) {
// This is a regular identifier, which may or may not have "is" after it.
next();
if (isContextual(ContextualKeyword._is) && !hasPrecedingLineBreak()) {
next();
} else {
// Regular type, so bail out and start type parsing from scratch.
state.restoreFromSnapshot(snapshot);
}
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/parser/tokenizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,7 @@ function readToken_dot(): void {
return;
}

const next2 = input.charCodeAt(state.pos + 2);
if (nextChar === charCodes.dot && next2 === charCodes.dot) {
if (nextChar === charCodes.dot && input.charCodeAt(state.pos + 2) === charCodes.dot) {
state.pos += 3;
finishToken(tt.ellipsis);
} else {
Expand Down
1 change: 1 addition & 0 deletions src/parser/tokenizer/keywords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export enum ContextualKeyword {
NONE,
_abstract,
_as,
_asserts,
_async,
_await,
_checks,
Expand Down
458 changes: 234 additions & 224 deletions src/parser/tokenizer/readWordTree.ts

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/parser/traverser/statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,7 @@ function parseClassMemberWithIsStatic(

// The so-called parsed name would have been "async": get the real name.
parseClassPropertyName(classContextId);
parsePostMemberNameModifiers();
parseClassMethod(memberStart, false /* isConstructor */);
} else if (
(token.contextualKeyword === ContextualKeyword._get ||
Expand Down
14 changes: 14 additions & 0 deletions test/sucrase-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -859,4 +859,18 @@ describe("sucrase", () => {
{transforms: []},
);
});

it("handles comments after trailing comma after elision", () => {
assertResult(
`
function foo([foo, /* not used */, /* not used */]) {
}
`,
`
function foo([foo, /* not used */, /* not used */]) {
}
`,
{transforms: []},
);
});
});
133 changes: 133 additions & 0 deletions test/typescript-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,25 @@ describe("typescript transform", () => {
);
});

it("handles type predicates involving this", () => {
assertTypeScriptResult(
`
class A {
foo(): this is B {
return false;
}
}
`,
`"use strict";
class A {
foo() {
return false;
}
}
`,
);
});

it("export default functions with type parameters", () => {
assertTypeScriptResult(
`
Expand Down Expand Up @@ -1797,4 +1816,118 @@ describe("typescript transform", () => {
`,
);
});

it("allows private field syntax", () => {
assertTypeScriptResult(
`
class Foo {
readonly #x: number;
}
`,
`"use strict";
class Foo {

}
`,
);
});

it("allows assertion signature syntax", () => {
assertTypeScriptResult(
`
function assertIsDefined<T>(x: T): asserts x is NonNullable<T> {
if (x == null) throw "oh no";
}
`,
`"use strict";
function assertIsDefined(x) {
if (x == null) throw "oh no";
}
`,
);
});

it("allows assertion signature syntax using this", () => {
assertTypeScriptResult(
`
class Foo {
isBar(): asserts this is Foo {}
isBaz = (): asserts this is Foo => {}
}
`,
`"use strict";
class Foo {constructor() { Foo.prototype.__init.call(this); }
isBar() {}
__init() {this.isBaz = () => {}}
}
`,
);
});

it("does not get confused by a user-defined type guard on a variable called asserts", () => {
assertTypeScriptResult(
`
function checkIsDefined(asserts: any): asserts is NonNullable<T> {
return false;
}
`,
`"use strict";
function checkIsDefined(asserts) {
return false;
}
`,
);
});

it("does not get confused by a return type called asserts", () => {
assertTypeScriptResult(
`
function checkIsDefined(x: any): asserts {
return false;
}
`,
`"use strict";
function checkIsDefined(x) {
return false;
}
`,
);
});

it("correctly parses optional chain calls with type arguments", () => {
assertTypeScriptResult(
`
example.inner?.greet<string>()
`,
`"use strict";
example.inner?.greet()
`,
);
});

it("allows optional async methods", () => {
assertTypeScriptResult(
`
class A extends B {
async method?(val: string): Promise<void>;
}
`,
`"use strict";
class A extends B {

}
`,
);
});

it("handles trailing commas at the end of tuple type with rest", () => {
assertTypeScriptResult(
`
let x: [string, ...string[],]
`,
`"use strict";
let x
`,
);
});
});
2 changes: 1 addition & 1 deletion website/src/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ interface TransformInfo {

export const TRANSFORMS: Array<TransformInfo> = [
{name: "jsx", presetName: ["react", {development: true}]},
{name: "typescript", presetName: "typescript"},
{name: "typescript", presetName: ["typescript", {allowDeclareFields: true}]},
{name: "flow", presetName: "flow"},
{name: "imports", babelName: "transform-modules-commonjs"},
{name: "react-hot-loader", babelName: "react-hot-loader"},
Expand Down