-
-
Notifications
You must be signed in to change notification settings - Fork 877
Expand file tree
/
Copy pathisolated_declarations.rs
More file actions
53 lines (44 loc) · 1.67 KB
/
isolated_declarations.rs
File metadata and controls
53 lines (44 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#![allow(clippy::print_stdout)]
use std::{env, path::Path};
use oxc_allocator::Allocator;
use oxc_codegen::{CodeGenerator, CommentOptions};
use oxc_isolated_declarations::{IsolatedDeclarations, IsolatedDeclarationsOptions};
use oxc_parser::Parser;
use oxc_span::SourceType;
// Instruction:
// create a `test.js`,
// run `cargo run -p oxc_isolated_declarations --example isolated_declarations`
// or `just example isolated_declarations`
fn main() {
let name = env::args().nth(1).unwrap_or_else(|| "test.tsx".to_string());
let path = Path::new(&name);
let source_text = std::fs::read_to_string(path).expect("{name} not found");
let allocator = Allocator::default();
let source_type = SourceType::from_path(path).unwrap();
let ret = Parser::new(&allocator, &source_text, source_type).parse();
if !ret.errors.is_empty() {
for error in ret.errors {
let error = error.with_source_code(source_text.clone());
println!("{error:?}");
}
return;
}
println!("Original:\n");
println!("{source_text}\n");
let id_ret =
IsolatedDeclarations::new(&allocator, IsolatedDeclarationsOptions { strip_internal: true })
.build(&ret.program);
let printed = CodeGenerator::new()
.enable_comment(&ret.program, CommentOptions { preserve_annotate_comments: false })
.build(&id_ret.program)
.code;
println!("Dts Emit:\n");
println!("{printed}\n");
if !id_ret.errors.is_empty() {
println!("Transformed dts failed:\n");
for error in id_ret.errors {
let error = error.with_source_code(source_text.clone());
println!("{error:?}");
}
}
}