Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
avoid exceptions for control flow (#9974)
* avoid exceptions for control flow

* review feedback: remove conditional because we know it is "<"

* drop tsTryParseTypeArguments method
  • Loading branch information
matthewrobertson authored and nicolo-ribaudo committed May 21, 2019
1 parent f5b8140 commit 9c06e4e
Show file tree
Hide file tree
Showing 4 changed files with 378 additions and 6 deletions.
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"]
}

0 comments on commit 9c06e4e

Please sign in to comment.