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

feat(es/testing): Support babel-like fixture testing officially #8190

Merged
merged 59 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from 53 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
242b86c
migrate.sh
kdy1 Oct 29, 2023
66e2db3
deprecate
kdy1 Oct 29, 2023
731c35f
ls
kdy1 Oct 29, 2023
613ffa5
ast grep
kdy1 Oct 29, 2023
f698c09
Add babel_like
kdy1 Oct 29, 2023
9deab4f
Add impl
kdy1 Oct 29, 2023
307ac1d
builder
kdy1 Oct 29, 2023
8723fde
`BabelPassBuilder`
kdy1 Oct 29, 2023
a197fa0
comments
kdy1 Oct 29, 2023
e478b76
more
kdy1 Oct 29, 2023
008042c
parse options
kdy1 Oct 29, 2023
a1ee714
`parse_options`
kdy1 Oct 29, 2023
c7472ce
syntax
kdy1 Oct 29, 2023
11ca37a
fixup
kdy1 Oct 29, 2023
fbc59f3
doc
kdy1 Oct 29, 2023
879e81a
more fields
kdy1 Oct 29, 2023
139f811
`add_factory`
kdy1 Oct 29, 2023
7ed5c89
execute & fixture
kdy1 Oct 29, 2023
d6c1c92
TestOutput
kdy1 Oct 29, 2023
94b98ef
Build pass
kdy1 Oct 29, 2023
6dd6d76
panic
kdy1 Oct 29, 2023
cb82157
more work
kdy1 Oct 29, 2023
fbef36e
sourcemap
kdy1 Oct 29, 2023
312d9ab
parse
kdy1 Oct 29, 2023
963a69e
apply transform
kdy1 Oct 29, 2023
fa80594
apply transform
kdy1 Oct 29, 2023
e33616e
assert
kdy1 Oct 29, 2023
caaf8b4
invoke
kdy1 Oct 29, 2023
9e118e5
more work
kdy1 Oct 29, 2023
94d7ef3
babel_like
kdy1 Oct 29, 2023
e819d46
allow_error
kdy1 Oct 29, 2023
1575a01
Signature
kdy1 Oct 29, 2023
983eede
Migrate decorators test
kdy1 Oct 29, 2023
903679a
more doc
kdy1 Oct 29, 2023
7ab6de5
ast grep cannot do this
kdy1 Oct 29, 2023
8b98f5a
Migrate one
kdy1 Oct 29, 2023
cd3f2e6
lint
kdy1 Oct 29, 2023
ce9e822
Rename
kdy1 Oct 29, 2023
2a222f5
I found a better way
kdy1 Oct 29, 2023
9e7169b
doc
kdy1 Oct 29, 2023
6ff171d
fix
kdy1 Oct 29, 2023
b801aa1
Update test refs
kdy1 Oct 29, 2023
8defc06
fix
kdy1 Oct 29, 2023
fe839ab
Update test refs
kdy1 Oct 29, 2023
86b8557
fix
kdy1 Oct 29, 2023
60a9e14
Use cargo target dir
kdy1 Oct 29, 2023
d9a118f
Use `exec_with_node_test_runner`
kdy1 Oct 29, 2023
ddc0574
mocha
kdy1 Oct 29, 2023
936466e
Update test refs
kdy1 Oct 29, 2023
2650ec7
external helpers
kdy1 Oct 29, 2023
97e095f
Update test refs
kdy1 Oct 29, 2023
c4f1905
fix
kdy1 Oct 29, 2023
fb3b99d
Update test refs
kdy1 Oct 29, 2023
a6d3aab
exec_with_test_runner
kdy1 Oct 29, 2023
515e139
fix
kdy1 Oct 29, 2023
8694aec
Print input psth
kdy1 Oct 29, 2023
77e9ea9
src
kdy1 Oct 29, 2023
1716695
print code
kdy1 Oct 29, 2023
fd9cf29
Revert
kdy1 Oct 29, 2023
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
2 changes: 1 addition & 1 deletion crates/swc_ecma_transforms_compat/tests/es2015_arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::PathBuf;
use swc_common::{chain, Mark};
use swc_ecma_transforms_base::resolver;
use swc_ecma_transforms_compat::es2015::arrow;
use swc_ecma_transforms_testing::{compare_stdout, test, test_fixture};
use swc_ecma_transforms_testing::{compare_stdout, test_fixture};
use swc_ecma_visit::Fold;

fn tr() -> impl Fold {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 0 additions & 18 deletions crates/swc_ecma_transforms_module/tests/system_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,6 @@ fn tr(_tester: &mut Tester<'_>, config: Config) -> impl Fold {
)
}

