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};
|
|
|
|
use swc_ecma_parser::{EsConfig, 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 {
|
|
|
|
Syntax::Es(EsConfig {
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
test!(
|
|
|
|
syntax(),
|
|
|
|
|tester| tr(
|
|
|
|
tester,
|
|
|
|
Config {
|
|
|
|
allow_top_level_this: true
|
|
|
|
}
|
|
|
|
),
|
|
|
|
allow_top_level_this_true,
|
|
|
|
r#"export var v = this;"#,
|
|
|
|
r#"System.register([], function(_export, _context) {
|
|
|
|
"use strict";
|
|
|
|
var v;
|
|
|
|
return {
|
|
|
|
setters: [],
|
|
|
|
execute: function() {
|
|
|
|
_export("v", v = this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});"#
|
|
|
|
);
|
|
|
|
|
|
|
|
test!(
|
|
|
|
syntax(),
|
|
|
|
|tester| tr(
|
|
|
|
tester,
|
|
|
|
Config {
|
|
|
|
allow_top_level_this: false
|
|
|
|
}
|
|
|
|
),
|
|
|
|
allow_top_level_this_false,
|
|
|
|
r#"export var v = this;
|
|
|
|
function a() {
|
|
|
|
function d () {}
|
|
|
|
var b = this;
|
|
|
|
} "#,
|
|
|
|
r#"System.register([], function(_export, _context) {
|
|
|
|
"use strict";
|
|
|
|
var v;
|
|
|
|
function a() {
|
|
|
|
function d() {}
|
|
|
|
var b = this;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
setters: [],
|
|
|
|
execute: function() {
|
|
|
|
_export("v", v = void 0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});"#
|
|
|
|
);
|
|
|
|
|
|
|
|
// 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(),
|
|
|
|
&|tester| {
|
|
|
|
tr(
|
|
|
|
tester,
|
|
|
|
Config {
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
},
|
|
|
|
&input,
|
|
|
|
&output,
|
|
|
|
);
|
|
|
|
}
|