diff --git a/Cargo.lock b/Cargo.lock index 6471bd852..c2502bf01 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1781,6 +1781,7 @@ dependencies = [ "oxc_parser", "oxc_span", "oxc_syntax", + "pico-args", ] [[package]] diff --git a/crates/oxc_prettier/Cargo.toml b/crates/oxc_prettier/Cargo.toml index fc0b0dff8..4220ab115 100644 --- a/crates/oxc_prettier/Cargo.toml +++ b/crates/oxc_prettier/Cargo.toml @@ -29,3 +29,4 @@ bitflags = { workspace = true } [dev-dependencies] oxc_parser = { workspace = true } oxc_span = { workspace = true } +pico-args = { workspace = true } diff --git a/crates/oxc_prettier/examples/prettier.rs b/crates/oxc_prettier/examples/prettier.rs index ab910e1ea..e65233f85 100644 --- a/crates/oxc_prettier/examples/prettier.rs +++ b/crates/oxc_prettier/examples/prettier.rs @@ -1,10 +1,11 @@ -use std::{env, path::Path}; +use std::path::Path; + +use pico_args::Arguments; use oxc_allocator::Allocator; use oxc_parser::Parser; -use oxc_span::SourceType; - use oxc_prettier::{Prettier, PrettierOptions}; +use oxc_span::SourceType; // Instruction: // create a `test.js`, @@ -12,13 +13,22 @@ use oxc_prettier::{Prettier, PrettierOptions}; // or `just example prettier` fn main() { - let name = env::args().nth(1).unwrap_or_else(|| "test.js".to_string()); + 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::default()) - .build(&ret.program); + let output = Prettier::new( + &allocator, + &source_text, + ret.trivias, + PrettierOptions { semi, ..PrettierOptions::default() }, + ) + .build(&ret.program); println!("{output}"); } diff --git a/justfile b/justfile index c6b55644a..b5f011055 100755 --- a/justfile +++ b/justfile @@ -35,8 +35,8 @@ watch command: cargo watch --no-vcs-ignores -i '*snap*' -x '{{command}}' # Run the example in `parser`, `formatter`, `linter` -example tool: - just watch 'run -p oxc_{{tool}} --example {{tool}}' +example tool *args='': + just watch 'run -p oxc_{{tool}} --example {{tool}} -- {{args}}' # Format all files fmt: