2023-09-16 12:17:26 +03:00
|
|
|
use std::{collections::HashMap, fs::read_to_string, path::PathBuf};
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2021-01-05 08:29:52 +03:00
|
|
|
use swc_ecma_transforms_optimization::const_modules;
|
2023-09-16 12:17:26 +03:00
|
|
|
use swc_ecma_transforms_testing::{test, test_fixture, Tester};
|
2020-07-23 20:18:22 +03:00
|
|
|
use swc_ecma_visit::Fold;
|
2019-12-24 16:53:48 +03:00
|
|
|
|
2020-08-03 19:33:23 +03:00
|
|
|
fn tr(t: &mut Tester<'_>, sources: &[(&str, &[(&str, &str)])]) -> impl Fold {
|
2019-12-24 16:53:48 +03:00
|
|
|
let mut m = HashMap::default();
|
|
|
|
|
|
|
|
for (src, values) in sources {
|
|
|
|
let values = values
|
|
|
|
.iter()
|
|
|
|
.map(|(k, v)| ((*k).into(), v.to_string()))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
m.insert((*src).into(), values);
|
|
|
|
}
|
|
|
|
|
2020-08-03 19:33:23 +03:00
|
|
|
const_modules(t.cm.clone(), m)
|
2019-12-24 16:53:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
test!(
|
|
|
|
::swc_ecma_parser::Syntax::default(),
|
|
|
|
|tester| tr(tester, &[("@ember/env-flags", &[("DEBUG", "true")])]),
|
|
|
|
simple_flags,
|
|
|
|
r#"import { DEBUG } from '@ember/env-flags';
|
|
|
|
if (DEBUG) {
|
|
|
|
console.log('Foo!');
|
|
|
|
}"#
|
|
|
|
);
|
|
|
|
|
2023-03-06 06:33:52 +03:00
|
|
|
test!(
|
|
|
|
::swc_ecma_parser::Syntax::default(),
|
|
|
|
|tester| tr(tester, &[("@ember/env-flags", &[("DEBUG", "true")])]),
|
|
|
|
imports_hoisted,
|
|
|
|
r#"
|
|
|
|
if (DEBUG) {
|
|
|
|
console.log('Foo!');
|
|
|
|
}
|
|
|
|
|
|
|
|
import { DEBUG } from '@ember/env-flags';
|
2023-10-30 04:38:59 +03:00
|
|
|
"#
|
2023-03-06 06:33:52 +03:00
|
|
|
);
|
|
|
|
|
2019-12-24 16:53:48 +03:00
|
|
|
test!(
|
|
|
|
::swc_ecma_parser::Syntax::default(),
|
|
|
|
|tester| tr(
|
|
|
|
tester,
|
|
|
|
&[
|
|
|
|
("@ember/env-flags", &[("DEBUG", "true")]),
|
|
|
|
(
|
|
|
|
"@ember/features",
|
|
|
|
&[("FEATURE_A", "false"), ("FEATURE_B", "true")]
|
|
|
|
)
|
|
|
|
]
|
|
|
|
),
|
|
|
|
complex_multiple,
|
|
|
|
"
|
|
|
|
import { DEBUG } from '@ember/env-flags';
|
|
|
|
import { FEATURE_A, FEATURE_B } from '@ember/features';
|
|
|
|
if (DEBUG) {
|
|
|
|
console.log('Foo!');
|
|
|
|
}
|
|
|
|
|
|
|
|
let woot;
|
|
|
|
if (FEATURE_A) {
|
|
|
|
woot = () => 'woot';
|
|
|
|
} else if (FEATURE_B) {
|
|
|
|
woot = () => 'toow';
|
|
|
|
}
|
|
|
|
"
|
|
|
|
);
|
2023-03-06 06:33:52 +03:00
|
|
|
|
|
|
|
test!(
|
|
|
|
::swc_ecma_parser::Syntax::default(),
|
|
|
|
|tester| tr(tester, &[("foo", &[("bar", "true")])]),
|
|
|
|
namespace_import,
|
|
|
|
r#"
|
|
|
|
import * as foo from 'foo';
|
|
|
|
console.log(foo.bar)
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
test!(
|
|
|
|
::swc_ecma_parser::Syntax::default(),
|
|
|
|
|tester| tr(tester, &[("foo", &[("bar", "true")])]),
|
|
|
|
namespace_import_computed,
|
|
|
|
r#"
|
|
|
|
import * as foo from 'foo';
|
|
|
|
console.log(foo["bar"])
|
|
|
|
"#
|
|
|
|
);
|
2023-03-07 07:10:06 +03:00
|
|
|
|
|
|
|
test!(
|
|
|
|
::swc_ecma_parser::Syntax::default(),
|
|
|
|
|tester| tr(
|
|
|
|
tester,
|
|
|
|
&[("testModule", &[("testMap", "{ 'var': 'value' }")])]
|
|
|
|
),
|
|
|
|
issue_7025,
|
|
|
|
r#"
|
|
|
|
import { testMap } from "testModule";
|
|
|
|
testMap['var'];
|
2023-10-30 04:38:59 +03:00
|
|
|
"#
|
2023-03-07 07:10:06 +03:00
|
|
|
);
|
2023-03-09 17:18:46 +03:00
|
|
|
|
|
|
|
test!(
|
|
|
|
::swc_ecma_parser::Syntax::default(),
|
|
|
|
|tester| tr(tester, &[("foo", &[("bar", "true")])]),
|
|
|
|
use_as_object_prop_shorthand,
|
|
|
|
r#"
|
|
|
|
import { bar } from 'foo';
|
|
|
|
console.log({ bar });
|
|
|
|
"#
|
|
|
|
);
|
2023-06-30 05:12:45 +03:00
|
|
|
|
|
|
|
test!(
|
|
|
|
::swc_ecma_parser::Syntax::default(),
|
|
|
|
|tester| tr(tester, &[("my-mod", &[("default", "true")])]),
|
|
|
|
default_import_issue_7601,
|
|
|
|
r#"
|
|
|
|
import something from 'my-mod';
|
|
|
|
console.log(something);
|
|
|
|
"#
|
|
|
|
);
|
2023-09-16 12:17:26 +03:00
|
|
|
|
|
|
|
#[testing::fixture("tests/const-modules/**/input.js")]
|
|
|
|
fn const_modules_test(input: PathBuf) {
|
|
|
|
let globals = input.with_file_name("globals.json");
|
|
|
|
let output = input.with_file_name("output.js");
|
|
|
|
|
|
|
|
test_fixture(
|
|
|
|
::swc_ecma_parser::Syntax::default(),
|
|
|
|
&|t| {
|
|
|
|
let globals = read_to_string(&globals)
|
|
|
|
.ok()
|
|
|
|
.and_then(|s| serde_json::from_str(&s).ok())
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
|
|
|
const_modules(t.cm.clone(), globals)
|
|
|
|
},
|
|
|
|
&input,
|
|
|
|
&output,
|
|
|
|
Default::default(),
|
|
|
|
);
|
|
|
|
}
|