2020-06-11 11:49:40 +03:00
|
|
|
use swc::{
|
|
|
|
config::{Config, JscConfig, Options},
|
|
|
|
Compiler,
|
|
|
|
};
|
|
|
|
use swc_common::FileName;
|
2021-03-27 15:06:47 +03:00
|
|
|
use swc_ecma_ast::EsVersion;
|
2021-08-13 13:03:04 +03:00
|
|
|
use swc_ecma_parser::{EsConfig, Syntax, TsConfig};
|
2020-06-11 11:49:40 +03:00
|
|
|
use testing::Tester;
|
|
|
|
|
|
|
|
fn compile(src: &str, options: Options) -> String {
|
|
|
|
Tester::new()
|
|
|
|
.print_errors(|cm, handler| {
|
2021-08-13 10:05:40 +03:00
|
|
|
let c = Compiler::new(cm.clone());
|
2020-06-11 11:49:40 +03:00
|
|
|
|
|
|
|
let fm = cm.new_source_file(FileName::Real("input.js".into()), src.into());
|
|
|
|
let s = c.process_js_file(
|
|
|
|
fm,
|
2021-08-13 10:05:40 +03:00
|
|
|
&handler,
|
2020-06-11 11:49:40 +03:00
|
|
|
&Options {
|
|
|
|
is_module: true,
|
|
|
|
..options
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
match s {
|
|
|
|
Ok(v) => {
|
2021-08-13 10:05:40 +03:00
|
|
|
if handler.has_errors() {
|
2020-06-11 11:49:40 +03:00
|
|
|
Err(())
|
|
|
|
} else {
|
|
|
|
Ok(v.code.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(..) => Err(()),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_834_1() {
|
|
|
|
compile(
|
|
|
|
"var foo = 2n + 7n;",
|
|
|
|
Options {
|
|
|
|
swcrc: false,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_834_2() {
|
|
|
|
compile(
|
|
|
|
"var ano = { some: {
|
|
|
|
ne: {
|
|
|
|
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
var foo = ano.some.ne?.sdf?.snop;
|
|
|
|
const someValue = 'test' ?? 'default value';",
|
|
|
|
Options {
|
2021-04-08 15:55:02 +03:00
|
|
|
config: Config {
|
2020-06-11 11:49:40 +03:00
|
|
|
jsc: JscConfig {
|
|
|
|
syntax: Some(Syntax::Es(EsConfig {
|
|
|
|
nullish_coalescing: true,
|
|
|
|
optional_chaining: true,
|
|
|
|
..Default::default()
|
|
|
|
})),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
..Default::default()
|
2021-04-08 15:55:02 +03:00
|
|
|
},
|
2020-06-11 11:49:40 +03:00
|
|
|
swcrc: false,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2020-06-12 20:05:49 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_834_3() {
|
|
|
|
compile(
|
|
|
|
"const someValue = 'test' ?? 'default value';",
|
|
|
|
Options {
|
2021-04-08 15:55:02 +03:00
|
|
|
config: Config {
|
2020-06-12 20:05:49 +03:00
|
|
|
jsc: JscConfig {
|
|
|
|
syntax: Some(Syntax::Es(EsConfig {
|
|
|
|
nullish_coalescing: true,
|
|
|
|
..Default::default()
|
|
|
|
})),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
..Default::default()
|
2021-04-08 15:55:02 +03:00
|
|
|
},
|
2020-06-12 20:05:49 +03:00
|
|
|
swcrc: false,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2021-03-27 15:06:47 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tsx_escape_xhtml() {
|
|
|
|
let source = r#"<div id="abc>" />"#;
|
|
|
|
|
2021-05-09 08:23:03 +03:00
|
|
|
let expected = r#"/*#__PURE__*/ React.createElement("div", {
|
2021-03-27 15:06:47 +03:00
|
|
|
id: "abc>"
|
|
|
|
});
|
|
|
|
"#;
|
|
|
|
|
|
|
|
let compiled_es5 = compile(
|
|
|
|
source,
|
|
|
|
Options {
|
2021-04-08 15:55:02 +03:00
|
|
|
config: Config {
|
2021-03-27 15:06:47 +03:00
|
|
|
jsc: JscConfig {
|
|
|
|
syntax: Some(Syntax::Typescript(TsConfig {
|
|
|
|
tsx: true,
|
|
|
|
..Default::default()
|
|
|
|
})),
|
2021-04-08 15:55:02 +03:00
|
|
|
target: Some(EsVersion::Es5),
|
2021-03-27 15:06:47 +03:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
..Default::default()
|
2021-04-08 15:55:02 +03:00
|
|
|
},
|
2021-03-27 15:06:47 +03:00
|
|
|
swcrc: false,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(compiled_es5, expected);
|
|
|
|
|
|
|
|
let compiled_es2020 = compile(
|
|
|
|
source,
|
|
|
|
Options {
|
2021-04-08 15:55:02 +03:00
|
|
|
config: Config {
|
2021-03-27 15:06:47 +03:00
|
|
|
jsc: JscConfig {
|
|
|
|
syntax: Some(Syntax::Typescript(TsConfig {
|
|
|
|
tsx: true,
|
|
|
|
..Default::default()
|
|
|
|
})),
|
2021-04-08 15:55:02 +03:00
|
|
|
target: Some(EsVersion::Es2020),
|
2021-03-27 15:06:47 +03:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
..Default::default()
|
2021-04-08 15:55:02 +03:00
|
|
|
},
|
2021-03-27 15:06:47 +03:00
|
|
|
swcrc: false,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(compiled_es2020, expected);
|
|
|
|
}
|