mirror of
https://github.com/oxc-project/oxc.git
synced 2026-09-14 19:36:11 +08:00
030857b509
Remove the miette `Report` wrapper and update oxc-miette to v4's narrowed diagnostic and source protocol. AI-assisted by OpenAI Codex.
58 lines
1.9 KiB
Rust
58 lines
1.9 KiB
Rust
mod deno;
|
|
|
|
use std::{fmt::Write, fs, path::Path, sync::Arc};
|
|
|
|
use oxc_allocator::Allocator;
|
|
use oxc_codegen::Codegen;
|
|
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();
|
|
assert!(
|
|
parser_ret.diagnostics.is_empty(),
|
|
"Parser errors for {}: {:?}",
|
|
path.display(),
|
|
parser_ret.diagnostics
|
|
);
|
|
|
|
let id_ret =
|
|
IsolatedDeclarations::new(&allocator, IsolatedDeclarationsOptions { strip_internal: true })
|
|
.build(&parser_ret.program);
|
|
let code = Codegen::new().build(&id_ret.program).code;
|
|
|
|
let mut snapshot =
|
|
format!("```\n==================== .D.TS ====================\n\n{code}\n\n");
|
|
if !id_ret.diagnostics.is_empty() {
|
|
let source = Arc::new(source_text.to_string());
|
|
let error_messages = id_ret
|
|
.diagnostics
|
|
.iter()
|
|
.map(|diagnostic| diagnostic.clone().render_with_source_code(Arc::clone(&source)))
|
|
.fold(String::new(), |output, error| output + &error);
|
|
|
|
write!(
|
|
snapshot,
|
|
"==================== Errors ====================\n{error_messages}\n\n```"
|
|
)
|
|
.unwrap();
|
|
}
|
|
|
|
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);
|
|
});
|
|
});
|
|
}
|