oxc/crates/oxc_isolated_declarations/tests/mod.rs
2024-10-27 13:30:20 +00:00

49 lines
1.7 KiB
Rust

mod deno;
use std::{fs, path::Path, sync::Arc};
use oxc_allocator::Allocator;
use oxc_codegen::CodeGenerator;
use oxc_isolated_declarations::{IsolatedDeclarations, IsolatedDeclarationsOptions};
use oxc_parser::Parser;
use oxc_span::SourceType;
fn transform(path: &Path, source_text: &str) -> String {
let allocator = Allocator::default();
let source_type = SourceType::from_path(path).unwrap();
let parser_ret = Parser::new(&allocator, source_text, source_type).parse();
let id_ret =
IsolatedDeclarations::new(&allocator, IsolatedDeclarationsOptions { strip_internal: true })
.build(&parser_ret.program);
let code = CodeGenerator::new().build(&id_ret.program).code;
let mut snapshot =
format!("```\n==================== .D.TS ====================\n\n{code}\n\n");
if !id_ret.errors.is_empty() {
let source = Arc::new(source_text.to_string());
let error_messages = id_ret
.errors
.iter()
.map(|d| d.clone().with_source_code(Arc::clone(&source)))
.fold(String::new(), |s, error| s + &format!("{error:?}"));
snapshot.push_str(&format!(
"==================== Errors ====================\n{error_messages}\n\n```"
));
}
snapshot
}
#[test]
fn snapshots() {
insta::glob!("fixtures/*.{ts,tsx}", |path| {
let source_text = fs::read_to_string(path).unwrap();
let snapshot = transform(path, &source_text);
let name = path.file_stem().unwrap().to_str().unwrap();
insta::with_settings!({ prepend_module_to_snapshot => false, snapshot_suffix => "", omit_expression => true }, {
insta::assert_snapshot!(name, snapshot);
});
});
}