feat(transformer): call build module record (#2529)

This commit is contained in:
Dunqing 2024-02-28 14:35:35 +08:00 committed by GitHub
parent 0ddfc856d2
commit f760108094
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 22 additions and 4 deletions

View file

@ -146,7 +146,7 @@ impl<'a> SemanticBuilder<'a> {
pub fn build_module_record( pub fn build_module_record(
mut self, mut self,
resolved_absolute_path: PathBuf, resolved_absolute_path: PathBuf,
program: &'a Program<'a>, program: &Program<'a>,
) -> Self { ) -> Self {
let mut module_record_builder = ModuleRecordBuilder::new(resolved_absolute_path); let mut module_record_builder = ModuleRecordBuilder::new(resolved_absolute_path);
module_record_builder.visit(program); module_record_builder.visit(program);

View file

@ -1,4 +1,7 @@
use std::{env, path::Path}; use std::{
env,
path::{Path, PathBuf},
};
use oxc_allocator::Allocator; use oxc_allocator::Allocator;
use oxc_codegen::{Codegen, CodegenOptions}; use oxc_codegen::{Codegen, CodegenOptions};
@ -37,6 +40,7 @@ fn main() {
let semantic = SemanticBuilder::new(&source_text, source_type) let semantic = SemanticBuilder::new(&source_text, source_type)
.with_trivias(ret.trivias) .with_trivias(ret.trivias)
.build_module_record(PathBuf::new(), &ret.program)
.build(&ret.program) .build(&ret.program)
.semantic; .semantic;

View file

@ -1,3 +1,5 @@
use std::path::PathBuf;
use oxc_allocator::Allocator; use oxc_allocator::Allocator;
use oxc_codegen::{Codegen, CodegenOptions}; use oxc_codegen::{Codegen, CodegenOptions};
use oxc_diagnostics::Error; use oxc_diagnostics::Error;
@ -31,8 +33,13 @@ impl Tester {
fn transform(&self, source_text: &str) -> Result<std::string::String, std::vec::Vec<Error>> { fn transform(&self, source_text: &str) -> Result<std::string::String, std::vec::Vec<Error>> {
let program = Parser::new(&self.allocator, source_text, self.source_type).parse().program; let program = Parser::new(&self.allocator, source_text, self.source_type).parse().program;
let semantic = SemanticBuilder::new(source_text, self.source_type).build(&program).semantic; let semantic = SemanticBuilder::new(source_text, self.source_type)
.build_module_record(PathBuf::new(), &program)
.build(&program)
.semantic;
let program = self.allocator.alloc(program); let program = self.allocator.alloc(program);
Transformer::new(&self.allocator, self.source_type, semantic, self.options.clone()) Transformer::new(&self.allocator, self.source_type, semantic, self.options.clone())
.build(program) .build(program)
.map(move |()| { .map(move |()| {

View file

@ -189,7 +189,10 @@ impl Oxc {
if run_options.transform() { if run_options.transform() {
// FIXME: this should not be duplicated with the linter semantic, // FIXME: this should not be duplicated with the linter semantic,
// we need to fix the API so symbols and scopes can be shared. // we need to fix the API so symbols and scopes can be shared.
let semantic = SemanticBuilder::new(source_text, source_type).build(program).semantic; let semantic = SemanticBuilder::new(source_text, source_type)
.build_module_record(PathBuf::new(), program)
.build(program)
.semantic;
let options = let options =
TransformOptions { target: TransformTarget::ES2015, ..TransformOptions::default() }; TransformOptions { target: TransformTarget::ES2015, ..TransformOptions::default() };
let result = let result =

View file

@ -165,8 +165,10 @@ pub trait TestCase {
let semantic = SemanticBuilder::new(&source_text, source_type) let semantic = SemanticBuilder::new(&source_text, source_type)
.with_trivias(ret.trivias) .with_trivias(ret.trivias)
.build_module_record(PathBuf::new(), &ret.program)
.build(&ret.program) .build(&ret.program)
.semantic; .semantic;
let transformed_program = allocator.alloc(ret.program); let transformed_program = allocator.alloc(ret.program);
let result = Transformer::new(&allocator, source_type, semantic, self.transform_options()) let result = Transformer::new(&allocator, source_type, semantic, self.transform_options())
@ -231,6 +233,7 @@ impl TestCase for ConformanceTestCase {
let ret = Parser::new(&allocator, &input, source_type).parse(); let ret = Parser::new(&allocator, &input, source_type).parse();
let semantic = SemanticBuilder::new(&input, source_type) let semantic = SemanticBuilder::new(&input, source_type)
.with_trivias(ret.trivias) .with_trivias(ret.trivias)
.build_module_record(PathBuf::new(), &ret.program)
.build(&ret.program) .build(&ret.program)
.semantic; .semantic;
let program = allocator.alloc(ret.program); let program = allocator.alloc(ret.program);

View file

@ -99,6 +99,7 @@ impl TypeScriptFixtures {
let semantic_ret = SemanticBuilder::new(&source_text, source_type) let semantic_ret = SemanticBuilder::new(&source_text, source_type)
.with_trivias(parser_ret.trivias) .with_trivias(parser_ret.trivias)
.with_check_syntax_error(true) .with_check_syntax_error(true)
.build_module_record(PathBuf::new(), &parser_ret.program)
.build(&parser_ret.program); .build(&parser_ret.program);
let errors = parser_ret.errors.into_iter().chain(semantic_ret.errors).collect::<Vec<_>>(); let errors = parser_ret.errors.into_iter().chain(semantic_ret.errors).collect::<Vec<_>>();