mirror of
https://github.com/danbulant/oxc
synced 2026-05-22 05:38:54 +00:00
feat(prettier): add --no-semi to prettier example (#1588)
This commit is contained in:
parent
1554f7c0d2
commit
6ec257ba2d
4 changed files with 20 additions and 8 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -1781,6 +1781,7 @@ dependencies = [
|
|||
"oxc_parser",
|
||||
"oxc_span",
|
||||
"oxc_syntax",
|
||||
"pico-args",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
|
|||
|
|
@ -29,3 +29,4 @@ bitflags = { workspace = true }
|
|||
[dev-dependencies]
|
||||
oxc_parser = { workspace = true }
|
||||
oxc_span = { workspace = true }
|
||||
pico-args = { workspace = true }
|
||||
|
|
|
|||
|
|
@ -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}");
|
||||
}
|
||||
|
|
|
|||
4
justfile
4
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:
|
||||
|
|
|
|||
Loading…
Reference in a new issue