mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
34 lines
1 KiB
Rust
34 lines
1 KiB
Rust
use std::path::Path;
|
|
|
|
use pico_args::Arguments;
|
|
|
|
use oxc_allocator::Allocator;
|
|
use oxc_parser::Parser;
|
|
use oxc_prettier::{Prettier, PrettierOptions};
|
|
use oxc_span::SourceType;
|
|
|
|
// Instruction:
|
|
// create a `test.js`,
|
|
// run `cargo run -p oxc_prettier --example prettier`
|
|
// or `just example prettier`
|
|
|
|
fn main() {
|
|
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).unwrap_or_else(|_| panic!("{name} not found"));
|
|
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, ..PrettierOptions::default() },
|
|
)
|
|
.build(&ret.program);
|
|
println!("{output}");
|
|
}
|