mirror of
https://github.com/oxc-project/oxc.git
synced 2026-09-14 19:36:11 +08:00
8b7420a7e8
Moves React Compiler integration out of `oxc` and `oxc_transformer` into the dedicated binding. AI-assisted.
53 lines
1.7 KiB
Rust
53 lines
1.7 KiB
Rust
mod comments;
|
|
mod enum_eval;
|
|
mod es_target;
|
|
mod helper_call;
|
|
mod targets;
|
|
|
|
use std::path::Path;
|
|
|
|
use oxc_allocator::Allocator;
|
|
use oxc_codegen::{Codegen, CodegenOptions};
|
|
use oxc_diagnostics::Diagnostics;
|
|
use oxc_parser::Parser;
|
|
use oxc_semantic::SemanticBuilder;
|
|
use oxc_span::SourceType;
|
|
use oxc_transformer::{TransformOptions, Transformer};
|
|
|
|
/// # Panics
|
|
/// Panics if there are parse errors.
|
|
pub fn codegen(source_text: &str, source_type: SourceType) -> String {
|
|
let allocator = Allocator::default();
|
|
let ret = Parser::new(&allocator, source_text, source_type).parse();
|
|
assert!(ret.diagnostics.is_empty());
|
|
Codegen::new()
|
|
.with_options(CodegenOptions { single_quote: true, ..CodegenOptions::default() })
|
|
.build(&ret.program)
|
|
.code
|
|
}
|
|
|
|
pub(crate) fn test(source_text: &str, options: &TransformOptions) -> Result<String, Diagnostics> {
|
|
test_with_source_type(source_text, SourceType::default(), options)
|
|
}
|
|
|
|
pub(crate) fn test_with_source_type(
|
|
source_text: &str,
|
|
source_type: SourceType,
|
|
options: &TransformOptions,
|
|
) -> Result<String, Diagnostics> {
|
|
let allocator = Allocator::default();
|
|
let ret = Parser::new(&allocator, source_text, source_type).parse();
|
|
let mut program = ret.program;
|
|
let scoping = SemanticBuilder::new().build(&program).semantic.into_scoping();
|
|
let ret = Transformer::new(&allocator, Path::new(""), options)
|
|
.build_with_scoping(scoping, &mut program);
|
|
if !ret.diagnostics.is_empty() {
|
|
return Err(ret.diagnostics);
|
|
}
|
|
let code = Codegen::new()
|
|
.with_options(CodegenOptions { single_quote: true, ..CodegenOptions::default() })
|
|
.build(&program)
|
|
.code;
|
|
Ok(code)
|
|
}
|