From be3dca295ba410bd287aa4ebeb53af8c9ec74bee Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Sun, 10 Oct 2021 10:02:32 +0800 Subject: [PATCH] fix(es/parser): Allow using `async` as the first one in parameters (#2388) --- Cargo.lock | 2 +- ecmascript/parser/Cargo.toml | 2 +- ecmascript/parser/src/parser/expr.rs | 7 +- .../arrow-function/async-param/input.ts | 5 +- .../arrow-function/async-param/input.ts.json | 307 +++++++++++++++++- .../typescript/custom/issue-410-1/input.ts | 2 + .../custom/issue-410-1/input.ts.json | 112 ++++++- .../typescript/custom/issue-410-2/input.ts | 2 + .../custom/issue-410-2/input.ts.json | 78 ++++- 9 files changed, 498 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e0857a9f048..635357e9466 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3478,7 +3478,7 @@ dependencies = [ [[package]] name = "swc_ecma_parser" -version = "0.73.5" +version = "0.73.6" dependencies = [ "either", "enum_kind", diff --git a/ecmascript/parser/Cargo.toml b/ecmascript/parser/Cargo.toml index e0571213483..b866f45bad9 100644 --- a/ecmascript/parser/Cargo.toml +++ b/ecmascript/parser/Cargo.toml @@ -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 diff --git a/ecmascript/parser/src/parser/expr.rs b/ecmascript/parser/src/parser/expr.rs index 61199ef7c0c..11045056328 100644 --- a/ecmascript/parser/src/parser/expr.rs +++ b/ecmascript/parser/src/parser/expr.rs @@ -1266,7 +1266,12 @@ impl<'a, I: Tokens> Parser { // 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()?; diff --git a/ecmascript/parser/tests/typescript/arrow-function/async-param/input.ts b/ecmascript/parser/tests/typescript/arrow-function/async-param/input.ts index ff6f3474feb..934e469cf04 100644 --- a/ecmascript/parser/tests/typescript/arrow-function/async-param/input.ts +++ b/ecmascript/parser/tests/typescript/arrow-function/async-param/input.ts @@ -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) => {}; diff --git a/ecmascript/parser/tests/typescript/arrow-function/async-param/input.ts.json b/ecmascript/parser/tests/typescript/arrow-function/async-param/input.ts.json index c44a25b45dc..1636dd9789c 100644 --- a/ecmascript/parser/tests/typescript/arrow-function/async-param/input.ts.json +++ b/ecmascript/parser/tests/typescript/arrow-function/async-param/input.ts.json @@ -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": [] diff --git a/ecmascript/parser/tests/typescript/custom/issue-410-1/input.ts b/ecmascript/parser/tests/typescript/custom/issue-410-1/input.ts index 68b172c0e25..c04d571043b 100644 --- a/ecmascript/parser/tests/typescript/custom/issue-410-1/input.ts +++ b/ecmascript/parser/tests/typescript/custom/issue-410-1/input.ts @@ -1 +1,3 @@ (async () => {})(); +(async async => {})(); +(async function () {})(); diff --git a/ecmascript/parser/tests/typescript/custom/issue-410-1/input.ts.json b/ecmascript/parser/tests/typescript/custom/issue-410-1/input.ts.json index 5651e568267..4a289faaaf7 100644 --- a/ecmascript/parser/tests/typescript/custom/issue-410-1/input.ts.json +++ b/ecmascript/parser/tests/typescript/custom/issue-410-1/input.ts.json @@ -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 diff --git a/ecmascript/parser/tests/typescript/custom/issue-410-2/input.ts b/ecmascript/parser/tests/typescript/custom/issue-410-2/input.ts index b659bfbb7e6..7c2163249e4 100644 --- a/ecmascript/parser/tests/typescript/custom/issue-410-2/input.ts +++ b/ecmascript/parser/tests/typescript/custom/issue-410-2/input.ts @@ -1 +1,3 @@ async () => {}; +async (async) => {}; +async function async() {} diff --git a/ecmascript/parser/tests/typescript/custom/issue-410-2/input.ts.json b/ecmascript/parser/tests/typescript/custom/issue-410-2/input.ts.json index 593821f040b..e7854887304 100644 --- a/ecmascript/parser/tests/typescript/custom/issue-410-2/input.ts.json +++ b/ecmascript/parser/tests/typescript/custom/issue-410-2/input.ts.json @@ -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