Skip to content

Commit

Permalink
fix(es/decorator): Insert initializer to constructor with body (#4028)
Browse files Browse the repository at this point in the history
  • Loading branch information
Austaras authored Mar 15, 2022
1 parent 6d5541c commit 0c76696
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 33 deletions.
37 changes: 37 additions & 0 deletions crates/swc_ecma_transforms/tests/decorators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6061,6 +6061,43 @@ test!(
}), _class);"
);

test!(
ts(),
|_| decorators(Config {
legacy: true,
emit_metadata: false,
}),
issue_3979,
"
class A {
@foo x = 1;
constructor()
constructor() {
console.log(123)
}
}
",
r#"
var _class, _descriptor;
let A = ((_class = class A {
constructor();
constructor(){
_initializerDefineProperty(this, "x", _descriptor, this);
console.log(123);
}
}) || _class, _descriptor = _applyDecoratedDescriptor(_class.prototype, "x", [
foo
], {
configurable: true,
enumerable: true,
writable: true,
initializer: function() {
return 1;
}
}), _class);
"#
);

#[testing::fixture("tests/fixture/decorator/**/exec.ts")]
fn fixture(input: PathBuf) {
let code = fs::read_to_string(&input).expect("failed to read file");
Expand Down
48 changes: 15 additions & 33 deletions crates/swc_ecma_transforms_proposal/src/decorators/legacy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -702,41 +702,23 @@ impl Legacy {
});

if !constructor_stmts.is_empty() {
{
// Create constructors as required

let has = c
.class
let constructor = if let Some(c) = c.class.body.iter_mut().find_map(|m| match m {
ClassMember::Constructor(c @ Constructor { body: Some(..), .. }) => Some(c),
_ => None,
}) {
c
} else {
c.class
.body
.iter()
.any(|m| matches!(m, ClassMember::Constructor(..)));

if !has {
c.class
.body
.push(ClassMember::Constructor(default_constructor(
c.class.super_class.is_some(),
)))
.push(ClassMember::Constructor(default_constructor(
c.class.super_class.is_some(),
)));
if let ClassMember::Constructor(c) = c.class.body.last_mut().unwrap() {
c
} else {
unreachable!()
}
}

let constructor = c
.class
.body
.iter_mut()
.filter_map(|m| match m {
ClassMember::Constructor(c) => Some(c),
_ => None,
})
.next()
.unwrap();

if constructor.body.is_none() {
constructor.body = Some(BlockStmt {
span: DUMMY_SP,
stmts: vec![],
});
}
};

let decorate_stmts_insert_position = constructor
.body
Expand Down

1 comment on commit 0c76696

@github-actions
Copy link

Choose a reason for hiding this comment

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

Benchmark

Benchmark suite Current: 0c76696 Previous: 25e5ccc Ratio
base_tr_fixer 22210 ns/iter (± 751) 32686 ns/iter (± 5463) 0.68
base_tr_resolver_and_hygiene 91901 ns/iter (± 9013) 140899 ns/iter (± 20846) 0.65
codegen_es2015 27767 ns/iter (± 199) 36619 ns/iter (± 4953) 0.76
codegen_es2016 27713 ns/iter (± 243) 35161 ns/iter (± 3579) 0.79
codegen_es2017 27739 ns/iter (± 177) 35760 ns/iter (± 7540) 0.78
codegen_es2018 27641 ns/iter (± 176) 35631 ns/iter (± 4409) 0.78
codegen_es2019 27643 ns/iter (± 154) 34987 ns/iter (± 4438) 0.79
codegen_es2020 27659 ns/iter (± 227) 35503 ns/iter (± 3886) 0.78
codegen_es3 27393 ns/iter (± 241) 35369 ns/iter (± 4558) 0.77
codegen_es5 27462 ns/iter (± 310) 36038 ns/iter (± 6679) 0.76
full_es2015 136313683 ns/iter (± 14557834) 169846791 ns/iter (± 16741573) 0.80
full_es2016 135077479 ns/iter (± 5884726) 169424514 ns/iter (± 12703486) 0.80
full_es2017 136134649 ns/iter (± 6824306) 169493942 ns/iter (± 15532245) 0.80
full_es2018 129790430 ns/iter (± 7739716) 167841352 ns/iter (± 13492100) 0.77
full_es2019 126033588 ns/iter (± 6144735) 165777279 ns/iter (± 9967797) 0.76
full_es2020 116215039 ns/iter (± 3101287) 158198663 ns/iter (± 16081426) 0.73
full_es3 180050115 ns/iter (± 4644360) 226494543 ns/iter (± 18330006) 0.79
full_es5 170149473 ns/iter (± 5103280) 210252819 ns/iter (± 15390375) 0.81
parser 540854 ns/iter (± 17435) 755244 ns/iter (± 102287) 0.72
ser_ast_node 151 ns/iter (± 2) 181 ns/iter (± 12) 0.83
ser_serde 145 ns/iter (± 1) 173 ns/iter (± 18) 0.84
emit_colors 4969549 ns/iter (± 3487441) 5602398 ns/iter (± 3899981) 0.89
emit_large 121924818 ns/iter (± 183229827) 90676412 ns/iter (± 120853120) 1.34
base_clone 2507259 ns/iter (± 23714) 2780113 ns/iter (± 518431) 0.90
fold_span 4080971 ns/iter (± 81634) 5124185 ns/iter (± 909313) 0.80
fold_span_panic 3935121 ns/iter (± 54964) 5107630 ns/iter (± 848540) 0.77
visit_mut_span 2687845 ns/iter (± 13352) 3384639 ns/iter (± 427953) 0.79
visit_mut_span_panic 2740659 ns/iter (± 35531) 3454199 ns/iter (± 682692) 0.79
usage_builtin_type 14733129 ns/iter (± 9183134) 18670296 ns/iter (± 11498314) 0.79
usage_property 352364 ns/iter (± 1057) 462372 ns/iter (± 50225) 0.76
boxing_boxed 135 ns/iter (± 15) 192 ns/iter (± 33) 0.70
boxing_boxed_clone 77 ns/iter (± 0) 96 ns/iter (± 14) 0.80
boxing_unboxed 116 ns/iter (± 1) 149 ns/iter (± 18) 0.78
boxing_unboxed_clone 72 ns/iter (± 0) 92 ns/iter (± 12) 0.78
time_10 294 ns/iter (± 2) 410 ns/iter (± 61) 0.72
time_15 634 ns/iter (± 2) 759 ns/iter (± 104) 0.84
time_20 1239 ns/iter (± 3) 1447 ns/iter (± 311) 0.86
time_40 7015 ns/iter (± 17) 5574 ns/iter (± 862) 1.26
time_5 94 ns/iter (± 0) 133 ns/iter (± 26) 0.71
time_60 15915 ns/iter (± 50) 11684 ns/iter (± 1513) 1.36
total 0 ns/iter (± 0) 0 ns/iter (± 0) NaN

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.