feat(prettier): add --no-semi to prettier example (#1588)

This commit is contained in:
Boshen 2023-11-29 23:33:35 +08:00 committed by GitHub
parent 1554f7c0d2
commit 6ec257ba2d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 8 deletions

1
Cargo.lock generated
View file

@ -1781,6 +1781,7 @@ dependencies = [
"oxc_parser",
"oxc_span",
"oxc_syntax",
"pico-args",
]
[[package]]

View file

@ -29,3 +29,4 @@ bitflags = { workspace = true }
[dev-dependencies]
oxc_parser = { workspace = true }
oxc_span = { workspace = true }
pico-args = { workspace = true }

View file

@ -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}");
}

View file

@ -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: