Skip to content

Commit

Permalink
fix(es/codegen): Fix codegen of async methods with decorators (#8575)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #4311
  • Loading branch information
kdy1 authored Jan 31, 2024
1 parent d006482 commit 8c32225
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
22 changes: 22 additions & 0 deletions crates/swc/tests/fixture/issues-4xxx/4311/input/.swcrc
Original file line number Diff line number Diff line change
@@ -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
}
24 changes: 24 additions & 0 deletions crates/swc/tests/fixture/issues-4xxx/4311/input/1.js
Original file line number Diff line number Diff line change
@@ -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<boolean> {
return await Promise.resolve(true);
}
}

export { TestClass };
30 changes: 30 additions & 0 deletions crates/swc/tests/fixture/issues-4xxx/4311/output/1.js
Original file line number Diff line number Diff line change
@@ -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);
2 changes: 2 additions & 0 deletions crates/swc_ecma_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 8c32225

Please sign in to comment.