mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 20:32:10 +00:00
fix(semantic): fix incorrect semantic example (#2198)
This commit is contained in:
parent
8e7c376898
commit
972be831e9
1 changed files with 20 additions and 5 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
use std::{env, path::Path};
|
use std::{env, path::Path, sync::Arc};
|
||||||
|
|
||||||
|
use itertools::Itertools;
|
||||||
use oxc_allocator::Allocator;
|
use oxc_allocator::Allocator;
|
||||||
use oxc_parser::Parser;
|
use oxc_parser::Parser;
|
||||||
use oxc_semantic::SemanticBuilder;
|
use oxc_semantic::SemanticBuilder;
|
||||||
|
|
@ -7,17 +8,31 @@ use oxc_span::SourceType;
|
||||||
|
|
||||||
// Instruction:
|
// Instruction:
|
||||||
// create a `test.js`,
|
// create a `test.js`,
|
||||||
// run `cargo run -p oxc_query --example simple`
|
// run `cargo run -p oxc_semantic --example simple`
|
||||||
// or `cargo watch -x "run -p oxc_query --example simple"`
|
// or `just watch "run -p oxc_semantic --example simple"`
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let name = env::args().nth(1).unwrap_or_else(|| "test.js".to_string());
|
let name = env::args().nth(1).unwrap_or_else(|| "test.js".to_string());
|
||||||
let path = Path::new(&name);
|
let path = Path::new(&name);
|
||||||
let source_text = std::fs::read_to_string(path).unwrap_or_else(|_| panic!("{name} not found"));
|
let source_text =
|
||||||
|
Arc::new(std::fs::read_to_string(path).unwrap_or_else(|_| panic!("{name} not found")));
|
||||||
let allocator = Allocator::default();
|
let allocator = Allocator::default();
|
||||||
let source_type = SourceType::from_path(path).unwrap();
|
let source_type = SourceType::from_path(path).unwrap();
|
||||||
let ret = Parser::new(&allocator, &source_text, source_type).parse();
|
let ret = Parser::new(&allocator, &source_text, source_type).parse();
|
||||||
|
|
||||||
let program = allocator.alloc(ret.program);
|
let program = allocator.alloc(ret.program);
|
||||||
SemanticBuilder::new(&source_text, source_type).with_trivias(ret.trivias).build(program);
|
let semantic = SemanticBuilder::new(&source_text, source_type)
|
||||||
|
.with_check_syntax_error(true)
|
||||||
|
.with_trivias(ret.trivias)
|
||||||
|
.build(program);
|
||||||
|
|
||||||
|
if !semantic.errors.is_empty() {
|
||||||
|
let error_message: String = semantic
|
||||||
|
.errors
|
||||||
|
.into_iter()
|
||||||
|
.map(|error| error.with_source_code(Arc::clone(&source_text)).to_string())
|
||||||
|
.join("\n\n");
|
||||||
|
|
||||||
|
panic!("Semantic analysis failed:\n\n{error_message}",);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue