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

avoid exceptions for control flow #9974

Merged
merged 3 commits into from May 21, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 13 additions & 6 deletions packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -1442,6 +1442,9 @@ export default (superClass: Class<Parser>): Class<Parser> =>
startPos: number,
startLoc: Position,
): ?N.ArrowFunctionExpression {
if (!this.isRelational("<")) {
return undefined;
}
const res: ?N.ArrowFunctionExpression = this.tsTryParseAndCatch(() => {
const node: N.ArrowFunctionExpression = this.startNodeAt(
startPos,
Expand Down Expand Up @@ -2204,8 +2207,10 @@ export default (superClass: Class<Parser>): Class<Parser> =>
const returnType = this.tsParseTypeOrTypePredicateAnnotation(
tt.colon,
);
if (this.canInsertSemicolon()) this.unexpected();
if (!this.match(tt.arrow)) this.unexpected();
if (this.canInsertSemicolon() || !this.match(tt.arrow)) {
this.state = state;
return undefined;
}
node.returnType = returnType;
} catch (err) {
if (err instanceof SyntaxError) {
Expand Down Expand Up @@ -2438,10 +2443,12 @@ export default (superClass: Class<Parser>): Class<Parser> =>
jsxParseOpeningElementAfterName(
node: N.JSXOpeningElement,
): N.JSXOpeningElement {
const typeArguments = this.tsTryParseAndCatch(() =>
this.tsParseTypeArguments(),
);
if (typeArguments) node.typeParameters = typeArguments;
if (this.isRelational("<")) {
const typeArguments = this.tsTryParseAndCatch(() =>
this.tsParseTypeArguments(),
);
if (typeArguments) node.typeParameters = typeArguments;
}
return super.jsxParseOpeningElementAfterName(node);
}

Expand Down
@@ -0,0 +1,3 @@
for (let i = 0; i < require('foo').bar; i++) {
x(i);
}
@@ -0,0 +1,4 @@
{
"sourceType": "module",
"plugins": ["typescript", "jsx"]
}