Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(es/decorators): Fix syntax context of decorated classes #4905

Merged
merged 9 commits into from
Jun 8, 2022
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
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import _class_call_check from "@swc/helpers/lib/_class_call_check.js";
import _ts_decorate from "@swc/helpers/lib/_ts_decorate.js";
var _TestClass;
var TestClass = (_TestClass = function TestClass() {
var TestClass = (_TestClass = function TestClass1() {
"use strict";
_class_call_check(this, TestClass);
_class_call_check(this, TestClass1);
}, _TestClass.Something = "hello", _TestClass.SomeProperties = {
firstProp: _TestClass.Something
}, _TestClass);
Expand Down
22 changes: 22 additions & 0 deletions crates/swc/tests/fixture/issues-4xxx/4899/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"$schema": "http://json.schemastore.org/swcrc",
"exclude": "\\.js$",
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": false,
"decorators": true
},
"target": "es2020",
"loose": false,
"minify": {
"compress": false,
"mangle": false
}
},
"module": {
"type": "amd",
"noInterop": true
},
"minify": false
}
23 changes: 23 additions & 0 deletions crates/swc/tests/fixture/issues-4xxx/4899/input/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function es5ClassCompat(target: Function): any {
///@ts-expect-error
function _() {
return Reflect.construct(target, arguments, this.constructor);
}
Object.defineProperty(
_,
"name",
Object.getOwnPropertyDescriptor(target, "name")!
);
Object.setPrototypeOf(_, target);
Object.setPrototypeOf(_.prototype, target.prototype);
return _;
}

@es5ClassCompat
class Foo {
static create() {
return new Foo();
}

constructor() {}
}
24 changes: 24 additions & 0 deletions crates/swc/tests/fixture/issues-4xxx/4899/output/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
define([
"require",
"@swc/helpers/lib/_ts_decorate.js"
], function(require, _ts_decorate) {
"use strict";
function es5ClassCompat(target) {
function _() {
return Reflect.construct(target, arguments, this.constructor);
}
Object.defineProperty(_, "name", Object.getOwnPropertyDescriptor(target, "name"));
Object.setPrototypeOf(_, target);
Object.setPrototypeOf(_.prototype, target.prototype);
return _;
}
let Foo = class Foo1 {
static create() {
return new Foo();
}
constructor(){}
};
Foo = _ts_decorate([
es5ClassCompat
], Foo);
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
let TestClass = class TestClass {
let TestClass = class TestClass1 {
static Something = "hello";
static SomeProperties = {
firstProp: TestClass.Something
firstProp: TestClass1.Something
};
};
TestClass = __decorate([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use swc_ecma_ast::*;
use swc_ecma_transforms_base::helper;
use swc_ecma_utils::{
constructor::inject_after_super, default_constructor, private_ident, prop_name_to_expr_value,
quote_ident, undefined, ExprFactory, StmtLike,
quote_ident, replace_ident, undefined, ExprFactory, StmtLike,
};
use swc_ecma_visit::{Visit, VisitMut, VisitMutWith, VisitWith};

Expand Down Expand Up @@ -358,11 +358,26 @@ impl VisitMut for TscDecorator {
decl.visit_mut_with(self);

if convert_to_let {
let inner_ident = private_ident!(decl.ident.sym.clone());

decl.class.body.iter_mut().for_each(|m| match m {
ClassMember::PrivateProp(PrivateProp {
is_static: true, ..
})
| ClassMember::StaticBlock(..)
| ClassMember::ClassProp(ClassProp {
is_static: true, ..
}) => {
replace_ident(m, decl.ident.to_id(), &inner_ident);
}
_ => {}
});

let d = VarDeclarator {
span: DUMMY_SP,
name: decl.ident.clone().into(),
init: Some(Box::new(Expr::Class(ClassExpr {
ident: Some(decl.ident.clone()),
ident: Some(inner_ident),
class: decl.class.take(),
}))),
definite: Default::default(),
Expand Down
21 changes: 21 additions & 0 deletions crates/swc_ecma_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2292,6 +2292,27 @@ impl VisitMut for IdentReplacer<'_> {

visit_mut_obj_and_computed!();

fn visit_mut_prop(&mut self, node: &mut Prop) {
match node {
Prop::Shorthand(i) => {
let cloned = i.clone();
i.visit_mut_with(self);
if i.sym != cloned.sym || i.span.ctxt != cloned.span.ctxt {
*node = Prop::KeyValue(KeyValueProp {
key: PropName::Ident(Ident::new(
cloned.sym,
cloned.span.with_ctxt(SyntaxContext::empty()),
)),
value: Box::new(Expr::Ident(i.clone())),
});
}
}
_ => {
node.visit_mut_children_with(self);
}
}
}

fn visit_mut_ident(&mut self, node: &mut Ident) {
if node.sym == self.from.0 && node.span.ctxt == self.from.1 {
*node = self.to.clone();
Expand Down