Skip to content

Commit

Permalink
fix(es/transforms/proposal): Fix order of constructor statements (#1914)
Browse files Browse the repository at this point in the history
swc_ecma_transforms_proposal:
 - `decorators`: Fix order. (#1913)
  • Loading branch information
IronLu233 committed Jul 13, 2021
1 parent 4e42c66 commit d13eff9
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 4 deletions.
2 changes: 1 addition & 1 deletion ecmascript/transforms/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2018"
license = "Apache-2.0/MIT"
name = "swc_ecma_transforms"
repository = "https://github.com/swc-project/swc.git"
version = "0.58.0"
version = "0.58.1"

[package.metadata.docs.rs]
all-features = true
Expand Down
24 changes: 21 additions & 3 deletions ecmascript/transforms/proposal/src/decorators/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -773,12 +773,30 @@ impl Legacy {
});
}

constructor
let decorate_stmts_insert_position = constructor
.body
.as_mut()
.as_ref()
.unwrap()
.stmts
.extend(constructor_stmts)
.iter()
.position(|stmt| {
if let Stmt::Expr(expr) = stmt {
let expr = expr.expr.as_ref();
if let Expr::Call(call) = expr {
if let ExprOrSuper::Super(_) = call.callee {
return true;
}
}
}

return false;
})
.map_or(0, |p| p + 1);

constructor.body.as_mut().unwrap().stmts.splice(
decorate_stmts_insert_position..decorate_stmts_insert_position,
constructor_stmts,
);
}

let cls_assign = Box::new(Expr::Assign(AssignExpr {
Expand Down
82 changes: 82 additions & 0 deletions ecmascript/transforms/tests/decorators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5867,3 +5867,85 @@ test!(
console.log(new MyClass());
"
);

test!(
syntax(false),
|_| decorators(Config {
legacy: true,
..Default::default()
}),
issue_1913_1,
"
class Store {
constructor() {
this.doSomething();
}
@action
doSomething = () => {
console.log('run');
}
}",
"
var _class, _descriptor;
let Store = ((_class = class Store {
constructor(){
_initializerDefineProperty(this, 'doSomething', _descriptor, this);
this.doSomething();
}
}) || _class, _descriptor = _applyDecoratedDescriptor(_class.prototype, 'doSomething', [
action
], {
configurable: true,
enumerable: true,
writable: true,
initializer: function() {
return ()=>{
console.log('run');
};
}
}), _class);
"
);

test!(
syntax(false),
|_| decorators(Config {
legacy: true,
..Default::default()
}),
issue_1913_2,
"
class Store extends BaseStore{
constructor() {
super();
this.doSomething();
}
@action
doSomething = () => {
console.log('run');
}
}",
"
var _class, _descriptor;
let Store = ((_class = class Store extends BaseStore {
constructor(){
super();
_initializerDefineProperty(this, 'doSomething', _descriptor, this);
this.doSomething();
}
}) || _class, _descriptor = _applyDecoratedDescriptor(_class.prototype, 'doSomething', [
action
], {
configurable: true,
enumerable: true,
writable: true,
initializer: function() {
return ()=>{
console.log('run');
};
}
}), _class);
"
);

0 comments on commit d13eff9

Please sign in to comment.