Files
oxc-project__oxc/crates/oxc_transformer/tests/integrations/main.rs
Boshen 8b7420a7e8 refactor(transformer): decouple React Compiler (#25065)
Moves React Compiler integration out of `oxc` and `oxc_transformer` into the dedicated binding.

AI-assisted.
2026-07-29 09:46:48 +00:00

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)
}