2022-03-01 12:55:45 +03:00
|
|
|
#![deny(warnings)]
|
|
|
|
|
2022-03-01 08:18:55 +03:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
use swc_common::{chain, Mark};
|
2023-03-30 11:06:02 +03:00
|
|
|
use swc_ecma_parser::Syntax;
|
2022-04-26 10:38:50 +03:00
|
|
|
use swc_ecma_transforms_base::resolver;
|
2022-03-01 12:55:45 +03:00
|
|
|
use swc_ecma_transforms_module::system_js::{system_js, Config};
|
2022-03-01 08:18:55 +03:00
|
|
|
use swc_ecma_transforms_testing::{test, test_fixture, Tester};
|
|
|
|
use swc_ecma_visit::Fold;
|
|
|
|
|
|
|
|
fn syntax() -> Syntax {
|
2023-03-30 11:06:02 +03:00
|
|
|
Syntax::Es(Default::default())
|
2022-03-01 08:18:55 +03:00
|
|
|
}
|
|
|
|
|
2022-03-01 12:55:45 +03:00
|
|
|
fn tr(_tester: &mut Tester<'_>, config: Config) -> impl Fold {
|
2022-04-26 10:38:50 +03:00
|
|
|
let unresolved_mark = Mark::new();
|
|
|
|
let top_level_mark = Mark::new();
|
2022-03-01 12:55:45 +03:00
|
|
|
chain!(
|
2022-04-26 10:38:50 +03:00
|
|
|
resolver(unresolved_mark, top_level_mark, false),
|
|
|
|
system_js(unresolved_mark, config)
|
2022-03-01 12:55:45 +03:00
|
|
|
)
|
2022-03-01 08:18:55 +03:00
|
|
|
}
|
|
|
|
|
2023-10-30 04:38:59 +03:00
|
|
|
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;"#
|
|
|
|
);
|
|
|
|
|
2022-03-01 08:18:55 +03:00
|
|
|
test!(
|
|
|
|
syntax(),
|
|
|
|
|tester| tr(
|
|
|
|
tester,
|
|
|
|
Config {
|
2023-09-14 10:17:23 +03:00
|
|
|
allow_top_level_this: true,
|
|
|
|
..Default::default()
|
2022-03-01 08:18:55 +03:00
|
|
|
}
|
|
|
|
),
|
|
|
|
allow_top_level_this_true,
|
2023-10-30 04:38:59 +03:00
|
|
|
r#"export var v = this;"#
|
2022-03-01 08:18:55 +03:00
|
|
|
);
|
|
|
|
|
2023-01-27 15:58:22 +03:00
|
|
|
test!(
|
|
|
|
syntax(),
|
|
|
|
|tester| tr(
|
|
|
|
tester,
|
|
|
|
Config {
|
2023-09-14 10:17:23 +03:00
|
|
|
allow_top_level_this: false,
|
|
|
|
..Default::default()
|
2023-01-27 15:58:22 +03:00
|
|
|
}
|
|
|
|
),
|
|
|
|
iife,
|
|
|
|
r#"
|
|
|
|
(function(a) {
|
|
|
|
this.foo = a;
|
|
|
|
})(this);
|
2023-10-30 04:38:59 +03:00
|
|
|
"#
|
2023-01-27 15:58:22 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
test!(
|
|
|
|
syntax(),
|
|
|
|
|tester| tr(
|
|
|
|
tester,
|
|
|
|
Config {
|
2023-09-14 10:17:23 +03:00
|
|
|
allow_top_level_this: false,
|
|
|
|
..Default::default()
|
2023-01-27 15:58:22 +03:00
|
|
|
}
|
|
|
|
),
|
|
|
|
top_level_this_false_class,
|
|
|
|
r#"
|
|
|
|
const a = this;
|
|
|
|
class A {
|
|
|
|
constructor() {
|
|
|
|
this.a = 1;
|
|
|
|
}
|
|
|
|
test() {
|
|
|
|
this.a = 2;
|
|
|
|
}
|
2023-10-30 04:38:59 +03:00
|
|
|
}"#
|
2023-01-27 15:58:22 +03:00
|
|
|
);
|
|
|
|
|
2022-03-01 08:18:55 +03:00
|
|
|
test!(
|
|
|
|
syntax(),
|
|
|
|
|tester| tr(
|
|
|
|
tester,
|
|
|
|
Config {
|
2023-09-14 10:17:23 +03:00
|
|
|
allow_top_level_this: false,
|
|
|
|
..Default::default()
|
2022-03-01 08:18:55 +03:00
|
|
|
}
|
|
|
|
),
|
|
|
|
allow_top_level_this_false,
|
|
|
|
r#"export var v = this;
|
|
|
|
function a() {
|
|
|
|
function d () {}
|
|
|
|
var b = this;
|
2023-10-30 04:38:59 +03:00
|
|
|
} "#
|
2022-03-01 08:18:55 +03:00
|
|
|
);
|
|
|
|
|
2023-02-07 19:07:46 +03:00
|
|
|
test!(
|
|
|
|
syntax(),
|
2023-03-30 11:06:02 +03:00
|
|
|
|tester| tr(tester, Default::default()),
|
2023-02-07 19:07:46 +03:00
|
|
|
imports,
|
|
|
|
r#"
|
|
|
|
import.meta.url;
|
|
|
|
import.meta.fn();
|
|
|
|
await import('./test2');
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
2022-03-01 08:18:55 +03:00
|
|
|
// TODO: test get-module-name-option, tla
|
|
|
|
|
|
|
|
#[testing::fixture("tests/fixture/systemjs/**/input.mjs")]
|
|
|
|
fn fixture(input: PathBuf) {
|
|
|
|
let dir = input.parent().unwrap().to_path_buf();
|
|
|
|
|
|
|
|
let output = dir.join("output.mjs");
|
|
|
|
|
|
|
|
test_fixture(
|
|
|
|
syntax(),
|
2023-03-30 11:06:02 +03:00
|
|
|
&|tester| tr(tester, Default::default()),
|
2022-03-01 08:18:55 +03:00
|
|
|
&input,
|
|
|
|
&output,
|
2022-10-07 08:52:22 +03:00
|
|
|
Default::default(),
|
2022-03-01 08:18:55 +03:00
|
|
|
);
|
|
|
|
}
|