fix(es/parser): Fix parsing of tsx with a type annotation in a conditional expression (#7440)

**Related issue:**

 - Closes #7224.
This commit is contained in:
Donny/강동윤 2023-05-24 15:57:50 +09:00 committed by GitHub
parent b95cd8444e
commit a37d59a134
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 453 additions and 1 deletions

View File

@ -173,7 +173,13 @@ impl<I: Tokens> Parser<I> {
let name = self.parse_jsx_namespaced_name()?; let name = self.parse_jsx_namespaced_name()?;
let value = if eat!(self, '=') { let value = if eat!(self, '=') {
self.parse_jsx_attr_value().map(Some)? let ctx = Context {
in_cond_expr: false,
will_expect_colon_for_cond: false,
..self.ctx()
};
self.with_ctx(ctx).parse_jsx_attr_value().map(Some)?
} else { } else {
None None
}; };

View File

@ -0,0 +1,8 @@
const getStatusLabel = () => {
return (
<div>
{disabled ? <StyledComponent className={({ theme }): { [key: string]: any } => ({ color: theme.blue })} /> : null}
</div>
);
};
export default getStatusLabel;

View File

@ -0,0 +1,438 @@
{
"type": "Module",
"span": {
"start": 1,
"end": 223,
"ctxt": 0
},
"body": [
{
"type": "VariableDeclaration",
"span": {
"start": 1,
"end": 192,
"ctxt": 0
},
"kind": "const",
"declare": false,
"declarations": [
{
"type": "VariableDeclarator",
"span": {
"start": 7,
"end": 191,
"ctxt": 0
},
"id": {
"type": "Identifier",
"span": {
"start": 7,
"end": 21,
"ctxt": 0
},
"value": "getStatusLabel",
"optional": false,
"typeAnnotation": null
},
"init": {
"type": "ArrowFunctionExpression",
"span": {
"start": 24,
"end": 191,
"ctxt": 0
},
"params": [],
"body": {
"type": "BlockStatement",
"span": {
"start": 30,
"end": 191,
"ctxt": 0
},
"stmts": [
{
"type": "ReturnStatement",
"span": {
"start": 34,
"end": 189,
"ctxt": 0
},
"argument": {
"type": "ParenthesisExpression",
"span": {
"start": 41,
"end": 188,
"ctxt": 0
},
"expression": {
"type": "JSXElement",
"span": {
"start": 47,
"end": 184,
"ctxt": 0
},
"opening": {
"type": "JSXOpeningElement",
"name": {
"type": "Identifier",
"span": {
"start": 48,
"end": 51,
"ctxt": 0
},
"value": "div",
"optional": false
},
"span": {
"start": 47,
"end": 52,
"ctxt": 0
},
"attributes": [],
"selfClosing": false,
"typeArguments": null
},
"children": [
{
"type": "JSXText",
"span": {
"start": 52,
"end": 59,
"ctxt": 0
},
"value": "\n\n ",
"raw": "\n\n "
},
{
"type": "JSXExpressionContainer",
"span": {
"start": 59,
"end": 173,
"ctxt": 0
},
"expression": {
"type": "ConditionalExpression",
"span": {
"start": 60,
"end": 172,
"ctxt": 0
},
"test": {
"type": "Identifier",
"span": {
"start": 60,
"end": 68,
"ctxt": 0
},
"value": "disabled",
"optional": false
},
"consequent": {
"type": "JSXElement",
"span": {
"start": 71,
"end": 165,
"ctxt": 0
},
"opening": {
"type": "JSXOpeningElement",
"name": {
"type": "Identifier",
"span": {
"start": 72,
"end": 87,
"ctxt": 0
},
"value": "StyledComponent",
"optional": false
},
"span": {
"start": 71,
"end": 165,
"ctxt": 0
},
"attributes": [
{
"type": "JSXAttribute",
"span": {
"start": 88,
"end": 162,
"ctxt": 0
},
"name": {
"type": "Identifier",
"span": {
"start": 88,
"end": 97,
"ctxt": 0
},
"value": "className",
"optional": false
},
"value": {
"type": "JSXExpressionContainer",
"span": {
"start": 98,
"end": 162,
"ctxt": 0
},
"expression": {
"type": "ArrowFunctionExpression",
"span": {
"start": 99,
"end": 161,
"ctxt": 0
},
"params": [
{
"type": "ObjectPattern",
"span": {
"start": 100,
"end": 109,
"ctxt": 0
},
"properties": [
{
"type": "AssignmentPatternProperty",
"span": {
"start": 102,
"end": 107,
"ctxt": 0
},
"key": {
"type": "Identifier",
"span": {
"start": 102,
"end": 107,
"ctxt": 0
},
"value": "theme",
"optional": false
},
"value": null
}
],
"optional": false,
"typeAnnotation": null
}
],
"body": {
"type": "ParenthesisExpression",
"span": {
"start": 138,
"end": 161,
"ctxt": 0
},
"expression": {
"type": "ObjectExpression",
"span": {
"start": 139,
"end": 160,
"ctxt": 0
},
"properties": [
{
"type": "KeyValueProperty",
"key": {
"type": "Identifier",
"span": {
"start": 141,
"end": 146,
"ctxt": 0
},
"value": "color",
"optional": false
},
"value": {
"type": "MemberExpression",
"span": {
"start": 148,
"end": 158,
"ctxt": 0
},
"object": {
"type": "Identifier",
"span": {
"start": 148,
"end": 153,
"ctxt": 0
},
"value": "theme",
"optional": false
},
"property": {
"type": "Identifier",
"span": {
"start": 154,
"end": 158,
"ctxt": 0
},
"value": "blue",
"optional": false
}
}
}
]
}
},
"async": false,
"generator": false,
"typeParameters": null,
"returnType": {
"type": "TsTypeAnnotation",
"span": {
"start": 110,
"end": 134,
"ctxt": 0
},
"typeAnnotation": {
"type": "TsTypeLiteral",
"span": {
"start": 112,
"end": 134,
"ctxt": 0
},
"members": [
{
"type": "TsIndexSignature",
"params": [
{
"type": "Identifier",
"span": {
"start": 115,
"end": 126,
"ctxt": 0
},
"value": "key",
"optional": false,
"typeAnnotation": {
"type": "TsTypeAnnotation",
"span": {
"start": 118,
"end": 126,
"ctxt": 0
},
"typeAnnotation": {
"type": "TsKeywordType",
"span": {
"start": 120,
"end": 126,
"ctxt": 0
},
"kind": "string"
}
}
}
],
"typeAnnotation": {
"type": "TsTypeAnnotation",
"span": {
"start": 127,
"end": 132,
"ctxt": 0
},
"typeAnnotation": {
"type": "TsKeywordType",
"span": {
"start": 129,
"end": 132,
"ctxt": 0
},
"kind": "any"
}
},
"readonly": false,
"static": false,
"span": {
"start": 114,
"end": 132,
"ctxt": 0
}
}
]
}
}
}
}
}
],
"selfClosing": true,
"typeArguments": null
},
"children": [],
"closing": null
},
"alternate": {
"type": "NullLiteral",
"span": {
"start": 168,
"end": 172,
"ctxt": 0
}
}
}
},
{
"type": "JSXText",
"span": {
"start": 173,
"end": 178,
"ctxt": 0
},
"value": "\n\n ",
"raw": "\n\n "
}
],
"closing": {
"type": "JSXClosingElement",
"span": {
"start": 178,
"end": 184,
"ctxt": 0
},
"name": {
"type": "Identifier",
"span": {
"start": 180,
"end": 183,
"ctxt": 0
},
"value": "div",
"optional": false
}
}
}
}
}
]
},
"async": false,
"generator": false,
"typeParameters": null,
"returnType": null
},
"definite": false
}
]
},
{
"type": "ExportDefaultExpression",
"span": {
"start": 193,
"end": 223,
"ctxt": 0
},
"expression": {
"type": "Identifier",
"span": {
"start": 208,
"end": 222,
"ctxt": 0
},
"value": "getStatusLabel",
"optional": false
}
}
],
"interpreter": null
}