From d164f812d5910c7160a20e210f994088f7fdbb76 Mon Sep 17 00:00:00 2001 From: magic-akari Date: Fri, 24 Jan 2025 15:09:57 +0800 Subject: [PATCH 1/3] fix(es/parser): Parse `yield (v: T)=>v` --- crates/swc_ecma_parser/src/parser/expr.rs | 47 +++++++++++------------ 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/crates/swc_ecma_parser/src/parser/expr.rs b/crates/swc_ecma_parser/src/parser/expr.rs index d727473527a9..d782df1d3784 100644 --- a/crates/swc_ecma_parser/src/parser/expr.rs +++ b/crates/swc_ecma_parser/src/parser/expr.rs @@ -39,34 +39,32 @@ impl Parser { pub(super) fn parse_assignment_expr(&mut self) -> PResult> { trace_cur!(self, parse_assignment_expr); - if self.input.syntax().typescript() && self.input.syntax().jsx() { + if self.input.syntax().typescript() && is!(self, JSXTagStart) { // Note: When the JSX plugin is on, type assertions (` x`) aren't valid // syntax. - if is!(self, JSXTagStart) { - let cur_context = self.input.token_context().current(); - debug_assert_eq!(cur_context, Some(TokenContext::JSXOpeningTag)); - // Only time j_oTag is pushed is right after j_expr. + let cur_context = self.input.token_context().current(); + debug_assert_eq!(cur_context, Some(TokenContext::JSXOpeningTag)); + // Only time j_oTag is pushed is right after j_expr. + debug_assert_eq!( + self.input.token_context().0[self.input.token_context().len() - 2], + TokenContext::JSXExpr + ); + + let res = self.try_parse_ts(|p| p.parse_assignment_expr_base().map(Some)); + if let Some(res) = res { + return Ok(res); + } else { debug_assert_eq!( - self.input.token_context().0[self.input.token_context().len() - 2], - TokenContext::JSXExpr + self.input.token_context().current(), + Some(TokenContext::JSXOpeningTag) ); - - let res = self.try_parse_ts(|p| p.parse_assignment_expr_base().map(Some)); - if let Some(res) = res { - return Ok(res); - } else { - debug_assert_eq!( - self.input.token_context().current(), - Some(TokenContext::JSXOpeningTag) - ); - self.input.token_context_mut().pop(); - debug_assert_eq!( - self.input.token_context().current(), - Some(TokenContext::JSXExpr) - ); - self.input.token_context_mut().pop(); - } + self.input.token_context_mut().pop(); + debug_assert_eq!( + self.input.token_context().current(), + Some(TokenContext::JSXExpr) + ); + self.input.token_context_mut().pop(); } } @@ -2004,7 +2002,8 @@ impl Parser { } if is!(self, ';') - || (!is!(self, '*') + || (!is!(self, '<') + && !is!(self, '*') && !is!(self, '/') && !is!(self, "/=") && !cur!(self, false) From b9ec18d620226f66e5549333cfb3591503264fb4 Mon Sep 17 00:00:00 2001 From: magic-akari Date: Fri, 24 Jan 2025 15:11:09 +0800 Subject: [PATCH 2/3] chore: update test cases --- .../tests/typescript/issue-9914/1/input.ts | 3 + .../typescript/issue-9914/1/input.ts.json | 153 ++++++++++++++++++ .../tests/typescript/issue-9914/2/input.ts | 3 + .../typescript/issue-9914/2/input.ts.json | 153 ++++++++++++++++++ 4 files changed, 312 insertions(+) create mode 100644 crates/swc_ecma_parser/tests/typescript/issue-9914/1/input.ts create mode 100644 crates/swc_ecma_parser/tests/typescript/issue-9914/1/input.ts.json create mode 100644 crates/swc_ecma_parser/tests/typescript/issue-9914/2/input.ts create mode 100644 crates/swc_ecma_parser/tests/typescript/issue-9914/2/input.ts.json diff --git a/crates/swc_ecma_parser/tests/typescript/issue-9914/1/input.ts b/crates/swc_ecma_parser/tests/typescript/issue-9914/1/input.ts new file mode 100644 index 000000000000..1c8da84e0dc5 --- /dev/null +++ b/crates/swc_ecma_parser/tests/typescript/issue-9914/1/input.ts @@ -0,0 +1,3 @@ +(function* () { + yield (v: T) => v; +}); \ No newline at end of file diff --git a/crates/swc_ecma_parser/tests/typescript/issue-9914/1/input.ts.json b/crates/swc_ecma_parser/tests/typescript/issue-9914/1/input.ts.json new file mode 100644 index 000000000000..dca95c236c35 --- /dev/null +++ b/crates/swc_ecma_parser/tests/typescript/issue-9914/1/input.ts.json @@ -0,0 +1,153 @@ +{ + "type": "Script", + "span": { + "start": 1, + "end": 46 + }, + "body": [ + { + "type": "ExpressionStatement", + "span": { + "start": 1, + "end": 46 + }, + "expression": { + "type": "ParenthesisExpression", + "span": { + "start": 1, + "end": 45 + }, + "expression": { + "type": "FunctionExpression", + "identifier": null, + "params": [], + "decorators": [], + "span": { + "start": 2, + "end": 44 + }, + "ctxt": 0, + "body": { + "type": "BlockStatement", + "span": { + "start": 15, + "end": 44 + }, + "ctxt": 0, + "stmts": [ + { + "type": "ExpressionStatement", + "span": { + "start": 21, + "end": 42 + }, + "expression": { + "type": "YieldExpression", + "span": { + "start": 21, + "end": 41 + }, + "argument": { + "type": "ArrowFunctionExpression", + "span": { + "start": 27, + "end": 41 + }, + "ctxt": 0, + "params": [ + { + "type": "Identifier", + "span": { + "start": 31, + "end": 35 + }, + "ctxt": 0, + "value": "v", + "optional": false, + "typeAnnotation": { + "type": "TsTypeAnnotation", + "span": { + "start": 32, + "end": 35 + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 34, + "end": 35 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 34, + "end": 35 + }, + "ctxt": 0, + "value": "T", + "optional": false + }, + "typeParams": null + } + } + } + ], + "body": { + "type": "Identifier", + "span": { + "start": 40, + "end": 41 + }, + "ctxt": 0, + "value": "v", + "optional": false + }, + "async": false, + "generator": false, + "typeParameters": { + "type": "TsTypeParameterDeclaration", + "span": { + "start": 27, + "end": 30 + }, + "parameters": [ + { + "type": "TsTypeParameter", + "span": { + "start": 28, + "end": 29 + }, + "name": { + "type": "Identifier", + "span": { + "start": 28, + "end": 29 + }, + "ctxt": 0, + "value": "T", + "optional": false + }, + "in": false, + "out": false, + "const": false, + "constraint": null, + "default": null + } + ] + }, + "returnType": null + }, + "delegate": false + } + } + ] + }, + "generator": true, + "async": false, + "typeParameters": null, + "returnType": null + } + } + } + ], + "interpreter": null +} diff --git a/crates/swc_ecma_parser/tests/typescript/issue-9914/2/input.ts b/crates/swc_ecma_parser/tests/typescript/issue-9914/2/input.ts new file mode 100644 index 000000000000..77eb305ad98a --- /dev/null +++ b/crates/swc_ecma_parser/tests/typescript/issue-9914/2/input.ts @@ -0,0 +1,3 @@ +(function* () { + yield (v: T) => v; +}); \ No newline at end of file diff --git a/crates/swc_ecma_parser/tests/typescript/issue-9914/2/input.ts.json b/crates/swc_ecma_parser/tests/typescript/issue-9914/2/input.ts.json new file mode 100644 index 000000000000..69a052620ad9 --- /dev/null +++ b/crates/swc_ecma_parser/tests/typescript/issue-9914/2/input.ts.json @@ -0,0 +1,153 @@ +{ + "type": "Script", + "span": { + "start": 1, + "end": 47 + }, + "body": [ + { + "type": "ExpressionStatement", + "span": { + "start": 1, + "end": 47 + }, + "expression": { + "type": "ParenthesisExpression", + "span": { + "start": 1, + "end": 46 + }, + "expression": { + "type": "FunctionExpression", + "identifier": null, + "params": [], + "decorators": [], + "span": { + "start": 2, + "end": 45 + }, + "ctxt": 0, + "body": { + "type": "BlockStatement", + "span": { + "start": 15, + "end": 45 + }, + "ctxt": 0, + "stmts": [ + { + "type": "ExpressionStatement", + "span": { + "start": 21, + "end": 43 + }, + "expression": { + "type": "YieldExpression", + "span": { + "start": 21, + "end": 42 + }, + "argument": { + "type": "ArrowFunctionExpression", + "span": { + "start": 27, + "end": 42 + }, + "ctxt": 0, + "params": [ + { + "type": "Identifier", + "span": { + "start": 32, + "end": 36 + }, + "ctxt": 0, + "value": "v", + "optional": false, + "typeAnnotation": { + "type": "TsTypeAnnotation", + "span": { + "start": 33, + "end": 36 + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 35, + "end": 36 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 35, + "end": 36 + }, + "ctxt": 0, + "value": "T", + "optional": false + }, + "typeParams": null + } + } + } + ], + "body": { + "type": "Identifier", + "span": { + "start": 41, + "end": 42 + }, + "ctxt": 0, + "value": "v", + "optional": false + }, + "async": false, + "generator": false, + "typeParameters": { + "type": "TsTypeParameterDeclaration", + "span": { + "start": 27, + "end": 31 + }, + "parameters": [ + { + "type": "TsTypeParameter", + "span": { + "start": 28, + "end": 29 + }, + "name": { + "type": "Identifier", + "span": { + "start": 28, + "end": 29 + }, + "ctxt": 0, + "value": "T", + "optional": false + }, + "in": false, + "out": false, + "const": false, + "constraint": null, + "default": null + } + ] + }, + "returnType": null + }, + "delegate": false + } + } + ] + }, + "generator": true, + "async": false, + "typeParameters": null, + "returnType": null + } + } + } + ], + "interpreter": null +} From b5ba00b89711a38d9e647b88f25d15b28800d3ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 24 Jan 2025 19:16:06 +0900 Subject: [PATCH 3/3] Create pink-kids-perform.md --- .changeset/pink-kids-perform.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/pink-kids-perform.md diff --git a/.changeset/pink-kids-perform.md b/.changeset/pink-kids-perform.md new file mode 100644 index 000000000000..e658e090b818 --- /dev/null +++ b/.changeset/pink-kids-perform.md @@ -0,0 +1,6 @@ +--- +swc_core: patch +swc_ecma_parser: patch +--- + +fix(es/parser): Parse `yield (v: T)=>v`