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 decorator initialization order #8762

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions crates/swc/tests/fixture/issues-7xxx/issue-7404/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"jsc": {
"transform": {
"useDefineForClassFields": true,
"decoratorVersion": "2022-03"
},
"parser": {
"syntax": "typescript",
"decorators": true
},
"target": "es2015",
"loose": false
},
"module": {
"type": "es6"
},
"isModule": true,
"minify": false
}
31 changes: 31 additions & 0 deletions crates/swc/tests/fixture/issues-7xxx/issue-7404/input/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
function minusTwo({ set, get }) {
return {
set(v) {
set.call(this, v - 2)
},
init(v) {
return v - 2;
}
}
}

function timesFour({ set, get }) {
return {
set(v) {
set.call(this, v * 4)
},
init(v) {
return v * 4;
}
}
}

class Foo {
@minusTwo @timesFour accessor bar = 5;
}

const foo = new Foo();
console.log({ init: foo.bar });

foo.bar = 5;
console.log({ set: foo.bar });
25 changes: 21 additions & 4 deletions crates/swc_ecma_transforms_proposal/src/decorator_2022_03.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
default,
iter::once,
mem::{take, transmute},
};
Expand All @@ -16,11 +17,27 @@ use swc_ecma_utils::{
use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith};

pub fn decorator_2022_03() -> impl VisitMut + Fold {
as_folder(Decorator202203::default())
as_folder(SpecDecorator::default())
}

pub fn decorator_2023_11() -> impl VisitMut + Fold {
as_folder(SpecDecorator {
version: Version::V202311,
..Default::default()
})
}

#[derive(Debug, Default)]
enum Version {
#[default]
V202203,
V202311,
}

#[derive(Default)]
struct Decorator202203 {
struct SpecDecorator {
version: Version,

/// Variables without initializer.
extra_vars: Vec<VarDeclarator>,

Expand Down Expand Up @@ -58,7 +75,7 @@ struct ClassState {
super_class: Option<Ident>,
}

impl Decorator202203 {
impl SpecDecorator {
fn preserve_side_effect_of_decorators(
&mut self,
decorators: Vec<Decorator>,
Expand Down Expand Up @@ -752,7 +769,7 @@ impl Decorator202203 {
}
}

impl VisitMut for Decorator202203 {
impl VisitMut for SpecDecorator {
noop_visit_mut_type!();

fn visit_mut_class(&mut self, n: &mut Class) {
Expand Down
25 changes: 19 additions & 6 deletions crates/swc_ecma_transforms_proposal/tests/decorators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use serde::Deserialize;
use swc_common::{chain, comments::SingleThreadedComments, Mark};
use swc_ecma_parser::{EsConfig, Syntax, TsConfig};
use swc_ecma_transforms_base::{assumptions::Assumptions, resolver};
use swc_ecma_transforms_proposal::decorator_2022_03::decorator_2022_03;
use swc_ecma_transforms_proposal::decorator_2022_03::{decorator_2022_03, decorator_2023_11};
use swc_ecma_transforms_testing::{test_fixture, FixtureTestConfig};
use swc_ecma_visit::Fold;

Expand Down Expand Up @@ -133,10 +133,7 @@ fn create_pass(comments: Rc<SingleThreadedComments>, input: &Path) -> Box<dyn Fo
let unresolved_mark = Mark::new();
let top_level_mark = Mark::new();

let mut pass: Box<dyn Fold> = Box::new(chain!(
resolver(unresolved_mark, top_level_mark, false),
decorator_2022_03()
));
let mut pass: Box<dyn Fold> = Box::new(resolver(unresolved_mark, top_level_mark, false));

macro_rules! add {
($e:expr) => {{
Expand Down Expand Up @@ -178,7 +175,23 @@ fn create_pass(comments: Rc<SingleThreadedComments>, input: &Path) -> Box<dyn Fo
}
_ => {}
},
BabelPluginEntry::WithConfig(name, config) => {}
BabelPluginEntry::WithConfig(name, config) => {
if let "proposal-decorators" = &**name {
match config {
BabelPluginOption::Decorator { version } => match &**version {
"2022-03" => {
add!(decorator_2022_03());
}
"2023-11" => {
add!(decorator_2023_11());
}

_ => {}
},
}
continue;
}
}
}

dbg!(&plugin);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
function minusTwo({ set, get }) {
return {
set(v) {
set.call(this, v - 2)
},
init(v) {
return v - 2;
}
}
}

function timesFour({ set, get }) {
return {
set(v) {
set.call(this, v * 4)
},
init(v) {
return v * 4;
}
}
}

class Foo {
@minusTwo @timesFour accessor bar = 5;
}

const foo = new Foo();
console.log({ init: foo.bar });

foo.bar = 5;
console.log({ set: foo.bar });
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
function minusTwo({
set,
get
}) {
return {
set(v) {
set.call(this, v - 2);
},
init(v) {
return v - 2;
}
};
}
function timesFour({
set,
get
}) {
return {
set(v) {
set.call(this, v * 4);
},
init(v) {
return v * 4;
}
};
}
var _A = /*#__PURE__*/new WeakMap();
class Foo {
constructor() {
_classPrivateFieldInitSpec(this, _A, _init_bar(this, 5));
_init_extra_bar(this);
}
get bar() {
return _classPrivateFieldGet(_A, this);
}
set bar(v) {
_classPrivateFieldSet(_A, this, v);
}
}
_Foo = Foo;
[_init_bar, _init_extra_bar] = _applyDecs(_Foo, [], [[[minusTwo, timesFour], 1, "bar"]]).e;
const foo = new Foo();
console.log({
init: foo.bar
});
foo.bar = 5;
console.log({
set: foo.bar
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const assert = require("assert");

function minusTwo({ set, get }) {
return {
set(v) {
set.call(this, v - 2)
},
init(v) {
return v - 2;
}
}
}

function timesFour({ set, get }) {
return {
set(v) {
set.call(this, v * 4)
},
init(v) {
return v * 4;
}
}
}

class Foo {
@minusTwo @timesFour accessor bar = 5;
}

const foo = new Foo();
console.log({ init: foo.bar });
assert.deepStrictEqual({ init: foo.bar }, { init: 12 });

foo.bar = 5;
console.log({ set: foo.bar });

assert.deepStrictEqual({ set: foo.bar }, { set: 12 });
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": [["proposal-decorators", { "version": "2023-11" }]]
}