test!(
syntax(),
|tester| tr(tester, Default::default()),
allow_continuous_assignment,
r#"var e = {}; e.a = e.b = e.c = e.d = e.e = e.f = e.g = e.h = e.i = e.j = e.k = e.l = e.m = e.n = e.o = e.p = e.q = e.r = e.s = e.t = e.u = e.v = e.w = e.x = e.y = e.z = e.A = e.B = e.C = e.D = e.E = e.F = e.G = e.H = e.I = e.J = e.K = e.L = e.M = e.N = e.O = e.P = e.Q = e.R = e.S = void 0;"#,
r#"System.register([], function (_export, _context) {
"use strict";
var e;
return {
setters: [],
execute: function () {
e = {};
e.a = e.b = e.c = e.d = e.e = e.f = e.g = e.h = e.i = e.j = e.k = e.l = e.m = e.n = e.o = e.p = e.q = e.r = e.s = e.t = e.u = e.v = e.w = e.x = e.y = e.z = e.A = e.B = e.C = e.D = e.E = e.F = e.G = e.H = e.I = e.J = e.K = e.L = e.M = e.N = e.O = e.P = e.Q = e.R = e.S = void 0;
}
};
});"#
);

test!(
syntax(),
|tester| tr(
Expand Down
175 changes: 55 additions & 120 deletions crates/swc_ecma_transforms_proposal/tests/decorators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ 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::{test_fixture, FixtureTestConfig};
use swc_ecma_transforms_testing::{
babel_like::{BabelLikeFixtureTest, PassContext},
test_fixture, FixtureTestConfig,
};
use swc_ecma_visit::Fold;

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

fn exec_inner(input: PathBuf) {
let code = std::fs::read_to_string(&input).unwrap();

swc_ecma_transforms_testing::exec_tr(
"decorator",
Syntax::Typescript(TsConfig {
BabelLikeFixtureTest::new(&input)
.syntax(Syntax::Typescript(TsConfig {
decorators: true,
..Default::default()
}),
|t| create_pass(t.comments.clone(), &input),
&code,
);
}))
.add_factory(|| {
let static_block_mark = Mark::new();

Box::new(move |c, name, opts| create_pass(c, name, opts, static_block_mark))
})
.execute();
}

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

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

#[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 },
}))
.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);
}

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");
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,
}

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 Bar {
class Bar1 {
}
var __ = {
writable: true,
value: { c: [_Bar1, _initClass] } = _apply_decs_2203_r(Bar, [], [
value: { c: [_Bar1, _initClass] } = _apply_decs_2203_r(Bar1, [], [
dec1
])
};
Expand All @@ -15,7 +15,7 @@ var __2 = {
value: _initClass()
};
let _Foo;
class Foo extends (_Bar = _Bar1) {
class Foo extends (_Bar = Bar) {
}
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(_Foo1), _initClass();
super(Foo), _initClass();
}
}, __ = {
writable: true,
value: (()=>{
class Foo {
class Foo1 {
}
var __ = {
writable: true,
value: { c: [_Foo1, _initClass] } = _apply_decs_2203_r(Foo, [], [
value: { c: [_Foo1, _initClass] } = _apply_decs_2203_r(Foo1, [], [
dec
])
};
_define_property(Foo, "field", 123);
_define_property(Foo1, "field", 123);
})()
}, _class)();
let _Bar;
new (_class1 = class extends _identity {
constructor(){
super(_Bar), _initClass1();
super(Bar), _initClass1();
}
}, __1 = {
writable: true,
value: (()=>{
var _ref;
class Bar extends (_ref = _Foo = _Foo1) {
class Bar1 extends (_ref = _Foo = Foo) {
}
var __ = {
writable: true,
value: { c: [_Bar, _initClass1] } = _apply_decs_2203_r(Bar, [], [
value: { c: [_Bar, _initClass1] } = _apply_decs_2203_r(Bar1, [], [
dec
], _Foo)
};
_define_property(Bar, "field", ((()=>{
Bar.otherField = 456;
_define_property(Bar1, "field", ((()=>{
Bar1.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 Foo {
class Foo1 {
}
var __ = {
writable: true,
value: { c: [_Foo, _initClass] } = _apply_decs_2203_r(Foo, [], [
value: { c: [_Foo, _initClass] } = _apply_decs_2203_r(Foo1, [], [
dec
])
};
_define_property(Foo, "foo", new _Foo());
_define_property(Foo1, "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
Loading