[ts] Error on invalid type casts in JSX (#12221)
This commit is contained in:
parent
8b579a27dc
commit
c00bb14f79
@ -752,7 +752,7 @@ export default class ExpressionParser extends LValParser {
|
|||||||
this.state.yieldPos = oldYieldPos;
|
this.state.yieldPos = oldYieldPos;
|
||||||
this.state.awaitPos = oldAwaitPos;
|
this.state.awaitPos = oldAwaitPos;
|
||||||
} else {
|
} else {
|
||||||
this.toReferencedListDeep(node.arguments);
|
this.toReferencedArguments(node);
|
||||||
|
|
||||||
// We keep the old value if it isn't null, for cases like
|
// We keep the old value if it isn't null, for cases like
|
||||||
// (x = async(yield)) => {}
|
// (x = async(yield)) => {}
|
||||||
@ -791,6 +791,13 @@ export default class ExpressionParser extends LValParser {
|
|||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
toReferencedArguments(
|
||||||
|
node: N.CallExpression | N.OptionalCallExpression,
|
||||||
|
isParenthesizedExpr?: boolean,
|
||||||
|
) {
|
||||||
|
this.toReferencedListDeep(node.arguments, isParenthesizedExpr);
|
||||||
|
}
|
||||||
|
|
||||||
// MemberExpression [?Yield, ?Await] TemplateLiteral[?Yield, ?Await, +Tagged]
|
// MemberExpression [?Yield, ?Await] TemplateLiteral[?Yield, ?Await, +Tagged]
|
||||||
// CallExpression [?Yield, ?Await] TemplateLiteral[?Yield, ?Await, +Tagged]
|
// CallExpression [?Yield, ?Await] TemplateLiteral[?Yield, ?Await, +Tagged]
|
||||||
parseTaggedTemplateExpression(
|
parseTaggedTemplateExpression(
|
||||||
@ -2057,14 +2064,6 @@ export default class ExpressionParser extends LValParser {
|
|||||||
refExpressionErrors,
|
refExpressionErrors,
|
||||||
node,
|
node,
|
||||||
);
|
);
|
||||||
if (canBePattern && !this.state.maybeInArrowParameters) {
|
|
||||||
// This could be an array pattern:
|
|
||||||
// ([a: string, b: string]) => {}
|
|
||||||
// In this case, we don't have to call toReferencedList. We will
|
|
||||||
// call it, if needed, when we are sure that it is a parenthesized
|
|
||||||
// expression by calling toReferencedListDeep.
|
|
||||||
this.toReferencedList(node.elements);
|
|
||||||
}
|
|
||||||
this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
|
this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
|
||||||
return this.finishNode(
|
return this.finishNode(
|
||||||
node,
|
node,
|
||||||
|
|||||||
@ -379,16 +379,19 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
|||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
toReferencedListDeep(
|
toReferencedArguments(
|
||||||
exprList: $ReadOnlyArray<?N.Expression>,
|
node:
|
||||||
isParenthesizedExpr?: boolean,
|
| N.CallExpression
|
||||||
): void {
|
| N.OptionalCallExpression
|
||||||
|
| N.EstreeImportExpression,
|
||||||
|
/* isParenthesizedExpr?: boolean, */
|
||||||
|
) {
|
||||||
// ImportExpressions do not have an arguments array.
|
// ImportExpressions do not have an arguments array.
|
||||||
if (!exprList) {
|
if (node.type === "ImportExpression") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
super.toReferencedListDeep(exprList, isParenthesizedExpr);
|
super.toReferencedArguments(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
parseExport(node: N.Node) {
|
parseExport(node: N.Node) {
|
||||||
|
|||||||
@ -2233,6 +2233,31 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
|||||||
return exprList;
|
return exprList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
parseArrayLike(
|
||||||
|
close: TokenType,
|
||||||
|
canBePattern: boolean,
|
||||||
|
isTuple: boolean,
|
||||||
|
refExpressionErrors: ?ExpressionErrors,
|
||||||
|
): N.ArrayExpression | N.TupleExpression {
|
||||||
|
const node = super.parseArrayLike(
|
||||||
|
close,
|
||||||
|
canBePattern,
|
||||||
|
isTuple,
|
||||||
|
refExpressionErrors,
|
||||||
|
);
|
||||||
|
|
||||||
|
// This could be an array pattern:
|
||||||
|
// ([a: string, b: string]) => {}
|
||||||
|
// In this case, we don't have to call toReferencedList. We will
|
||||||
|
// call it, if needed, when we are sure that it is a parenthesized
|
||||||
|
// expression by calling toReferencedListDeep.
|
||||||
|
if (canBePattern && !this.state.maybeInArrowParameters) {
|
||||||
|
this.toReferencedList(node.elements);
|
||||||
|
}
|
||||||
|
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
checkLVal(
|
checkLVal(
|
||||||
expr: N.Expression,
|
expr: N.Expression,
|
||||||
bindingType: BindingTypes = BIND_NONE,
|
bindingType: BindingTypes = BIND_NONE,
|
||||||
|
|||||||
@ -1839,6 +1839,16 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
|||||||
return exprList;
|
return exprList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
parseArrayLike(...args): N.ArrayExpression | N.TupleExpression {
|
||||||
|
const node = super.parseArrayLike(...args);
|
||||||
|
|
||||||
|
if (node.type === "ArrayExpression") {
|
||||||
|
this.tsCheckForInvalidTypeCasts(node.elements);
|
||||||
|
}
|
||||||
|
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
parseSubscript(
|
parseSubscript(
|
||||||
base: N.Expression,
|
base: N.Expression,
|
||||||
startPos: number,
|
startPos: number,
|
||||||
|
|||||||
@ -1,2 +1,4 @@
|
|||||||
(a:b);
|
(a:b);
|
||||||
(a:b,c:d);
|
(a:b,c:d);
|
||||||
|
[a:b];
|
||||||
|
[a:b, c:d];
|
||||||
|
|||||||
@ -1,14 +1,17 @@
|
|||||||
{
|
{
|
||||||
"type": "File",
|
"type": "File",
|
||||||
"start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":10}},
|
"start":0,"end":36,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":11}},
|
||||||
"errors": [
|
"errors": [
|
||||||
"SyntaxError: Did not expect a type annotation here. (1:2)",
|
"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:2)",
|
||||||
"SyntaxError: Did not expect a type annotation here. (2:6)"
|
"SyntaxError: Did not expect a type annotation here. (2:6)",
|
||||||
|
"SyntaxError: Did not expect a type annotation here. (3:2)",
|
||||||
|
"SyntaxError: Did not expect a type annotation here. (4:2)",
|
||||||
|
"SyntaxError: Did not expect a type annotation here. (4:7)"
|
||||||
],
|
],
|
||||||
"program": {
|
"program": {
|
||||||
"type": "Program",
|
"type": "Program",
|
||||||
"start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":10}},
|
"start":0,"end":36,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":11}},
|
||||||
"sourceType": "module",
|
"sourceType": "module",
|
||||||
"interpreter": null,
|
"interpreter": null,
|
||||||
"body": [
|
"body": [
|
||||||
@ -99,6 +102,92 @@
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "ExpressionStatement",
|
||||||
|
"start":18,"end":24,"loc":{"start":{"line":3,"column":0},"end":{"line":3,"column":6}},
|
||||||
|
"expression": {
|
||||||
|
"type": "ArrayExpression",
|
||||||
|
"start":18,"end":23,"loc":{"start":{"line":3,"column":0},"end":{"line":3,"column":5}},
|
||||||
|
"elements": [
|
||||||
|
{
|
||||||
|
"type": "TSTypeCastExpression",
|
||||||
|
"start":19,"end":22,"loc":{"start":{"line":3,"column":1},"end":{"line":3,"column":4}},
|
||||||
|
"expression": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":19,"end":20,"loc":{"start":{"line":3,"column":1},"end":{"line":3,"column":2},"identifierName":"a"},
|
||||||
|
"name": "a"
|
||||||
|
},
|
||||||
|
"typeAnnotation": {
|
||||||
|
"type": "TSTypeAnnotation",
|
||||||
|
"start":20,"end":22,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":4}},
|
||||||
|
"typeAnnotation": {
|
||||||
|
"type": "TSTypeReference",
|
||||||
|
"start":21,"end":22,"loc":{"start":{"line":3,"column":3},"end":{"line":3,"column":4}},
|
||||||
|
"typeName": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":21,"end":22,"loc":{"start":{"line":3,"column":3},"end":{"line":3,"column":4},"identifierName":"b"},
|
||||||
|
"name": "b"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "ExpressionStatement",
|
||||||
|
"start":25,"end":36,"loc":{"start":{"line":4,"column":0},"end":{"line":4,"column":11}},
|
||||||
|
"expression": {
|
||||||
|
"type": "ArrayExpression",
|
||||||
|
"start":25,"end":35,"loc":{"start":{"line":4,"column":0},"end":{"line":4,"column":10}},
|
||||||
|
"elements": [
|
||||||
|
{
|
||||||
|
"type": "TSTypeCastExpression",
|
||||||
|
"start":26,"end":29,"loc":{"start":{"line":4,"column":1},"end":{"line":4,"column":4}},
|
||||||
|
"expression": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":26,"end":27,"loc":{"start":{"line":4,"column":1},"end":{"line":4,"column":2},"identifierName":"a"},
|
||||||
|
"name": "a"
|
||||||
|
},
|
||||||
|
"typeAnnotation": {
|
||||||
|
"type": "TSTypeAnnotation",
|
||||||
|
"start":27,"end":29,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":4}},
|
||||||
|
"typeAnnotation": {
|
||||||
|
"type": "TSTypeReference",
|
||||||
|
"start":28,"end":29,"loc":{"start":{"line":4,"column":3},"end":{"line":4,"column":4}},
|
||||||
|
"typeName": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":28,"end":29,"loc":{"start":{"line":4,"column":3},"end":{"line":4,"column":4},"identifierName":"b"},
|
||||||
|
"name": "b"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "TSTypeCastExpression",
|
||||||
|
"start":31,"end":34,"loc":{"start":{"line":4,"column":6},"end":{"line":4,"column":9}},
|
||||||
|
"expression": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":31,"end":32,"loc":{"start":{"line":4,"column":6},"end":{"line":4,"column":7},"identifierName":"c"},
|
||||||
|
"name": "c"
|
||||||
|
},
|
||||||
|
"typeAnnotation": {
|
||||||
|
"type": "TSTypeAnnotation",
|
||||||
|
"start":32,"end":34,"loc":{"start":{"line":4,"column":7},"end":{"line":4,"column":9}},
|
||||||
|
"typeAnnotation": {
|
||||||
|
"type": "TSTypeReference",
|
||||||
|
"start":33,"end":34,"loc":{"start":{"line":4,"column":8},"end":{"line":4,"column":9}},
|
||||||
|
"typeName": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":33,"end":34,"loc":{"start":{"line":4,"column":8},"end":{"line":4,"column":9},"identifierName":"d"},
|
||||||
|
"name": "d"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"directives": []
|
"directives": []
|
||||||
|
|||||||
6
packages/babel-parser/test/fixtures/typescript/tsx/cast-invalid/input.tsx
vendored
Normal file
6
packages/babel-parser/test/fixtures/typescript/tsx/cast-invalid/input.tsx
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
function Foo() {
|
||||||
|
return (
|
||||||
|
<div propA={[ key: value ]} propsB={(a: b)} />
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
142
packages/babel-parser/test/fixtures/typescript/tsx/cast-invalid/output.json
vendored
Normal file
142
packages/babel-parser/test/fixtures/typescript/tsx/cast-invalid/output.json
vendored
Normal file
@ -0,0 +1,142 @@
|
|||||||
|
{
|
||||||
|
"type": "File",
|
||||||
|
"start":0,"end":85,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}},
|
||||||
|
"errors": [
|
||||||
|
"SyntaxError: Did not expect a type annotation here. (3:21)",
|
||||||
|
"SyntaxError: Did not expect a type annotation here. (3:42)"
|
||||||
|
],
|
||||||
|
"program": {
|
||||||
|
"type": "Program",
|
||||||
|
"start":0,"end":85,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}},
|
||||||
|
"sourceType": "module",
|
||||||
|
"interpreter": null,
|
||||||
|
"body": [
|
||||||
|
{
|
||||||
|
"type": "FunctionDeclaration",
|
||||||
|
"start":0,"end":85,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}},
|
||||||
|
"id": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":9,"end":12,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":12},"identifierName":"Foo"},
|
||||||
|
"name": "Foo"
|
||||||
|
},
|
||||||
|
"generator": false,
|
||||||
|
"async": false,
|
||||||
|
"params": [],
|
||||||
|
"body": {
|
||||||
|
"type": "BlockStatement",
|
||||||
|
"start":15,"end":85,"loc":{"start":{"line":1,"column":15},"end":{"line":5,"column":1}},
|
||||||
|
"body": [
|
||||||
|
{
|
||||||
|
"type": "ReturnStatement",
|
||||||
|
"start":19,"end":83,"loc":{"start":{"line":2,"column":2},"end":{"line":4,"column":4}},
|
||||||
|
"argument": {
|
||||||
|
"type": "JSXElement",
|
||||||
|
"start":32,"end":78,"loc":{"start":{"line":3,"column":4},"end":{"line":3,"column":50}},
|
||||||
|
"extra": {
|
||||||
|
"parenthesized": true,
|
||||||
|
"parenStart": 26
|
||||||
|
},
|
||||||
|
"openingElement": {
|
||||||
|
"type": "JSXOpeningElement",
|
||||||
|
"start":32,"end":78,"loc":{"start":{"line":3,"column":4},"end":{"line":3,"column":50}},
|
||||||
|
"name": {
|
||||||
|
"type": "JSXIdentifier",
|
||||||
|
"start":33,"end":36,"loc":{"start":{"line":3,"column":5},"end":{"line":3,"column":8}},
|
||||||
|
"name": "div"
|
||||||
|
},
|
||||||
|
"attributes": [
|
||||||
|
{
|
||||||
|
"type": "JSXAttribute",
|
||||||
|
"start":37,"end":59,"loc":{"start":{"line":3,"column":9},"end":{"line":3,"column":31}},
|
||||||
|
"name": {
|
||||||
|
"type": "JSXIdentifier",
|
||||||
|
"start":37,"end":42,"loc":{"start":{"line":3,"column":9},"end":{"line":3,"column":14}},
|
||||||
|
"name": "propA"
|
||||||
|
},
|
||||||
|
"value": {
|
||||||
|
"type": "JSXExpressionContainer",
|
||||||
|
"start":43,"end":59,"loc":{"start":{"line":3,"column":15},"end":{"line":3,"column":31}},
|
||||||
|
"expression": {
|
||||||
|
"type": "ArrayExpression",
|
||||||
|
"start":44,"end":58,"loc":{"start":{"line":3,"column":16},"end":{"line":3,"column":30}},
|
||||||
|
"elements": [
|
||||||
|
{
|
||||||
|
"type": "TSTypeCastExpression",
|
||||||
|
"start":46,"end":56,"loc":{"start":{"line":3,"column":18},"end":{"line":3,"column":28}},
|
||||||
|
"expression": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":46,"end":49,"loc":{"start":{"line":3,"column":18},"end":{"line":3,"column":21},"identifierName":"key"},
|
||||||
|
"name": "key"
|
||||||
|
},
|
||||||
|
"typeAnnotation": {
|
||||||
|
"type": "TSTypeAnnotation",
|
||||||
|
"start":49,"end":56,"loc":{"start":{"line":3,"column":21},"end":{"line":3,"column":28}},
|
||||||
|
"typeAnnotation": {
|
||||||
|
"type": "TSTypeReference",
|
||||||
|
"start":51,"end":56,"loc":{"start":{"line":3,"column":23},"end":{"line":3,"column":28}},
|
||||||
|
"typeName": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":51,"end":56,"loc":{"start":{"line":3,"column":23},"end":{"line":3,"column":28},"identifierName":"value"},
|
||||||
|
"name": "value"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "JSXAttribute",
|
||||||
|
"start":60,"end":75,"loc":{"start":{"line":3,"column":32},"end":{"line":3,"column":47}},
|
||||||
|
"name": {
|
||||||
|
"type": "JSXIdentifier",
|
||||||
|
"start":60,"end":66,"loc":{"start":{"line":3,"column":32},"end":{"line":3,"column":38}},
|
||||||
|
"name": "propsB"
|
||||||
|
},
|
||||||
|
"value": {
|
||||||
|
"type": "JSXExpressionContainer",
|
||||||
|
"start":67,"end":75,"loc":{"start":{"line":3,"column":39},"end":{"line":3,"column":47}},
|
||||||
|
"expression": {
|
||||||
|
"type": "TSTypeCastExpression",
|
||||||
|
"start":69,"end":73,"loc":{"start":{"line":3,"column":41},"end":{"line":3,"column":45}},
|
||||||
|
"extra": {
|
||||||
|
"parenthesized": true,
|
||||||
|
"parenStart": 68
|
||||||
|
},
|
||||||
|
"expression": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":69,"end":70,"loc":{"start":{"line":3,"column":41},"end":{"line":3,"column":42},"identifierName":"a"},
|
||||||
|
"name": "a"
|
||||||
|
},
|
||||||
|
"typeAnnotation": {
|
||||||
|
"type": "TSTypeAnnotation",
|
||||||
|
"start":70,"end":73,"loc":{"start":{"line":3,"column":42},"end":{"line":3,"column":45}},
|
||||||
|
"typeAnnotation": {
|
||||||
|
"type": "TSTypeReference",
|
||||||
|
"start":72,"end":73,"loc":{"start":{"line":3,"column":44},"end":{"line":3,"column":45}},
|
||||||
|
"typeName": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":72,"end":73,"loc":{"start":{"line":3,"column":44},"end":{"line":3,"column":45},"identifierName":"b"},
|
||||||
|
"name": "b"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"selfClosing": true
|
||||||
|
},
|
||||||
|
"closingElement": null,
|
||||||
|
"children": []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"directives": []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"directives": []
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user