Skip to content

Commit

Permalink
Revert
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Oct 29, 2023
1 parent 1716695 commit fd9cf29
Show file tree
Hide file tree
Showing 54 changed files with 216 additions and 153 deletions.
175 changes: 120 additions & 55 deletions crates/swc_ecma_transforms_proposal/tests/decorators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ 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_testing::{
babel_like::{BabelLikeFixtureTest, PassContext},
test_fixture, FixtureTestConfig,
};
use swc_ecma_transforms_testing::{test_fixture, FixtureTestConfig};
use swc_ecma_visit::Fold;

fn syntax_default() -> Syntax {
Expand All @@ -32,17 +29,17 @@ fn exec(input: PathBuf) {
}

fn exec_inner(input: PathBuf) {
BabelLikeFixtureTest::new(&input)
.syntax(Syntax::Typescript(TsConfig {
let code = std::fs::read_to_string(&input).unwrap();

swc_ecma_transforms_testing::exec_tr(
"decorator",
Syntax::Typescript(TsConfig {
decorators: true,
..Default::default()
}))
.add_factory(|| {
let static_block_mark = Mark::new();

Box::new(move |c, name, opts| create_pass(c, name, opts, static_block_mark))
})
.exec_with_test_runner();
}),
|t| create_pass(t.comments.clone(), &input),
&code,
);
}

#[testing::fixture("tests/decorators/**/input.js")]
Expand All @@ -59,50 +56,118 @@ fn fixture_inner(input: PathBuf) {
input.extension().unwrap().to_string_lossy()
));

BabelLikeFixtureTest::new(&input)
.syntax(Syntax::Es(EsConfig {
decorators: true,
auto_accessors: true,
allow_super_outside_method: true,
decorators_before_export: true,
test_fixture(
syntax_default(),
&|t| create_pass(t.comments.clone(), &input),
&input,
&output,
FixtureTestConfig {
allow_error: true,
..Default::default()
}))
.add_factory(|| {
let static_block_mark = Mark::new();

Box::new(move |c, name, opts| create_pass(c, name, opts, static_block_mark))
})
.allow_error()
.fixture(&output);
},
);
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
struct BabelTestOptions {
#[serde(default)]
assumptions: Assumptions,

#[serde(default)]
plugins: Vec<BabelPluginEntry>,

#[serde(default)]
min_node_version: String,

#[serde(default)]
throws: Option<String>,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase", untagged)]
enum BabelPluginEntry {
NameOnly(String),
WithConfig(String, BabelPluginOption),
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields, untagged, rename_all = "camelCase")]
enum BabelPluginOption {
Decorator { version: String },
}

