Skip to content

Commit

Permalink
fix(es/parser): Allow async in TsAsExpr (#2395)
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane authored Oct 10, 2021
1 parent fd08a27 commit 4458f9c
Show file tree
Hide file tree
Showing 5 changed files with 356 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ecmascript/parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion ecmascript/parser/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,21 @@ impl<'a, I: Tokens> Parser<I> {
}

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)?;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(async as any);
(async as async as async);
async as any;
async as async as async;

(async as => {});
async as => {};
333 changes: 333 additions & 0 deletions ecmascript/parser/tests/typescript/custom/async-with-as/input.ts.json
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 4458f9c

Please sign in to comment.