avoid exceptions for control flow (#9974)

* avoid exceptions for control flow

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

* drop tsTryParseTypeArguments method
This commit is contained in:
Matthew Robertson 2019-05-21 08:23:53 -07:00 committed by Nicolò Ribaudo
parent f5b8140580
commit 9c06e4ed4d
4 changed files with 378 additions and 6 deletions

View File

@ -1442,6 +1442,9 @@ export default (superClass: Class<Parser>): Class<Parser> =>
startPos: number, startPos: number,
startLoc: Position, startLoc: Position,
): ?N.ArrowFunctionExpression { ): ?N.ArrowFunctionExpression {
if (!this.isRelational("<")) {
return undefined;
}
const res: ?N.ArrowFunctionExpression = this.tsTryParseAndCatch(() => { const res: ?N.ArrowFunctionExpression = this.tsTryParseAndCatch(() => {
const node: N.ArrowFunctionExpression = this.startNodeAt( const node: N.ArrowFunctionExpression = this.startNodeAt(
startPos, startPos,
@ -2204,8 +2207,10 @@ export default (superClass: Class<Parser>): Class<Parser> =>
const returnType = this.tsParseTypeOrTypePredicateAnnotation( const returnType = this.tsParseTypeOrTypePredicateAnnotation(
tt.colon, tt.colon,
); );
if (this.canInsertSemicolon()) this.unexpected(); if (this.canInsertSemicolon() || !this.match(tt.arrow)) {
if (!this.match(tt.arrow)) this.unexpected(); this.state = state;
return undefined;
}
node.returnType = returnType; node.returnType = returnType;
} catch (err) { } catch (err) {
if (err instanceof SyntaxError) { if (err instanceof SyntaxError) {
@ -2438,10 +2443,12 @@ export default (superClass: Class<Parser>): Class<Parser> =>
jsxParseOpeningElementAfterName( jsxParseOpeningElementAfterName(
node: N.JSXOpeningElement, node: N.JSXOpeningElement,
): N.JSXOpeningElement { ): N.JSXOpeningElement {
const typeArguments = this.tsTryParseAndCatch(() => if (this.isRelational("<")) {
this.tsParseTypeArguments(), const typeArguments = this.tsTryParseAndCatch(() =>
); this.tsParseTypeArguments(),
if (typeArguments) node.typeParameters = typeArguments; );
if (typeArguments) node.typeParameters = typeArguments;
}
return super.jsxParseOpeningElementAfterName(node); return super.jsxParseOpeningElementAfterName(node);
} }

View File

@ -0,0 +1,3 @@
for (let i = 0; i < require('foo').bar; i++) {
x(i);
}

View File

@ -0,0 +1,4 @@
{
"sourceType": "module",
"plugins": ["typescript", "jsx"]
}

View File

@ -0,0 +1,358 @@
{
"type": "File",
"start": 0,
"end": 58,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 58,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ForStatement",
"start": 0,
"end": 58,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"init": {
"type": "VariableDeclaration",
"start": 5,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 14
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 9,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 14
}
},
"id": {
"type": "Identifier",
"start": 9,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 10
},
"identifierName": "i"
},
"name": "i"
},
"init": {
"type": "NumericLiteral",
"start": 13,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 13
},
"end": {
"line": 1,
"column": 14
}
},
"extra": {
"rawValue": 0,
"raw": "0"
},
"value": 0
}
}
],
"kind": "let"
},
"test": {
"type": "BinaryExpression",
"start": 16,
"end": 38,
"loc": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 1,
"column": 38
}
},
"left": {
"type": "Identifier",
"start": 16,
"end": 17,
"loc": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 1,
"column": 17
},
"identifierName": "i"
},
"name": "i"
},
"operator": "<",
"right": {
"type": "MemberExpression",
"start": 20,
"end": 38,
"loc": {
"start": {
"line": 1,
"column": 20
},
"end": {
"line": 1,
"column": 38
}
},
"object": {
"type": "CallExpression",
"start": 20,
"end": 34,
"loc": {
"start": {
"line": 1,
"column": 20
},
"end": {
"line": 1,
"column": 34
}
},
"callee": {
"type": "Identifier",
"start": 20,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 20
},
"end": {
"line": 1,
"column": 27
},
"identifierName": "require"
},
"name": "require"
},
"arguments": [
{
"type": "StringLiteral",
"start": 28,
"end": 33,
"loc": {
"start": {
"line": 1,
"column": 28
},
"end": {
"line": 1,
"column": 33
}
},
"extra": {
"rawValue": "foo",
"raw": "'foo'"
},
"value": "foo"
}
]
},
"property": {
"type": "Identifier",
"start": 35,
"end": 38,
"loc": {
"start": {
"line": 1,
"column": 35
},
"end": {
"line": 1,
"column": 38
},
"identifierName": "bar"
},
"name": "bar"
},
"computed": false
}
},
"update": {
"type": "UpdateExpression",
"start": 40,
"end": 43,
"loc": {
"start": {
"line": 1,
"column": 40
},
"end": {
"line": 1,
"column": 43
}
},
"operator": "++",
"prefix": false,
"argument": {
"type": "Identifier",
"start": 40,
"end": 41,
"loc": {
"start": {
"line": 1,
"column": 40
},
"end": {
"line": 1,
"column": 41
},
"identifierName": "i"
},
"name": "i"
}
},
"body": {
"type": "BlockStatement",
"start": 45,
"end": 58,
"loc": {
"start": {
"line": 1,
"column": 45
},
"end": {
"line": 3,
"column": 1
}
},
"body": [
{
"type": "ExpressionStatement",
"start": 51,
"end": 56,
"loc": {
"start": {
"line": 2,
"column": 4
},
"end": {
"line": 2,
"column": 9
}
},
"expression": {
"type": "CallExpression",
"start": 51,
"end": 55,
"loc": {
"start": {
"line": 2,
"column": 4
},
"end": {
"line": 2,
"column": 8
}
},
"callee": {
"type": "Identifier",
"start": 51,
"end": 52,
"loc": {
"start": {
"line": 2,
"column": 4
},
"end": {
"line": 2,
"column": 5
},
"identifierName": "x"
},
"name": "x"
},
"arguments": [
{
"type": "Identifier",
"start": 53,
"end": 54,
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 7
},
"identifierName": "i"
},
"name": "i"
}
]
}
}
],
"directives": []
}
}
],
"directives": []
}
}