fix(es/parser): Allow using async as the first one in parameters (#2388)

This commit is contained in:
Pig Fang 2021-10-10 10:02:32 +08:00 committed by GitHub
parent a5592e3207
commit be3dca295b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 498 additions and 19 deletions

2
Cargo.lock generated
View File

@ -3478,7 +3478,7 @@ dependencies = [
[[package]]
name = "swc_ecma_parser"
version = "0.73.5"
version = "0.73.6"
dependencies = [
"either",
"enum_kind",

View File

@ -7,7 +7,7 @@ include = ["Cargo.toml", "src/**/*.rs", "examples/**/*.rs"]
license = "Apache-2.0/MIT"
name = "swc_ecma_parser"
repository = "https://github.com/swc-project/swc.git"
version = "0.73.5"
version = "0.73.6"
[package.metadata.docs.rs]
all-features = true

View File

@ -1266,7 +1266,12 @@ impl<'a, I: Tokens> Parser<I> {
// as a pattern instead of reparsing)
while !eof!(self) && !is!(self, ')') {
if first {
if is!(self, "async") && !peeked_is!(self, ',') {
if is!(self, "async")
&& matches!(
peek!(self),
Ok(tok!('(') | tok!("function") | Token::Word(..))
)
{
// https://github.com/swc-project/swc/issues/410
self.state.potential_arrow_start = Some(cur_pos!(self));
let expr = self.parse_assignment_expr()?;

View File

@ -1 +1,4 @@
let fn = (async, x) => {};
let fn1 = (async, x) => {};
let fn2 = (async = 1, x) => {};
let fn3 = (async?, x?) => {};
let fn4 = (async: any, x: any) => {};

View File

@ -2,7 +2,7 @@
"type": "Script",
"span": {
"start": 0,
"end": 26,
"end": 127,
"ctxt": 0
},
"body": [
@ -10,7 +10,7 @@
"type": "VariableDeclaration",
"span": {
"start": 0,
"end": 26,
"end": 27,
"ctxt": 0
},
"kind": "let",
@ -20,33 +20,33 @@
"type": "VariableDeclarator",
"span": {
"start": 4,
"end": 25,
"end": 26,
"ctxt": 0
},
"id": {
"type": "Identifier",
"span": {
"start": 4,
"end": 6,
"end": 7,
"ctxt": 0
},
"value": "fn",
"value": "fn1",
"optional": false,
"typeAnnotation": null
},
"init": {
"type": "ArrowFunctionExpression",
"span": {
"start": 9,
"end": 25,
"start": 10,
"end": 26,
"ctxt": 0
},
"params": [
{
"type": "Identifier",
"span": {
"start": 10,
"end": 15,
"start": 11,
"end": 16,
"ctxt": 0
},
"value": "async",
@ -56,8 +56,8 @@
{
"type": "Identifier",
"span": {
"start": 17,
"end": 18,
"start": 18,
"end": 19,
"ctxt": 0
},
"value": "x",
@ -68,8 +68,289 @@
"body": {
"type": "BlockStatement",
"span": {
"start": 23,
"end": 25,
"start": 24,
"end": 26,
"ctxt": 0
},
"stmts": []
},
"async": false,
"generator": false,
"typeParameters": null,
"returnType": null
},
"definite": false
}
]
},
{
"type": "VariableDeclaration",
"span": {
"start": 28,
"end": 59,
"ctxt": 0
},
"kind": "let",
"declare": false,
"declarations": [
{
"type": "VariableDeclarator",
"span": {
"start": 32,
"end": 58,
"ctxt": 0
},
"id": {
"type": "Identifier",
"span": {
"start": 32,
"end": 35,
"ctxt": 0
},
"value": "fn2",
"optional": false,
"typeAnnotation": null
},
"init": {
"type": "ArrowFunctionExpression",
"span": {
"start": 38,
"end": 58,
"ctxt": 0
},
"params": [
{
"type": "AssignmentPattern",
"span": {
"start": 39,
"end": 48,
"ctxt": 0
},
"left": {
"type": "Identifier",
"span": {
"start": 39,
"end": 44,
"ctxt": 0
},
"value": "async",
"optional": false,
"typeAnnotation": null
},
"right": {
"type": "NumericLiteral",
"span": {
"start": 47,
"end": 48,
"ctxt": 0
},
"value": 1.0
},
"typeAnnotation": null
},
{
"type": "Identifier",
"span": {
"start": 50,
"end": 51,
"ctxt": 0
},
"value": "x",
"optional": false,
"typeAnnotation": null
}
],
"body": {
"type": "BlockStatement",
"span": {
"start": 56,
"end": 58,
"ctxt": 0
},
"stmts": []
},
"async": false,
"generator": false,
"typeParameters": null,
"returnType": null
},
"definite": false
}
]
},
{
"type": "VariableDeclaration",
"span": {
"start": 60,
"end": 89,
"ctxt": 0
},
"kind": "let",
"declare": false,
"declarations": [
{
"type": "VariableDeclarator",
"span": {
"start": 64,
"end": 88,
"ctxt": 0
},
"id": {
"type": "Identifier",
"span": {
"start": 64,
"end": 67,
"ctxt": 0
},
"value": "fn3",
"optional": false,
"typeAnnotation": null
},
"init": {
"type": "ArrowFunctionExpression",
"span": {
"start": 70,
"end": 88,
"ctxt": 0
},
"params": [
{
"type": "Identifier",
"span": {
"start": 71,
"end": 76,
"ctxt": 0
},
"value": "async",
"optional": true,
"typeAnnotation": null
},
{
"type": "Identifier",
"span": {
"start": 79,
"end": 80,
"ctxt": 0
},
"value": "x",
"optional": true,
"typeAnnotation": null
}
],
"body": {
"type": "BlockStatement",
"span": {
"start": 86,
"end": 88,
"ctxt": 0
},
"stmts": []
},
"async": false,
"generator": false,
"typeParameters": null,
"returnType": null
},
"definite": false
}
]
},
{
"type": "VariableDeclaration",
"span": {
"start": 90,
"end": 127,
"ctxt": 0
},
"kind": "let",
"declare": false,
"declarations": [
{
"type": "VariableDeclarator",
"span": {
"start": 94,
"end": 126,
"ctxt": 0
},
"id": {
"type": "Identifier",
"span": {
"start": 94,
"end": 97,
"ctxt": 0
},
"value": "fn4",
"optional": false,
"typeAnnotation": null
},
"init": {
"type": "ArrowFunctionExpression",
"span": {
"start": 100,
"end": 126,
"ctxt": 0
},
"params": [
{
"type": "Identifier",
"span": {
"start": 101,
"end": 111,
"ctxt": 0
},
"value": "async",
"optional": false,
"typeAnnotation": {
"type": "TsTypeAnnotation",
"span": {
"start": 106,
"end": 111,
"ctxt": 0
},
"typeAnnotation": {
"type": "TsKeywordType",
"span": {
"start": 108,
"end": 111,
"ctxt": 0
},
"kind": "any"
}
}
},
{
"type": "Identifier",
"span": {
"start": 113,
"end": 119,
"ctxt": 0
},
"value": "x",
"optional": false,
"typeAnnotation": {
"type": "TsTypeAnnotation",
"span": {
"start": 114,
"end": 119,
"ctxt": 0
},
"typeAnnotation": {
"type": "TsKeywordType",
"span": {
"start": 116,
"end": 119,
"ctxt": 0
},
"kind": "any"
}
}
}
],
"body": {
"type": "BlockStatement",
"span": {
"start": 124,
"end": 126,
"ctxt": 0
},
"stmts": []

View File

@ -1 +1,3 @@
(async () => {})();
(async async => {})();
(async function () {})();

View File

@ -2,7 +2,7 @@
"type": "Script",
"span": {
"start": 0,
"end": 19,
"end": 68,
"ctxt": 0
},
"body": [
@ -53,6 +53,116 @@
"arguments": [],
"typeArguments": null
}
},
{
"type": "ExpressionStatement",
"span": {
"start": 20,
"end": 42,
"ctxt": 0
},
"expression": {
"type": "CallExpression",
"span": {
"start": 20,
"end": 41,
"ctxt": 0
},
"callee": {
"type": "ParenthesisExpression",
"span": {
"start": 20,
"end": 39,
"ctxt": 0
},
"expression": {
"type": "ArrowFunctionExpression",
"span": {
"start": 21,
"end": 38,
"ctxt": 0
},
"params": [
{
"type": "Identifier",
"span": {
"start": 27,
"end": 32,
"ctxt": 0
},
"value": "async",
"optional": false,
"typeAnnotation": null
}
],
"body": {
"type": "BlockStatement",
"span": {
"start": 36,
"end": 38,
"ctxt": 0
},
"stmts": []
},
"async": true,
"generator": false,
"typeParameters": null,
"returnType": null
}
},
"arguments": [],
"typeArguments": null
}
},
{
"type": "ExpressionStatement",
"span": {
"start": 43,
"end": 68,
"ctxt": 0
},
"expression": {
"type": "CallExpression",
"span": {
"start": 43,
"end": 67,
"ctxt": 0
},
"callee": {
"type": "ParenthesisExpression",
"span": {
"start": 43,
"end": 65,
"ctxt": 0
},
"expression": {
"type": "FunctionExpression",
"identifier": null,
"params": [],
"decorators": [],
"span": {
"start": 44,
"end": 64,
"ctxt": 0
},
"body": {
"type": "BlockStatement",
"span": {
"start": 62,
"end": 64,
"ctxt": 0
},
"stmts": []
},
"generator": false,
"async": true,
"typeParameters": null,
"returnType": null
}
},
"arguments": [],
"typeArguments": null
}
}
],
"interpreter": null

View File

@ -1 +1,3 @@
async () => {};
async (async) => {};
async function async() {}

View File

@ -2,7 +2,7 @@
"type": "Script",
"span": {
"start": 0,
"end": 15,
"end": 62,
"ctxt": 0
},
"body": [
@ -35,6 +35,82 @@
"typeParameters": null,
"returnType": null
}
},
{
"type": "ExpressionStatement",
"span": {
"start": 16,
"end": 36,
"ctxt": 0
},
"expression": {
"type": "ArrowFunctionExpression",
"span": {
"start": 16,
"end": 35,
"ctxt": 0
},
"params": [
{
"type": "Identifier",
"span": {
"start": 23,
"end": 28,
"ctxt": 0
},
"value": "async",
"optional": false,
"typeAnnotation": null
}
],
"body": {
"type": "BlockStatement",
"span": {
"start": 33,
"end": 35,
"ctxt": 0
},
"stmts": []
},
"async": true,
"generator": false,
"typeParameters": null,
"returnType": null
}
},
{
"type": "FunctionDeclaration",
"identifier": {
"type": "Identifier",
"span": {
"start": 52,
"end": 57,
"ctxt": 0
},
"value": "async",
"optional": false
},
"declare": false,
"params": [],
"decorators": [],
"span": {
"start": 37,
"end": 62,
"ctxt": 0
},
"body": {
"type": "BlockStatement",
"span": {
"start": 60,
"end": 62,
"ctxt": 0
},
"stmts": []
},
"generator": false,
"async": true,
"typeParameters": null,
"returnType": null
}
],
"interpreter": null