From 8c322250b7276f002fc0848bbb5c53bdb8802c00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Wed, 31 Jan 2024 17:48:30 +0900 Subject: [PATCH] fix(es/codegen): Fix codegen of async methods with decorators (#8575) **Related issue:** - Closes #4311 --- .../fixture/issues-4xxx/4311/input/.swcrc | 22 ++++++++++++++ .../tests/fixture/issues-4xxx/4311/input/1.js | 24 +++++++++++++++ .../fixture/issues-4xxx/4311/output/1.js | 30 +++++++++++++++++++ crates/swc_ecma_codegen/src/lib.rs | 2 ++ 4 files changed, 78 insertions(+) create mode 100644 crates/swc/tests/fixture/issues-4xxx/4311/input/.swcrc create mode 100644 crates/swc/tests/fixture/issues-4xxx/4311/input/1.js create mode 100644 crates/swc/tests/fixture/issues-4xxx/4311/output/1.js diff --git a/crates/swc/tests/fixture/issues-4xxx/4311/input/.swcrc b/crates/swc/tests/fixture/issues-4xxx/4311/input/.swcrc new file mode 100644 index 00000000000..6b57dfd8da7 --- /dev/null +++ b/crates/swc/tests/fixture/issues-4xxx/4311/input/.swcrc @@ -0,0 +1,22 @@ +{ + "jsc": { + "parser": { + "syntax": "typescript", + "decorators": true, + "tsx": false, + "keepClassNames": true + }, + "target": "es2020", + "preserveAllComments": true, + "loose": false, + "minify": { + "compress": false, + "mangle": false + } + }, + "module": { + "type": "commonjs" + }, + "minify": false, + "isModule": true +} \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-4xxx/4311/input/1.js b/crates/swc/tests/fixture/issues-4xxx/4311/input/1.js new file mode 100644 index 00000000000..7eb3ed65b40 --- /dev/null +++ b/crates/swc/tests/fixture/issues-4xxx/4311/input/1.js @@ -0,0 +1,24 @@ +const printMemberName = (target: any, memberName: string) => { + console.log(memberName); +}; + +class TestClass { + + + // moving the decorator below the comment works as expected + + @printMemberName + /** + * some tsdoc comments + * + * Some more comments + * over + * multiple + * lines + */ + async run(): Promise { + return await Promise.resolve(true); + } +} + +export { TestClass }; diff --git a/crates/swc/tests/fixture/issues-4xxx/4311/output/1.js b/crates/swc/tests/fixture/issues-4xxx/4311/output/1.js new file mode 100644 index 00000000000..a122eb0e46c --- /dev/null +++ b/crates/swc/tests/fixture/issues-4xxx/4311/output/1.js @@ -0,0 +1,30 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { + value: true +}); +Object.defineProperty(exports, "TestClass", { + enumerable: true, + get: function() { + return TestClass; + } +}); +const _ts_decorate = require("@swc/helpers/_/_ts_decorate"); +const printMemberName = (target, memberName)=>{ + console.log(memberName); +}; +class TestClass { + // moving the decorator below the comment works as expected + /** + * some tsdoc comments + * + * Some more comments + * over + * multiple + * lines + */ async run() { + return await Promise.resolve(true); + } +} +_ts_decorate._([ + printMemberName +], TestClass.prototype, "run", null); diff --git a/crates/swc_ecma_codegen/src/lib.rs b/crates/swc_ecma_codegen/src/lib.rs index 10d648f5093..5bde5a726cf 100644 --- a/crates/swc_ecma_codegen/src/lib.rs +++ b/crates/swc_ecma_codegen/src/lib.rs @@ -1474,6 +1474,8 @@ where fn emit_class_method(&mut self, n: &ClassMethod) -> Result { self.emit_leading_comments_of_span(n.span(), false)?; + self.emit_leading_comments_of_span(n.key.span(), false)?; + srcmap!(n, true); for d in &n.function.decorators {