use ezno_parser::{ASTNode, Module, ParseOptions, ToStringOptions};
use pretty_assertions::assert_eq;
#[test]
fn declarations() {
let input = r#"
const x = ;
const y =
const z = 2
"#
.trim_start()
.replace(" ", "\t");
let module = Module::from_string(
input.clone(),
ParseOptions { partial_syntax: true, ..Default::default() },
)
.unwrap();
let _output = module
.to_string(&ToStringOptions { expect_markers: true, ..ToStringOptions::typescript() });
assert!(Module::from_string(
input.clone(),
ParseOptions { partial_syntax: false, ..Default::default() },
)
.is_err());
}
#[test]
fn in_statements() {
let input = r#"
if () {
return 2
}"#
.trim_start()
.replace(" ", "\t");
let module = Module::from_string(
input.clone(),
ParseOptions { partial_syntax: true, ..Default::default() },
)
.unwrap();
let output = module
.to_string(&ToStringOptions { expect_markers: true, ..ToStringOptions::typescript() });
assert!(Module::from_string(
input.clone(),
ParseOptions { partial_syntax: false, ..Default::default() },
)
.is_err());
assert_eq!(output, input);
}
#[test]
fn type_annotations() {
let input = r#"
const x: = 5;
function y(c: ) {
return 2
}"#
.trim_start()
.replace(" ", "\t");
let module = Module::from_string(
input.clone(),
ParseOptions { partial_syntax: true, ..Default::default() },
)
.unwrap();
let output = module
.to_string(&ToStringOptions { expect_markers: true, ..ToStringOptions::typescript() });
assert!(Module::from_string(
input.clone(),
ParseOptions { partial_syntax: false, ..Default::default() },
)
.is_err());
assert_eq!(output, input);
}
#[test]
fn invalid_syntax() {
let sources = [("", true), ("][", false), ("{}}", false)];
for (source, is_okay) in sources.into_iter() {
let result = Module::from_string(
source.to_owned(),
ParseOptions { partial_syntax: true, ..Default::default() },
);
assert_eq!(result.is_ok(), is_okay, "Source = {:?} (parsed {:?})", source, result);
}
}