fix(es/parser): Handle trailing comma and bracket after an arrow function in conditional (#3685)

This commit is contained in:
William Tetlow 2022-02-25 12:50:04 +00:00 committed by GitHub
parent 70034af7b9
commit 342dccce47
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 21 additions and 331 deletions

View File

@ -707,7 +707,7 @@ impl<'a, I: Tokens> Parser<I> {
params.is_simple_parameter_list(),
)?;
if is_direct_child_of_cond && !is_one_of!(p, ':', ';') {
if is_direct_child_of_cond && !is_one_of!(p, ':', ';', ',', ')') {
trace_cur!(p, parse_arrow_in_cond__fail);
unexpected!(p, "fail")
}

View File

@ -494,6 +494,26 @@ fn super_expr_computed() {
);
}
#[test]
fn issue_3672_1() {
test_parser(
"report({
fix: fixable ? null : (): RuleFix => {},
});",
Syntax::Typescript(Default::default()),
|p| p.parse_module(),
);
}
#[test]
fn issue_3672_2() {
test_parser(
"f(a ? (): void => { } : (): void => { })",
Syntax::Typescript(Default::default()),
|p| p.parse_module(),
);
}
#[bench]
fn bench_new_expr_ts(b: &mut Bencher) {
bench_parser(

View File

@ -1,5 +0,0 @@
const x = {
prop: isCorrect
? fn => ({ })
: fn => true,
};

View File

@ -1,160 +0,0 @@
{
"type": "Script",
"span": {
"start": 0,
"end": 82,
"ctxt": 0
},
"body": [
{
"type": "VariableDeclaration",
"span": {
"start": 0,
"end": 82,
"ctxt": 0
},
"kind": "const",
"declare": false,
"declarations": [
{
"type": "VariableDeclarator",
"span": {
"start": 6,
"end": 81,
"ctxt": 0
},
"id": {
"type": "Identifier",
"span": {
"start": 6,
"end": 7,
"ctxt": 0
},
"value": "x",
"optional": false,
"typeAnnotation": null
},
"init": {
"type": "ObjectExpression",
"span": {
"start": 10,
"end": 81,
"ctxt": 0
},
"properties": [
{
"type": "KeyValueProperty",
"key": {
"type": "Identifier",
"span": {
"start": 16,
"end": 20,
"ctxt": 0
},
"value": "prop",
"optional": false
},
"value": {
"type": "ConditionalExpression",
"span": {
"start": 22,
"end": 78,
"ctxt": 0
},
"test": {
"type": "Identifier",
"span": {
"start": 22,
"end": 31,
"ctxt": 0
},
"value": "isCorrect",
"optional": false
},
"consequent": {
"type": "ArrowFunctionExpression",
"span": {
"start": 42,
"end": 53,
"ctxt": 0
},
"params": [
{
"type": "Identifier",
"span": {
"start": 42,
"end": 44,
"ctxt": 0
},
"value": "fn",
"optional": false,
"typeAnnotation": null
}
],
"body": {
"type": "ParenthesisExpression",
"span": {
"start": 48,
"end": 53,
"ctxt": 0
},
"expression": {
"type": "ObjectExpression",
"span": {
"start": 49,
"end": 52,
"ctxt": 0
},
"properties": []
}
},
"async": false,
"generator": false,
"typeParameters": null,
"returnType": null
},
"alternate": {
"type": "ArrowFunctionExpression",
"span": {
"start": 68,
"end": 78,
"ctxt": 0
},
"params": [
{
"type": "Identifier",
"span": {
"start": 68,
"end": 70,
"ctxt": 0
},
"value": "fn",
"optional": false,
"typeAnnotation": null
}
],
"body": {
"type": "BooleanLiteral",
"span": {
"start": 74,
"end": 78,
"ctxt": 0
},
"value": true
},
"async": false,
"generator": false,
"typeParameters": null,
"returnType": null
}
}
}
]
},
"definite": false
}
]
}
],
"interpreter": null
}

View File

@ -1,5 +0,0 @@
const x = {
prop: isCorrect
? fn => ({ })
: fn => true,
};

View File

@ -1,160 +0,0 @@
{
"type": "Script",
"span": {
"start": 0,
"end": 82,
"ctxt": 0
},
"body": [
{
"type": "VariableDeclaration",
"span": {
"start": 0,
"end": 82,
"ctxt": 0
},
"kind": "const",
"declare": false,
"declarations": [
{
"type": "VariableDeclarator",
"span": {
"start": 6,
"end": 81,
"ctxt": 0
},
"id": {
"type": "Identifier",
"span": {
"start": 6,
"end": 7,
"ctxt": 0
},
"value": "x",
"optional": false,
"typeAnnotation": null
},
"init": {
"type": "ObjectExpression",
"span": {
"start": 10,
"end": 81,
"ctxt": 0
},
"properties": [
{
"type": "KeyValueProperty",
"key": {
"type": "Identifier",
"span": {
"start": 16,
"end": 20,
"ctxt": 0
},
"value": "prop",
"optional": false
},
"value": {
"type": "ConditionalExpression",
"span": {
"start": 22,
"end": 78,
"ctxt": 0
},
"test": {
"type": "Identifier",
"span": {
"start": 22,
"end": 31,
"ctxt": 0
},
"value": "isCorrect",
"optional": false
},
"consequent": {
"type": "ArrowFunctionExpression",
"span": {
"start": 42,
"end": 53,
"ctxt": 0
},
"params": [
{
"type": "Identifier",
"span": {
"start": 42,
"end": 44,
"ctxt": 0
},
"value": "fn",
"optional": false,
"typeAnnotation": null
}
],
"body": {
"type": "ParenthesisExpression",
"span": {
"start": 48,
"end": 53,
"ctxt": 0
},
"expression": {
"type": "ObjectExpression",
"span": {
"start": 49,
"end": 52,
"ctxt": 0
},
"properties": []
}
},
"async": false,
"generator": false,
"typeParameters": null,
"returnType": null
},
"alternate": {
"type": "ArrowFunctionExpression",
"span": {
"start": 68,
"end": 78,
"ctxt": 0
},
"params": [
{
"type": "Identifier",
"span": {
"start": 68,
"end": 70,
"ctxt": 0
},
"value": "fn",
"optional": false,
"typeAnnotation": null
}
],
"body": {
"type": "BooleanLiteral",
"span": {
"start": 74,
"end": 78,
"ctxt": 0
},
"value": true
},
"async": false,
"generator": false,
"typeParameters": null,
"returnType": null
}
}
}
]
},
"definite": false
}
]
}
],
"interpreter": null
}