diff --git a/Cargo.lock b/Cargo.lock index 8c23440bc892..b69aff86d40b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2687,7 +2687,7 @@ dependencies = [ [[package]] name = "swc_ecma_parser" -version = "0.73.6" +version = "0.73.7" dependencies = [ "either", "enum_kind", diff --git a/ecmascript/parser/Cargo.toml b/ecmascript/parser/Cargo.toml index b866f45bad92..10703e737d39 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.6" +version = "0.73.7" [package.metadata.docs.rs] all-features = true diff --git a/ecmascript/parser/src/parser/expr.rs b/ecmascript/parser/src/parser/expr.rs index 110450563285..7f501544cf07 100644 --- a/ecmascript/parser/src/parser/expr.rs +++ b/ecmascript/parser/src/parser/expr.rs @@ -379,8 +379,21 @@ impl<'a, I: Tokens> Parser { } if can_be_arrow && id.sym == js_word!("async") && is!(self, BindingIdent) { + let ident = self.parse_binding_ident()?; + if self.input.syntax().typescript() + && ident.id.sym == js_word!("as") + && !is!(self, "=>") + { + // async as type + let type_ann = self.in_type().parse_with(|p| p.parse_ts_type())?; + return Ok(Box::new(Expr::TsAs(TsAsExpr { + span: span!(self, start), + expr: Box::new(Expr::Ident(id)), + type_ann, + }))); + } // async a => body - let arg = self.parse_binding_ident().map(Pat::from)?; + let arg = Pat::from(ident); let params = vec![arg]; expect!(self, "=>"); let body = self.parse_fn_body(true, false)?; diff --git a/ecmascript/parser/tests/typescript/custom/async-with-as/input.ts b/ecmascript/parser/tests/typescript/custom/async-with-as/input.ts new file mode 100644 index 000000000000..f53ac86c6347 --- /dev/null +++ b/ecmascript/parser/tests/typescript/custom/async-with-as/input.ts @@ -0,0 +1,7 @@ +(async as any); +(async as async as async); +async as any; +async as async as async; + +(async as => {}); +async as => {}; diff --git a/ecmascript/parser/tests/typescript/custom/async-with-as/input.ts.json b/ecmascript/parser/tests/typescript/custom/async-with-as/input.ts.json new file mode 100644 index 000000000000..eac9767f71bb --- /dev/null +++ b/ecmascript/parser/tests/typescript/custom/async-with-as/input.ts.json @@ -0,0 +1,333 @@ +{ + "type": "Script", + "span": { + "start": 0, + "end": 116, + "ctxt": 0 + }, + "body": [ + { + "type": "ExpressionStatement", + "span": { + "start": 0, + "end": 15, + "ctxt": 0 + }, + "expression": { + "type": "ParenthesisExpression", + "span": { + "start": 0, + "end": 14, + "ctxt": 0 + }, + "expression": { + "type": "TsAsExpression", + "span": { + "start": 1, + "end": 13, + "ctxt": 0 + }, + "expression": { + "type": "Identifier", + "span": { + "start": 1, + "end": 6, + "ctxt": 0 + }, + "value": "async", + "optional": false + }, + "typeAnnotation": { + "type": "TsKeywordType", + "span": { + "start": 10, + "end": 13, + "ctxt": 0 + }, + "kind": "any" + } + } + } + }, + { + "type": "ExpressionStatement", + "span": { + "start": 16, + "end": 42, + "ctxt": 0 + }, + "expression": { + "type": "ParenthesisExpression", + "span": { + "start": 16, + "end": 41, + "ctxt": 0 + }, + "expression": { + "type": "TsAsExpression", + "span": { + "start": 17, + "end": 40, + "ctxt": 0 + }, + "expression": { + "type": "TsAsExpression", + "span": { + "start": 17, + "end": 31, + "ctxt": 0 + }, + "expression": { + "type": "Identifier", + "span": { + "start": 17, + "end": 22, + "ctxt": 0 + }, + "value": "async", + "optional": false + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 26, + "end": 31, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 26, + "end": 31, + "ctxt": 0 + }, + "value": "async", + "optional": false + }, + "typeParams": null + } + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 35, + "end": 40, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 35, + "end": 40, + "ctxt": 0 + }, + "value": "async", + "optional": false + }, + "typeParams": null + } + } + } + }, + { + "type": "ExpressionStatement", + "span": { + "start": 43, + "end": 56, + "ctxt": 0 + }, + "expression": { + "type": "TsAsExpression", + "span": { + "start": 43, + "end": 55, + "ctxt": 0 + }, + "expression": { + "type": "Identifier", + "span": { + "start": 43, + "end": 48, + "ctxt": 0 + }, + "value": "async", + "optional": false + }, + "typeAnnotation": { + "type": "TsKeywordType", + "span": { + "start": 52, + "end": 55, + "ctxt": 0 + }, + "kind": "any" + } + } + }, + { + "type": "ExpressionStatement", + "span": { + "start": 57, + "end": 81, + "ctxt": 0 + }, + "expression": { + "type": "TsAsExpression", + "span": { + "start": 57, + "end": 80, + "ctxt": 0 + }, + "expression": { + "type": "TsAsExpression", + "span": { + "start": 57, + "end": 71, + "ctxt": 0 + }, + "expression": { + "type": "Identifier", + "span": { + "start": 57, + "end": 62, + "ctxt": 0 + }, + "value": "async", + "optional": false + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 66, + "end": 71, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 66, + "end": 71, + "ctxt": 0 + }, + "value": "async", + "optional": false + }, + "typeParams": null + } + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 75, + "end": 80, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 75, + "end": 80, + "ctxt": 0 + }, + "value": "async", + "optional": false + }, + "typeParams": null + } + } + }, + { + "type": "ExpressionStatement", + "span": { + "start": 83, + "end": 100, + "ctxt": 0 + }, + "expression": { + "type": "ParenthesisExpression", + "span": { + "start": 83, + "end": 99, + "ctxt": 0 + }, + "expression": { + "type": "ArrowFunctionExpression", + "span": { + "start": 84, + "end": 98, + "ctxt": 0 + }, + "params": [ + { + "type": "Identifier", + "span": { + "start": 90, + "end": 92, + "ctxt": 0 + }, + "value": "as", + "optional": false, + "typeAnnotation": null + } + ], + "body": { + "type": "BlockStatement", + "span": { + "start": 96, + "end": 98, + "ctxt": 0 + }, + "stmts": [] + }, + "async": true, + "generator": false, + "typeParameters": null, + "returnType": null + } + } + }, + { + "type": "ExpressionStatement", + "span": { + "start": 101, + "end": 116, + "ctxt": 0 + }, + "expression": { + "type": "ArrowFunctionExpression", + "span": { + "start": 101, + "end": 115, + "ctxt": 0 + }, + "params": [ + { + "type": "Identifier", + "span": { + "start": 107, + "end": 109, + "ctxt": 0 + }, + "value": "as", + "optional": false, + "typeAnnotation": null + } + ], + "body": { + "type": "BlockStatement", + "span": { + "start": 113, + "end": 115, + "ctxt": 0 + }, + "stmts": [] + }, + "async": true, + "generator": false, + "typeParameters": null, + "returnType": null + } + } + ], + "interpreter": null +}