swc/tests/simple.rs
강동윤 0ac55ae68b
Improve ux for rust users (#909)
testing:
 - Remove dependency on relative_path

swc_common:
 - Span's byte positions are now self-contained and `GLOBALS` is not required while parsing.
 - Changed `Comments` into a trait.
 - Provide single-threaded implementation of `Comments`
 - Cargo feature `tty-emiiter` (To remove tty related stuffs )
 - Cargo feature `sourcemap` (To remove sourcemap for web assets)
 - Removed dependency on dashmap

swc_ecma_parser:
 - No duplicated comments.
 - Removed dependency on once_cell and regex
 - Add a test suite to visualize and test span of nodes.

swc_ecma_utils:
 - Removed dependency on parser

swc:
 - Remove dependency on derive_more and path-clean
 - Add multi-threaded implementation of `Comments`

swc_ecmascript:
 - A new crate contains `ast`, `codegen`, `parser`, `utils`, `visit`.
2020-07-31 18:49:07 +09:00

97 lines
2.3 KiB
Rust

use std::sync::Arc;
use swc::{
config::{Config, JscConfig, Options},
Compiler,
};
use swc_common::FileName;
use swc_ecma_parser::{EsConfig, Syntax};
use testing::Tester;
fn compile(src: &str, options: Options) -> String {
Tester::new()
.print_errors(|cm, handler| {
let c = Compiler::new(cm.clone(), Arc::new(handler));
let fm = cm.new_source_file(FileName::Real("input.js".into()), src.into());
let s = c.process_js_file(
fm,
&Options {
is_module: true,
..options
},
);
match s {
Ok(v) => {
if c.handler.has_errors() {
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 {
config: Some(Config {
jsc: JscConfig {
syntax: Some(Syntax::Es(EsConfig {
nullish_coalescing: true,
optional_chaining: true,
..Default::default()
})),
..Default::default()
},
..Default::default()
}),
swcrc: false,
..Default::default()
},
);
}
#[test]
fn issue_834_3() {
compile(
"const someValue = 'test' ?? 'default value';",
Options {
config: Some(Config {
jsc: JscConfig {
syntax: Some(Syntax::Es(EsConfig {
nullish_coalescing: true,
..Default::default()
})),
..Default::default()
},
..Default::default()
}),
swcrc: false,
..Default::default()
},
);
}