From fa7a3521f531caa32439fa6f4c338a2a7d859e05 Mon Sep 17 00:00:00 2001 From: Austaras Date: Sun, 14 May 2023 13:21:13 +0800 Subject: [PATCH] fix(es/parser): Fix `>=` with type instantiate (#7388) --- crates/swc_ecma_parser/src/macros.rs | 5 +- .../swc_ecma_parser/src/parser/typescript.rs | 2 +- .../tests/typescript/issue-7387/input.ts | 1 + .../tests/typescript/issue-7387/input.ts.json | 104 ++++++++++++++++++ 4 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 crates/swc_ecma_parser/tests/typescript/issue-7387/input.ts create mode 100644 crates/swc_ecma_parser/tests/typescript/issue-7387/input.ts.json diff --git a/crates/swc_ecma_parser/src/macros.rs b/crates/swc_ecma_parser/src/macros.rs index dabf96cc3df1..389c9018cbf7 100644 --- a/crates/swc_ecma_parser/src/macros.rs +++ b/crates/swc_ecma_parser/src/macros.rs @@ -102,7 +102,7 @@ macro_rules! tok { crate::token::Token::AssignOp(crate::token::AssignOpToken::DivAssign) }; ('%') => { - crate::token::Token::BinOp(Mod) + crate::token::Token::BinOp(crate::token::BinOpToken::Mod) }; ('~') => { crate::token::Token::Tilde @@ -116,6 +116,9 @@ macro_rules! tok { (">>") => { crate::token::Token::BinOp(crate::token::BinOpToken::RShift) }; + (">=") => { + crate::token::Token::BinOp(crate::token::BinOpToken::GtEq) + }; ("++") => { crate::token::Token::PlusPlus diff --git a/crates/swc_ecma_parser/src/parser/typescript.rs b/crates/swc_ecma_parser/src/parser/typescript.rs index 790618a28ef2..a0cdb9683e8a 100644 --- a/crates/swc_ecma_parser/src/parser/typescript.rs +++ b/crates/swc_ecma_parser/src/parser/typescript.rs @@ -576,7 +576,7 @@ impl Parser { if is_one_of!( p, '<', // invalid syntax - '>', ">>", '+', '-', // becomes relational expression + '>', '=', ">>", ">=", '+', '-', // becomes relational expression /* these should be type arguments in function call or template, * not instantiation expression */ '(', '`' diff --git a/crates/swc_ecma_parser/tests/typescript/issue-7387/input.ts b/crates/swc_ecma_parser/tests/typescript/issue-7387/input.ts new file mode 100644 index 000000000000..ff3a04321c91 --- /dev/null +++ b/crates/swc_ecma_parser/tests/typescript/issue-7387/input.ts @@ -0,0 +1 @@ +fn(x < y, x >= y1) diff --git a/crates/swc_ecma_parser/tests/typescript/issue-7387/input.ts.json b/crates/swc_ecma_parser/tests/typescript/issue-7387/input.ts.json new file mode 100644 index 000000000000..ea46ed1339db --- /dev/null +++ b/crates/swc_ecma_parser/tests/typescript/issue-7387/input.ts.json @@ -0,0 +1,104 @@ +{ + "type": "Script", + "span": { + "start": 1, + "end": 19, + "ctxt": 0 + }, + "body": [ + { + "type": "ExpressionStatement", + "span": { + "start": 1, + "end": 19, + "ctxt": 0 + }, + "expression": { + "type": "CallExpression", + "span": { + "start": 1, + "end": 19, + "ctxt": 0 + }, + "callee": { + "type": "Identifier", + "span": { + "start": 1, + "end": 3, + "ctxt": 0 + }, + "value": "fn", + "optional": false + }, + "arguments": [ + { + "spread": null, + "expression": { + "type": "BinaryExpression", + "span": { + "start": 4, + "end": 9, + "ctxt": 0 + }, + "operator": "<", + "left": { + "type": "Identifier", + "span": { + "start": 4, + "end": 5, + "ctxt": 0 + }, + "value": "x", + "optional": false + }, + "right": { + "type": "Identifier", + "span": { + "start": 8, + "end": 9, + "ctxt": 0 + }, + "value": "y", + "optional": false + } + } + }, + { + "spread": null, + "expression": { + "type": "BinaryExpression", + "span": { + "start": 11, + "end": 18, + "ctxt": 0 + }, + "operator": ">=", + "left": { + "type": "Identifier", + "span": { + "start": 11, + "end": 12, + "ctxt": 0 + }, + "value": "x", + "optional": false + }, + "right": { + "type": "Identifier", + "span": { + "start": 16, + "end": 18, + "ctxt": 0 + }, + "value": "y1", + "optional": false + } + } + } + ], + "typeArguments": null + } + } + ], + "interpreter": null +}