Fix parsing of single untyped param in IIFE

Closes #433
This commit is contained in:
강동윤 2019-10-23 20:44:22 +09:00
parent 2e5c81ee8e
commit 7f5d3a4db1
6 changed files with 488 additions and 5 deletions

View File

@ -999,8 +999,6 @@ impl<'a, I: Input> Parser<'a, I> {
// as a pattern instead of reparsing)
while !eof!() && !is!(')') {
if first {
first = false;
if is!("async") {
// https://github.com/swc-project/swc/issues/410
self.state.potential_arrow_start = Some(cur_pos!());
@ -1159,6 +1157,46 @@ impl<'a, I: Input> Parser<'a, I> {
} else {
items.push(PatOrExprOrSpread::ExprOrSpread(arg));
}
// https://github.com/swc-project/swc/issues/433
if first && eat!("=>") && {
debug_assert_eq!(items.len(), 1);
match items[0] {
PatOrExprOrSpread::ExprOrSpread(ExprOrSpread { ref expr, .. })
| PatOrExprOrSpread::Pat(Pat::Expr(ref expr)) => match **expr {
Expr::Ident(..) => true,
_ => false,
},
PatOrExprOrSpread::Pat(Pat::Ident(..)) => true,
_ => false,
}
} {
let params = self
.parse_paren_items_as_params(items)?
.into_iter()
.collect();
let body: BlockStmtOrExpr = self.parse_fn_body(false, false)?;
expect!(')');
let span = span!(start);
return Ok(vec![PatOrExprOrSpread::ExprOrSpread(ExprOrSpread {
expr: Box::new(
ArrowExpr {
span,
body,
is_async: false,
is_generator: false,
params,
type_params: None,
return_type: None,
}
.into(),
),
spread: None,
})]);
}
first = false;
}
expect!(')');

View File

@ -1,6 +1,6 @@
error: Expected Comma, got Some(Arrow)
--> $DIR/tests/test262-parser/fail/7f9bbf314145e16f.js:1:4
error: Expected Comma, got Some(Num(0.0))
--> $DIR/tests/test262-parser/fail/7f9bbf314145e16f.js:1:6
|
1 | ({}=>0)
| ^^
| ^

View File

@ -0,0 +1 @@
const result = (x => x)(1);

View File

@ -0,0 +1,203 @@
{
"type": "Module",
"span": {
"start": 0,
"end": 27,
"ctxt": 0,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 27
}
}
},
"body": [
{
"type": "VariableDeclaration",
"span": {
"start": 0,
"end": 27,
"ctxt": 0,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 27
}
}
},
"kind": "const",
"declare": false,
"declarations": [
{
"type": "VariableDeclarator",
"span": {
"start": 6,
"end": 26,
"ctxt": 0,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 26
}
}
},
"id": {
"type": "Identifier",
"span": {
"start": 6,
"end": 12,
"ctxt": 0,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 12
}
}
},
"value": "result",
"optional": false
},
"init": {
"type": "CallExpression",
"span": {
"start": 15,
"end": 26,
"ctxt": 0,
"loc": {
"start": {
"line": 1,
"column": 15
},
"end": {
"line": 1,
"column": 26
}
}
},
"callee": {
"type": "ParenthesisExpression",
"span": {
"start": 15,
"end": 23,
"ctxt": 0,
"loc": {
"start": {
"line": 1,
"column": 15
},
"end": {
"line": 1,
"column": 23
}
}
},
"expression": {
"type": "ArrowFunctionExpression",
"span": {
"start": 16,
"end": 23,
"ctxt": 0,
"loc": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 1,
"column": 23
}
}
},
"params": [
{
"type": "Identifier",
"span": {
"start": 16,
"end": 17,
"ctxt": 0,
"loc": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 1,
"column": 17
}
}
},
"value": "x",
"optional": false
}
],
"body": {
"type": "Identifier",
"span": {
"start": 21,
"end": 22,
"ctxt": 0,
"loc": {
"start": {
"line": 1,
"column": 21
},
"end": {
"line": 1,
"column": 22
}
}
},
"value": "x",
"optional": false
},
"async": false,
"generator": false
}
},
"arguments": [
{
"expr": {
"type": "NumericLiteral",
"span": {
"start": 24,
"end": 25,
"ctxt": 0,
"loc": {
"start": {
"line": 1,
"column": 24
},
"end": {
"line": 1,
"column": 25
}
}
},
"value": 1.0
}
}
],
"typeArguments": null
},
"definite": false
}
]
}
]
}

View File

@ -0,0 +1 @@
const result2 = ((x: number) => x)(1);

View File

@ -0,0 +1,240 @@
{
"type": "Module",
"span": {
"start": 0,
"end": 38,
"ctxt": 0,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 38
}
}
},
"body": [
{
"type": "VariableDeclaration",
"span": {
"start": 0,
"end": 38,
"ctxt": 0,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 38
}
}
},
"kind": "const",
"declare": false,
"declarations": [
{
"type": "VariableDeclarator",
"span": {
"start": 6,
"end": 37,
"ctxt": 0,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 37
}
}
},
"id": {
"type": "Identifier",
"span": {
"start": 6,
"end": 13,
"ctxt": 0,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 13
}
}
},
"value": "result2",
"optional": false
},
"init": {
"type": "CallExpression",
"span": {
"start": 16,
"end": 37,
"ctxt": 0,
"loc": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 1,
"column": 37
}
}
},
"callee": {
"type": "ParenthesisExpression",
"span": {
"start": 16,
"end": 34,
"ctxt": 0,
"loc": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 1,
"column": 34
}
}
},
"expression": {
"type": "ArrowFunctionExpression",
"span": {
"start": 17,
"end": 33,
"ctxt": 0,
"loc": {
"start": {
"line": 1,
"column": 17
},
"end": {
"line": 1,
"column": 33
}
}
},
"params": [
{
"type": "Identifier",
"span": {
"start": 18,
"end": 19,
"ctxt": 0,
"loc": {
"start": {
"line": 1,
"column": 18
},
"end": {
"line": 1,
"column": 19
}
}
},
"value": "x",
"typeAnnotation": {
"type": "TsTypeAnnotation",
"span": {
"start": 19,
"end": 27,
"ctxt": 0,
"loc": {
"start": {
"line": 1,
"column": 19
},
"end": {
"line": 1,
"column": 27
}
}
},
"typeAnnotation": {
"type": "TsKeywordType",
"span": {
"start": 21,
"end": 27,
"ctxt": 0,
"loc": {
"start": {
"line": 1,
"column": 21
},
"end": {
"line": 1,
"column": 27
}
}
},
"kind": "number"
}
},
"optional": false
}
],
"body": {
"type": "Identifier",
"span": {
"start": 32,
"end": 33,
"ctxt": 0,
"loc": {
"start": {
"line": 1,
"column": 32
},
"end": {
"line": 1,
"column": 33
}
}
},
"value": "x",
"optional": false
},
"async": false,
"generator": false
}
},
"arguments": [
{
"expr": {
"type": "NumericLiteral",
"span": {
"start": 35,
"end": 36,
"ctxt": 0,
"loc": {
"start": {
"line": 1,
"column": 35
},
"end": {
"line": 1,
"column": 36
}
}
},
"value": 1.0
}
}
],
"typeArguments": null
},
"definite": false
}
]
}
]
}