-
-
Notifications
You must be signed in to change notification settings - Fork 540
/
Copy pathprettier.rs
36 lines (29 loc) · 1.06 KB
/
prettier.rs
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
use std::path::Path;
use pico_args::Arguments;
use oxc_allocator::Allocator;
use oxc_parser::Parser;
use oxc_prettier::{Prettier, PrettierOptions, TrailingComma};
use oxc_span::SourceType;
// Instruction:
// create a `test.js`,
// run `cargo run -p oxc_prettier --example prettier`
// or `just example prettier`
fn main() -> std::io::Result<()> {
let mut args = Arguments::from_env();
let name = args.subcommand().ok().flatten().unwrap_or_else(|| String::from("test.js"));
let semi = !args.contains("--no-semi");
let path = Path::new(&name);
let source_text = std::fs::read_to_string(path)?;
let allocator = Allocator::default();
let source_type = SourceType::from_path(path).unwrap();
let ret = Parser::new(&allocator, &source_text, source_type).preserve_parens(false).parse();
let output = Prettier::new(
&allocator,
&source_text,
ret.trivias,
PrettierOptions { semi, trailing_comma: TrailingComma::All, ..PrettierOptions::default() },
)
.build(&ret.program);
println!("{output}");
Ok(())
}