refactor: remove panic! from examples (#2454)

relates #2308
This commit is contained in:
Boshen 2024-02-20 16:18:39 +08:00 committed by GitHub
parent cd48e1e9d9
commit a2c173de57
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 37 additions and 27 deletions

View file

@ -9,10 +9,10 @@ use oxc_span::SourceType;
// 1. create a `test.js`
// 2. run `cargo run -p oxc_codegen --example codegen` or `just example codegen`
fn main() {
fn main() -> std::io::Result<()> {
let name = env::args().nth(1).unwrap_or_else(|| "test.js".to_string());
let path = Path::new(&name);
let source_text = std::fs::read_to_string(path).unwrap_or_else(|_| panic!("{name} not found"));
let source_text = std::fs::read_to_string(path)?;
let source_type = SourceType::from_path(path).unwrap();
let allocator = Allocator::default();
let ret = Parser::new(&allocator, &source_text, source_type).parse();
@ -22,7 +22,7 @@ fn main() {
let error = error.with_source_code(source_text.clone());
println!("{error:?}");
}
return;
return Ok(());
}
println!("Original:");
@ -37,4 +37,6 @@ fn main() {
let minified = Codegen::<true>::new(source_text.len(), codegen_options).build(&ret.program);
println!("Minified:");
println!("{minified}");
Ok(())
}

View file

@ -17,10 +17,10 @@ use oxc_span::{SourceType, Span};
// run `cargo run -p oxc_linter --example linter`
// or `cargo watch -x "run -p oxc_linter --example linter"`
fn main() {
fn main() -> std::io::Result<()> {
let name = env::args().nth(1).unwrap_or_else(|| "test.js".to_string());
let path = Path::new(&name);
let source_text = std::fs::read_to_string(path).unwrap_or_else(|_| panic!("{name} not found"));
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).parse();
@ -28,7 +28,7 @@ fn main() {
// Handle parser errors
if !ret.errors.is_empty() {
print_errors(&source_text, ret.errors);
return;
return Ok(());
}
let program = allocator.alloc(ret.program);
@ -52,12 +52,13 @@ fn main() {
}
}
if !errors.is_empty() {
if errors.is_empty() {
println!("Success!");
} else {
print_errors(&source_text, errors);
return;
}
println!("Success!");
Ok(())
}
fn print_errors(source_text: &str, errors: Vec<oxc_diagnostics::Error>) {

View file

@ -13,7 +13,7 @@ use pico_args::Arguments;
// run `cargo run -p oxc_minifier --example minifier`
// or `just watch "run -p oxc_minifier --example minifier"`
fn main() {
fn main() -> std::io::Result<()> {
let mut args = Arguments::from_env();
let name = args.subcommand().ok().flatten().unwrap_or_else(|| String::from("test.js"));
@ -22,7 +22,7 @@ fn main() {
let twice = args.contains("--twice");
let path = Path::new(&name);
let source_text = std::fs::read_to_string(path).unwrap_or_else(|_| panic!("{name} not found"));
let source_text = std::fs::read_to_string(path)?;
let source_type = SourceType::from_path(path).unwrap();
let printed = minify(&source_text, source_type, mangle, whitespace);
@ -32,6 +32,8 @@ fn main() {
let printed = minify(&printed, source_type, mangle, whitespace);
println!("{printed}");
}
Ok(())
}
fn minify(source_text: &str, source_type: SourceType, mangle: bool, whitespace: bool) -> String {

View file

@ -9,10 +9,10 @@ use oxc_span::SourceType;
// run `cargo run -p oxc_parser --example parser`
// or `cargo watch -x "run -p oxc_parser --example parser"`
fn main() {
fn main() -> Result<(), String> {
let name = env::args().nth(1).unwrap_or_else(|| "test.js".to_string());
let path = Path::new(&name);
let source_text = std::fs::read_to_string(path).unwrap_or_else(|_| panic!("{name} not found"));
let source_text = std::fs::read_to_string(path).map_err(|_| format!("Missing '{name}'"))?;
let allocator = Allocator::default();
let source_type = SourceType::from_path(path).unwrap();
let ret = Parser::new(&allocator, &source_text, source_type).parse();
@ -26,4 +26,6 @@ fn main() {
println!("{error:?}");
}
}
Ok(())
}

View file

@ -10,10 +10,10 @@ use oxc_span::SourceType;
// run `cargo run -p oxc_parser --example visitor`
// or `cargo watch -x "run -p oxc_parser --example visitor"`
fn main() {
fn main() -> std::io::Result<()> {
let name = env::args().nth(1).unwrap_or_else(|| "test.js".to_string());
let path = Path::new(&name);
let source_text = std::fs::read_to_string(path).unwrap_or_else(|_| panic!("{name} not found"));
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).parse();
@ -28,6 +28,8 @@ fn main() {
let mut ast_pass = ASTPass::default();
ast_pass.visit_program(&program);
println!("{ast_pass:?}");
Ok(())
}
#[derive(Debug, Default)]

View file

@ -12,14 +12,14 @@ use oxc_span::SourceType;
// run `cargo run -p oxc_prettier --example prettier`
// or `just example prettier`
fn main() {
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).unwrap_or_else(|_| panic!("{name} not found"));
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();
@ -31,4 +31,6 @@ fn main() {
)
.build(&ret.program);
println!("{output}");
Ok(())
}

View file

@ -16,7 +16,7 @@ use petgraph::dot::{Config, Dot};
// - CFG blocks (test.cfg.txt)
// - CFG graph (test.dot)
fn main() -> Result<(), std::io::Error> {
fn main() -> std::io::Result<()> {
let test_file_name = env::args().nth(1).unwrap_or_else(|| "test.js".to_string());
let ast_file_name = env::args().nth(1).unwrap_or_else(|| "test.ast.txt".to_string());
let cfg_file_name = env::args().nth(1).unwrap_or_else(|| "test.cfg.txt".to_string());
@ -27,10 +27,7 @@ fn main() -> Result<(), std::io::Error> {
let cfg_file_path = Path::new(&cfg_file_name);
let dot_file_path = Path::new(&dot_file_name);
let source_text = Arc::new(
std::fs::read_to_string(test_file_path)
.unwrap_or_else(|_| panic!("{test_file_name} not found")),
);
let source_text = Arc::new(std::fs::read_to_string(test_file_path)?);
let allocator = Allocator::default();
let source_type = SourceType::from_path(test_file_path).unwrap();
let ret = Parser::new(&allocator, &source_text, source_type).parse();
@ -51,7 +48,8 @@ fn main() -> Result<(), std::io::Error> {
.map(|error| error.with_source_code(Arc::clone(&source_text)).to_string())
.join("\n\n");
panic!("Semantic analysis failed:\n\n{error_message}",);
println!("Semantic analysis failed:\n\n{error_message}",);
return Ok(());
}
let mut ast_nodes_by_block = HashMap::<_, Vec<_>>::new();

View file

@ -11,11 +11,10 @@ use oxc_span::SourceType;
// run `cargo run -p oxc_semantic --example simple`
// or `just watch "run -p oxc_semantic --example simple"`
fn main() {
fn main() -> std::io::Result<()> {
let name = env::args().nth(1).unwrap_or_else(|| "test.js".to_string());
let path = Path::new(&name);
let source_text =
Arc::new(std::fs::read_to_string(path).unwrap_or_else(|_| panic!("{name} not found")));
let source_text = Arc::new(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).parse();
@ -34,6 +33,8 @@ fn main() {
.map(|error| error.with_source_code(Arc::clone(&source_text)).to_string())
.join("\n\n");
panic!("Semantic analysis failed:\n\n{error_message}",);
println!("Semantic analysis failed:\n\n{error_message}",);
}
Ok(())
}