fn create_pass(
c: &PassContext,
name: &str,
options: Option<serde_json::Value>,
static_block_mark: Mark,
) -> Option<Box<dyn Fold>> {
match name {
"proposal-decorators" => Some(Box::new(decorator_2022_03())),

"proposal-class-properties" => Some(Box::new(chain!(
swc_ecma_transforms_compat::es2022::static_blocks(static_block_mark),
swc_ecma_transforms_compat::es2022::class_properties(
Some(c.comments.clone()),
Default::default()
)
))),

"proposal-private-methods" => Some(Box::new(
swc_ecma_transforms_compat::es2022::class_properties(
Some(c.comments.clone()),
Default::default(),
),
)),

"proposal-class-static-block" => Some(Box::new(
swc_ecma_transforms_compat::es2022::static_blocks(static_block_mark),
)),
_ => None,
fn read_options_json(input: &Path) -> BabelTestOptions {
let mut options_path = input.to_path_buf();
options_path.set_file_name("options.json");

if options_path.exists() {
let s = std::fs::read_to_string(&options_path).unwrap();
println!("Options: {}", s);
return serde_json::from_str(&s).expect("failed to read options.json");
}

println!("Reading options from {:?}", options_path);

// Look for parent directory

read_options_json(options_path.parent().unwrap())
}

fn create_pass(comments: Rc<SingleThreadedComments>, input: &Path) -> Box<dyn Fold> {
let options_json = read_options_json(input);

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()
));

macro_rules! add {
($e:expr) => {{
pass = Box::new(chain!(pass, $e));
}};
}

let static_block_mark = Mark::new();

for plugin in &options_json.plugins {
match plugin {
BabelPluginEntry::NameOnly(name) => match &**name {
"proposal-class-properties" => {
add!(swc_ecma_transforms_compat::es2022::static_blocks(
static_block_mark
));
add!(swc_ecma_transforms_compat::es2022::class_properties(
Some(comments.clone()),
Default::default()
));
continue;
}

"proposal-private-methods" => {
add!(swc_ecma_transforms_compat::es2022::class_properties(
Some(comments.clone()),
Default::default()
));
continue;
}

"proposal-class-static-block" => {
add!(swc_ecma_transforms_compat::es2022::static_blocks(
static_block_mark
));
continue;
}
_ => {}
},
BabelPluginEntry::WithConfig(name, config) => {}
}

dbg!(&plugin);
}

pass
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var _b = {
var __ = {
writable: true,
value: (()=>{
({ e: [_init_a, _get___a, _set___a, _init_b, _get___b, _set___b, _initStatic] } = _apply_decs_2203_r(Foo, [
({ e: [_init_a, _get___a, _set___a, _init_b, _get___b, _set___b, _initStatic] } = _apply_decs_2203_r(Foo, [
[
dec,
6,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Foo {
var __ = {
writable: true,
value: (()=>{
({ e: [_init_a, _init_b, _init_computedKey, _initStatic] } = _apply_decs_2203_r(Foo, [
({ e: [_init_a, _init_b, _init_computedKey, _initStatic] } = _apply_decs_2203_r(Foo, [
[
dec,
6,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var _init_a, _get___a, _set___a, _init_b, _get___b, _set___b, _initProto;
const dec = ()=>{};
class Foo {
static{
({ e: [_init_a, _get___a, _set___a, _init_b, _get___b, _set___b, _initProto] } = _apply_decs_2203_r(this, [
({ e: [_init_a, _get___a, _set___a, _init_b, _get___b, _set___b, _initProto] } = _apply_decs_2203_r(this, [
[
dec,
1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const dec = ()=>{};
_computedKey = 'c';
class Foo {
static{
({ e: [_init_a, _init_b, _init_computedKey, _initProto] } = _apply_decs_2203_r(this, [
({ e: [_init_a, _init_b, _init_computedKey, _initProto] } = _apply_decs_2203_r(this, [
[
dec,
1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var _init_a, _get___a, _set___a, _init_b, _get___b, _set___b, _initStatic;
const dec = ()=>{};
class Foo {
static{
({ e: [_init_a, _get___a, _set___a, _init_b, _get___b, _set___b, _initStatic] } = _apply_decs_2203_r(this, [
({ e: [_init_a, _get___a, _set___a, _init_b, _get___b, _set___b, _initStatic] } = _apply_decs_2203_r(this, [
[
dec,
6,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const dec = ()=>{};
_computedKey = 'c';
class Foo {
static{
({ e: [_init_a, _init_b, _init_computedKey, _initStatic] } = _apply_decs_2203_r(this, [
({ e: [_init_a, _init_b, _init_computedKey, _initStatic] } = _apply_decs_2203_r(this, [
[
dec,
6,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ var _initClass, _initClass1, _Bar;
const dec1 = ()=>{};
const dec2 = ()=>{};
let _Bar1;
class Bar1 {
class Bar {
}
var __ = {
writable: true,
value: { c: [_Bar1, _initClass] } = _apply_decs_2203_r(Bar1, [], [
value: { c: [_Bar1, _initClass] } = _apply_decs_2203_r(Bar, [], [
dec1
])
};
Expand All @@ -15,7 +15,7 @@ var __2 = {
value: _initClass()
};
let _Foo;
class Foo extends (_Bar = Bar) {
class Foo extends (_Bar = _Bar1) {
}
var __1 = {
writable: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,41 @@ const dec = ()=>{};
let _Foo1;
new (_class = class extends _identity {
constructor(){
super(Foo), _initClass();
super(_Foo1), _initClass();
}
}, __ = {
writable: true,
value: (()=>{
class Foo1 {
class Foo {
}
var __ = {
writable: true,
value: { c: [_Foo1, _initClass] } = _apply_decs_2203_r(Foo1, [], [
value: { c: [_Foo1, _initClass] } = _apply_decs_2203_r(Foo, [], [
dec
])
};
_define_property(Foo1, "field", 123);
_define_property(Foo, "field", 123);
})()
}, _class)();
let _Bar;
new (_class1 = class extends _identity {
constructor(){
super(Bar), _initClass1();
super(_Bar), _initClass1();
}
}, __1 = {
writable: true,
value: (()=>{
var _ref;
class Bar1 extends (_ref = _Foo = Foo) {
class Bar extends (_ref = _Foo = _Foo1) {
}
var __ = {
writable: true,
value: { c: [_Bar, _initClass1] } = _apply_decs_2203_r(Bar1, [], [
value: { c: [_Bar, _initClass1] } = _apply_decs_2203_r(Bar, [], [
dec
], _Foo)
};
_define_property(Bar1, "field", ((()=>{
Bar1.otherField = 456;
_define_property(Bar, "field", ((()=>{
Bar.otherField = 456;
})(), 123));
})()
}, _class1)();
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ const dec = ()=>{};
let _Foo;
new (_class = class extends _identity {
constructor(){
super(Foo), _initClass();
super(_Foo), _initClass();
}
}, __ = {
writable: true,
value: (()=>{
class Foo1 {
class Foo {
}
var __ = {
writable: true,
value: { c: [_Foo, _initClass] } = _apply_decs_2203_r(Foo1, [], [
value: { c: [_Foo, _initClass] } = _apply_decs_2203_r(Foo, [], [
dec
])
};
_define_property(Foo1, "foo", new _Foo());
_define_property(Foo, "foo", new _Foo());
})()
}, _class)();
const foo = new Foo();
const foo = new _Foo();
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const memberDec = ()=>()=>42;
let _C;
class C {
static{
({ e: [_init_m], c: [_C, _initClass] } = _apply_decs_2203_r(this, [
({ e: [_init_m] , c: [_C, _initClass] } = _apply_decs_2203_r(this, [
[
memberDec,
0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const memberDec = ()=>()=>42;
let _C;
class C {
static{
({ e: [_initProto], c: [_C, _initClass] } = _apply_decs_2203_r(this, [
({ e: [_initProto] , c: [_C, _initClass] } = _apply_decs_2203_r(this, [
[
memberDec,
2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const F = [
];
const H = (class extends (_I = I) {
static{
({ c: [_class3, _initClass6] } = _apply_decs_2203_r(this, [], [
({ c: [_class3, _initClass6] } = _apply_decs_2203_r(this, [], [
dec
], _I));
}
Expand All @@ -81,7 +81,7 @@ const H = (class extends (_I = I) {
}, _class3);
const J = (class K extends (_L = L) {
static{
({ c: [_K, _initClass7] } = _apply_decs_2203_r(this, [], [
({ c: [_K, _initClass7] } = _apply_decs_2203_r(this, [], [
dec
], _L));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const H = (class extends (_I = I) {
}, _class3);
const J = (class K extends (_L = L) {
static{
({ c: [_K, _initClass7] } = _apply_decs_2203_r(this, [], [
({ c: [_K, _initClass7] } = _apply_decs_2203_r(this, [], [
dec
], _L));
}
Expand Down
Loading

0 comments on commit fd9cf29

Please sign in to comment.