From 77900d808e2a3be2e62b74022c88a81b26a73f86 Mon Sep 17 00:00:00 2001 From: CPunisher <1343316114@qq.com> Date: Tue, 1 Oct 2024 10:41:58 +0800 Subject: [PATCH] fix(es/parser): Fix failure of TS instantiation followed by satisfies (#9583) **Related issue:** it's already ok when followed by `as`. Closes https://github.com/swc-project/swc/issues/8627 --- .changeset/forty-rats-flow.md | 6 + crates/swc_ecma_parser/src/parser/expr.rs | 2 +- .../tests/typescript/issue-8627/1/input.ts | 3 + .../typescript/issue-8627/1/input.ts.json | 122 ++++++++++++++++++ .../tests/typescript/issue-8627/2/input.ts | 3 + .../typescript/issue-8627/2/input.ts.json | 122 ++++++++++++++++++ 6 files changed, 257 insertions(+), 1 deletion(-) create mode 100644 .changeset/forty-rats-flow.md create mode 100644 crates/swc_ecma_parser/tests/typescript/issue-8627/1/input.ts create mode 100644 crates/swc_ecma_parser/tests/typescript/issue-8627/1/input.ts.json create mode 100644 crates/swc_ecma_parser/tests/typescript/issue-8627/2/input.ts create mode 100644 crates/swc_ecma_parser/tests/typescript/issue-8627/2/input.ts.json diff --git a/.changeset/forty-rats-flow.md b/.changeset/forty-rats-flow.md new file mode 100644 index 00000000000..a1a58c1d312 --- /dev/null +++ b/.changeset/forty-rats-flow.md @@ -0,0 +1,6 @@ +--- +swc_ecma_parser: patch +swc_core: patch +--- + +fix(es/parser): Fix failure of TS instantiation followed by satisfies diff --git a/crates/swc_ecma_parser/src/parser/expr.rs b/crates/swc_ecma_parser/src/parser/expr.rs index 6f8015fa3f2..bea774b12e5 100644 --- a/crates/swc_ecma_parser/src/parser/expr.rs +++ b/crates/swc_ecma_parser/src/parser/expr.rs @@ -1245,7 +1245,7 @@ impl Parser { ) .map(|expr| (expr.into(), true)) .map(Some) - } else if is_one_of!(p, '=', "as") { + } else if is_one_of!(p, '=', "as", "satisfies") { Ok(Some(( TsInstantiation { span: span!(p, start), diff --git a/crates/swc_ecma_parser/tests/typescript/issue-8627/1/input.ts b/crates/swc_ecma_parser/tests/typescript/issue-8627/1/input.ts new file mode 100644 index 00000000000..1d369de6304 --- /dev/null +++ b/crates/swc_ecma_parser/tests/typescript/issue-8627/1/input.ts @@ -0,0 +1,3 @@ +class Module {} + +export default Module as unknown; diff --git a/crates/swc_ecma_parser/tests/typescript/issue-8627/1/input.ts.json b/crates/swc_ecma_parser/tests/typescript/issue-8627/1/input.ts.json new file mode 100644 index 00000000000..90fb2e5cb57 --- /dev/null +++ b/crates/swc_ecma_parser/tests/typescript/issue-8627/1/input.ts.json @@ -0,0 +1,122 @@ +{ + "type": "Module", + "span": { + "start": 1, + "end": 62 + }, + "body": [ + { + "type": "ClassDeclaration", + "identifier": { + "type": "Identifier", + "span": { + "start": 7, + "end": 13 + }, + "ctxt": 0, + "value": "Module", + "optional": false + }, + "declare": false, + "span": { + "start": 1, + "end": 19 + }, + "ctxt": 0, + "decorators": [], + "body": [], + "superClass": null, + "isAbstract": false, + "typeParams": { + "type": "TsTypeParameterDeclaration", + "span": { + "start": 13, + "end": 16 + }, + "parameters": [ + { + "type": "TsTypeParameter", + "span": { + "start": 14, + "end": 15 + }, + "name": { + "type": "Identifier", + "span": { + "start": 14, + "end": 15 + }, + "ctxt": 0, + "value": "T", + "optional": false + }, + "in": false, + "out": false, + "const": false, + "constraint": null, + "default": null + } + ] + }, + "superTypeParams": null, + "implements": [] + }, + { + "type": "ExportDefaultExpression", + "span": { + "start": 21, + "end": 62 + }, + "expression": { + "type": "TsAsExpression", + "span": { + "start": 36, + "end": 61 + }, + "expression": { + "type": "TsInstantiation", + "span": { + "start": 36, + "end": 50 + }, + "expression": { + "type": "Identifier", + "span": { + "start": 36, + "end": 42 + }, + "ctxt": 0, + "value": "Module", + "optional": false + }, + "typeArguments": { + "type": "TsTypeParameterInstantiation", + "span": { + "start": 42, + "end": 50 + }, + "params": [ + { + "type": "TsKeywordType", + "span": { + "start": 43, + "end": 49 + }, + "kind": "number" + } + ] + } + }, + "typeAnnotation": { + "type": "TsKeywordType", + "span": { + "start": 54, + "end": 61 + }, + "kind": "unknown" + } + } + } + ], + "interpreter": null +} diff --git a/crates/swc_ecma_parser/tests/typescript/issue-8627/2/input.ts b/crates/swc_ecma_parser/tests/typescript/issue-8627/2/input.ts new file mode 100644 index 00000000000..6cd3b3e72b2 --- /dev/null +++ b/crates/swc_ecma_parser/tests/typescript/issue-8627/2/input.ts @@ -0,0 +1,3 @@ +class Module {} + +export default Module satisfies unknown; diff --git a/crates/swc_ecma_parser/tests/typescript/issue-8627/2/input.ts.json b/crates/swc_ecma_parser/tests/typescript/issue-8627/2/input.ts.json new file mode 100644 index 00000000000..165f7c36af3 --- /dev/null +++ b/crates/swc_ecma_parser/tests/typescript/issue-8627/2/input.ts.json @@ -0,0 +1,122 @@ +{ + "type": "Module", + "span": { + "start": 1, + "end": 69 + }, + "body": [ + { + "type": "ClassDeclaration", + "identifier": { + "type": "Identifier", + "span": { + "start": 7, + "end": 13 + }, + "ctxt": 0, + "value": "Module", + "optional": false + }, + "declare": false, + "span": { + "start": 1, + "end": 19 + }, + "ctxt": 0, + "decorators": [], + "body": [], + "superClass": null, + "isAbstract": false, + "typeParams": { + "type": "TsTypeParameterDeclaration", + "span": { + "start": 13, + "end": 16 + }, + "parameters": [ + { + "type": "TsTypeParameter", + "span": { + "start": 14, + "end": 15 + }, + "name": { + "type": "Identifier", + "span": { + "start": 14, + "end": 15 + }, + "ctxt": 0, + "value": "T", + "optional": false + }, + "in": false, + "out": false, + "const": false, + "constraint": null, + "default": null + } + ] + }, + "superTypeParams": null, + "implements": [] + }, + { + "type": "ExportDefaultExpression", + "span": { + "start": 21, + "end": 69 + }, + "expression": { + "type": "TsSatisfiesExpression", + "span": { + "start": 36, + "end": 68 + }, + "expression": { + "type": "TsInstantiation", + "span": { + "start": 36, + "end": 50 + }, + "expression": { + "type": "Identifier", + "span": { + "start": 36, + "end": 42 + }, + "ctxt": 0, + "value": "Module", + "optional": false + }, + "typeArguments": { + "type": "TsTypeParameterInstantiation", + "span": { + "start": 42, + "end": 50 + }, + "params": [ + { + "type": "TsKeywordType", + "span": { + "start": 43, + "end": 49 + }, + "kind": "number" + } + ] + } + }, + "typeAnnotation": { + "type": "TsKeywordType", + "span": { + "start": 61, + "end": 68 + }, + "kind": "unknown" + } + } + } + ], + "interpreter": null +}