[ts] Disallow invalid type annotations in ExpressionStatements (#12185)
* Fix regression with invalid type annotations in ExpressionStatements
This commit is contained in:
parent
ee1f742035
commit
47250ffa65
@ -1816,25 +1816,27 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
}
|
||||
}
|
||||
|
||||
parseExprListItem(
|
||||
allowEmpty: ?boolean,
|
||||
refExpressionErrors?: ?ExpressionErrors,
|
||||
refNeedsArrowPos: ?Pos,
|
||||
allowPlaceholder: ?boolean,
|
||||
): ?N.Expression {
|
||||
const node = super.parseExprListItem(
|
||||
allowEmpty,
|
||||
refExpressionErrors,
|
||||
refNeedsArrowPos,
|
||||
allowPlaceholder,
|
||||
);
|
||||
tsCheckForInvalidTypeCasts(items: $ReadOnlyArray<?N.Expression>) {
|
||||
items.forEach(node => {
|
||||
if (node?.type === "TSTypeCastExpression") {
|
||||
this.raise(
|
||||
node.typeAnnotation.start,
|
||||
TSErrors.UnexpectedTypeAnnotation,
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Handle `func(a: T)` or `func<T>(a: T)`
|
||||
if (!refNeedsArrowPos && node?.type === "TSTypeCastExpression") {
|
||||
this.raise(node.start, TSErrors.UnexpectedTypeAnnotation);
|
||||
}
|
||||
|
||||
return node;
|
||||
toReferencedList(
|
||||
exprList: $ReadOnlyArray<?N.Expression>,
|
||||
isInParens?: boolean, // eslint-disable-line no-unused-vars
|
||||
): $ReadOnlyArray<?N.Expression> {
|
||||
// Handles invalid scenarios like: `f(a:b)`, `(a:b);`, and `(a:b,c:d)`.
|
||||
//
|
||||
// Note that `f<T>(a:b)` goes through a different path and is handled
|
||||
// in `parseSubscript` directly.
|
||||
this.tsCheckForInvalidTypeCasts(exprList);
|
||||
return exprList;
|
||||
}
|
||||
|
||||
parseSubscript(
|
||||
@ -1886,6 +1888,10 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
tt.parenR,
|
||||
/* possibleAsync */ false,
|
||||
);
|
||||
|
||||
// Handles invalid case: `f<T>(a:b)`
|
||||
this.tsCheckForInvalidTypeCasts(node.arguments);
|
||||
|
||||
node.typeParameters = typeArguments;
|
||||
return this.finishCallExpression(node, state.optionalChainMember);
|
||||
} else if (this.match(tt.backQuote)) {
|
||||
|
||||
2
packages/babel-parser/test/fixtures/typescript/cast/invalid/input.ts
vendored
Normal file
2
packages/babel-parser/test/fixtures/typescript/cast/invalid/input.ts
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
(a:b);
|
||||
(a:b,c:d);
|
||||
106
packages/babel-parser/test/fixtures/typescript/cast/invalid/output.json
vendored
Normal file
106
packages/babel-parser/test/fixtures/typescript/cast/invalid/output.json
vendored
Normal file
@ -0,0 +1,106 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":10}},
|
||||
"errors": [
|
||||
"SyntaxError: Did not expect a type annotation here. (1:2)",
|
||||
"SyntaxError: Did not expect a type annotation here. (2:2)",
|
||||
"SyntaxError: Did not expect a type annotation here. (2:6)"
|
||||
],
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":10}},
|
||||
"sourceType": "module",
|
||||
"interpreter": null,
|
||||
"body": [
|
||||
{
|
||||
"type": "ExpressionStatement",
|
||||
"start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}},
|
||||
"expression": {
|
||||
"type": "TSTypeCastExpression",
|
||||
"start":1,"end":4,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":4}},
|
||||
"extra": {
|
||||
"parenthesized": true,
|
||||
"parenStart": 0
|
||||
},
|
||||
"expression": {
|
||||
"type": "Identifier",
|
||||
"start":1,"end":2,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":2},"identifierName":"a"},
|
||||
"name": "a"
|
||||
},
|
||||
"typeAnnotation": {
|
||||
"type": "TSTypeAnnotation",
|
||||
"start":2,"end":4,"loc":{"start":{"line":1,"column":2},"end":{"line":1,"column":4}},
|
||||
"typeAnnotation": {
|
||||
"type": "TSTypeReference",
|
||||
"start":3,"end":4,"loc":{"start":{"line":1,"column":3},"end":{"line":1,"column":4}},
|
||||
"typeName": {
|
||||
"type": "Identifier",
|
||||
"start":3,"end":4,"loc":{"start":{"line":1,"column":3},"end":{"line":1,"column":4},"identifierName":"b"},
|
||||
"name": "b"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "ExpressionStatement",
|
||||
"start":7,"end":17,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":10}},
|
||||
"expression": {
|
||||
"type": "SequenceExpression",
|
||||
"start":8,"end":15,"loc":{"start":{"line":2,"column":1},"end":{"line":2,"column":8}},
|
||||
"extra": {
|
||||
"parenthesized": true,
|
||||
"parenStart": 7
|
||||
},
|
||||
"expressions": [
|
||||
{
|
||||
"type": "TSTypeCastExpression",
|
||||
"start":8,"end":11,"loc":{"start":{"line":2,"column":1},"end":{"line":2,"column":4}},
|
||||
"expression": {
|
||||
"type": "Identifier",
|
||||
"start":8,"end":9,"loc":{"start":{"line":2,"column":1},"end":{"line":2,"column":2},"identifierName":"a"},
|
||||
"name": "a"
|
||||
},
|
||||
"typeAnnotation": {
|
||||
"type": "TSTypeAnnotation",
|
||||
"start":9,"end":11,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":4}},
|
||||
"typeAnnotation": {
|
||||
"type": "TSTypeReference",
|
||||
"start":10,"end":11,"loc":{"start":{"line":2,"column":3},"end":{"line":2,"column":4}},
|
||||
"typeName": {
|
||||
"type": "Identifier",
|
||||
"start":10,"end":11,"loc":{"start":{"line":2,"column":3},"end":{"line":2,"column":4},"identifierName":"b"},
|
||||
"name": "b"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "TSTypeCastExpression",
|
||||
"start":12,"end":15,"loc":{"start":{"line":2,"column":5},"end":{"line":2,"column":8}},
|
||||
"expression": {
|
||||
"type": "Identifier",
|
||||
"start":12,"end":13,"loc":{"start":{"line":2,"column":5},"end":{"line":2,"column":6},"identifierName":"c"},
|
||||
"name": "c"
|
||||
},
|
||||
"typeAnnotation": {
|
||||
"type": "TSTypeAnnotation",
|
||||
"start":13,"end":15,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":8}},
|
||||
"typeAnnotation": {
|
||||
"type": "TSTypeReference",
|
||||
"start":14,"end":15,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":8}},
|
||||
"typeName": {
|
||||
"type": "Identifier",
|
||||
"start":14,"end":15,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":8},"identifierName":"d"},
|
||||
"name": "d"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
],
|
||||
"directives": []
|
||||
}
|
||||
}
|
||||
@ -2,8 +2,8 @@
|
||||
"type": "File",
|
||||
"start":0,"end":26,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":14}},
|
||||
"errors": [
|
||||
"SyntaxError: Did not expect a type annotation here. (1:5)",
|
||||
"SyntaxError: Did not expect a type annotation here. (2:8)"
|
||||
"SyntaxError: Did not expect a type annotation here. (1:6)",
|
||||
"SyntaxError: Did not expect a type annotation here. (2:9)"
|
||||
],
|
||||
"program": {
|
||||
"type": "Program",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user