oxc/crates/oxc_semantic/examples/simple.rs
Boshen 889837704c
feat(semantic): cfg prototype (#2019)
Co-authored-by: U9G <git@u9g.dev>
2024-01-25 20:19:35 +08:00

23 lines
835 B
Rust

use std::{env, path::Path};
use oxc_allocator::Allocator;
use oxc_parser::Parser;
use oxc_semantic::SemanticBuilder;
use oxc_span::SourceType;
// Instruction:
// create a `test.js`,
// run `cargo run -p oxc_query --example simple`
// or `cargo watch -x "run -p oxc_query --example simple"`
fn main() {
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 allocator = Allocator::default();
let source_type = SourceType::from_path(path).unwrap();
let ret = Parser::new(&allocator, &source_text, source_type).parse();
let program = allocator.alloc(ret.program);
SemanticBuilder::new(&source_text, source_type).with_trivias(ret.trivias).build(program);
}