Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/beige-cheetahs-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_core: patch
swc_ecma_transforms_proposal: patch
---

fix(es/transforms): rewrite class references in non-static members
19 changes: 19 additions & 0 deletions crates/swc/tests/fixture/issues-11xxx/11744/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03",
"legacyDecorator": false
},
"target": "es2022",
"loose": false,
"minify": {
"compress": false
}
},
"isModule": true,
"minify": false
}
13 changes: 13 additions & 0 deletions crates/swc/tests/fixture/issues-11xxx/11744/input/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function Component(target) {
return class subTarget extends target { };
}

@Component
class ScrollView {
static scrollInterval = 100;
autoScroll() {
console.log(ScrollView.scrollInterval)
}
}

new ScrollView().autoScroll();
25 changes: 25 additions & 0 deletions crates/swc/tests/fixture/issues-11xxx/11744/output/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { _ as _apply_decs_2203_r } from "@swc/helpers/_/_apply_decs_2203_r";
import { _ as _identity } from "@swc/helpers/_/_identity";
var _initClass;
function Component(target) {
return class subTarget extends target {
};
}
let _ScrollView;
new class extends _identity {
constructor(){
super(_ScrollView), _initClass();
}
static [class ScrollView {
static{
({ c: [_ScrollView, _initClass] } = _apply_decs_2203_r(this, [], [
Component
]));
}
autoScroll() {
console.log(_ScrollView.scrollInterval);
}
}];
scrollInterval = 100;
}();
new _ScrollView().autoScroll();
4 changes: 2 additions & 2 deletions crates/swc_ecma_transforms_proposal/src/decorator_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1756,10 +1756,10 @@ impl DecoratorPass {
| ClassMember::AutoAccessor(..) => {
replace_ident(m, c.ident.to_id(), &new_class_name);
}
ClassMember::Method(method) if method.is_static => {
ClassMember::Method(method) => {
replace_ident(method, c.ident.to_id(), &new_class_name);
Comment on lines +1759 to 1760
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Skip renaming class refs in instance method keys

Removing the is_static guard here makes replace_ident run on the entire ClassMethod, which includes computed keys. For decorated class declarations with static members, an instance method like [@dec class C { static x = 1; [C]() {} }] now has its key rewritten to [_C], but _C is assigned later in the decorator init flow, so the computed key evaluates to undefined instead of the class reference. This regression is introduced by broadening the match from static-only methods to all methods.

Useful? React with 👍 / 👎.

}
ClassMember::PrivateMethod(method) if method.is_static => {
ClassMember::PrivateMethod(method) => {
replace_ident(method, c.ident.to_id(), &new_class_name);
}

Expand Down
Loading