Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(transformer): always pass in symbols and scopes #5087

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion crates/oxc_transformer/examples/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{env, path::Path};
use oxc_allocator::Allocator;
use oxc_codegen::CodeGenerator;
use oxc_parser::Parser;
use oxc_semantic::SemanticBuilder;
use oxc_span::SourceType;
use oxc_transformer::{
ArrowFunctionsOptions, ES2015Options, ReactOptions, TransformOptions, Transformer,
Expand Down Expand Up @@ -47,6 +48,12 @@ fn main() {
},
..Default::default()
};

let (symbols, scopes) = SemanticBuilder::new(&source_text, source_type)
.build(&program)
.semantic
.into_symbol_table_and_scope_tree();

let _ = Transformer::new(
&allocator,
path,
Expand All @@ -55,7 +62,7 @@ fn main() {
ret.trivias.clone(),
transform_options,
)
.build(&mut program);
.build_with_symbols_and_scopes(symbols, scopes, &mut program);

let printed = CodeGenerator::new().build(&program).source_text;
println!("Transformed:\n");
Expand Down
12 changes: 1 addition & 11 deletions crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use es2021::ES2021;
use oxc_allocator::{Allocator, Vec};
use oxc_ast::{ast::*, Trivias};
use oxc_diagnostics::OxcDiagnostic;
use oxc_semantic::{ScopeTree, SemanticBuilder, SymbolTable};
use oxc_semantic::{ScopeTree, SymbolTable};
use oxc_span::SourceType;
use oxc_traverse::{traverse_mut, Traverse, TraverseCtx};

Expand Down Expand Up @@ -102,16 +102,6 @@ impl<'a> Transformer<'a> {
}
}

pub fn build(mut self, program: &mut Program<'a>) -> TransformerReturn {
let (symbols, scopes) = SemanticBuilder::new(self.ctx.source_text, self.ctx.source_type)
.build(program)
.semantic
.into_symbol_table_and_scope_tree();
let allocator: &'a Allocator = self.ctx.ast.allocator;
let (symbols, scopes) = traverse_mut(&mut self, allocator, program, symbols, scopes);
TransformerReturn { errors: self.ctx.take_errors(), symbols, scopes }
}

pub fn build_with_symbols_and_scopes(
mut self,
symbols: SymbolTable,
Expand Down
6 changes: 5 additions & 1 deletion crates/oxc_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ impl Oxc {
}

if run_options.transform() {
let (symbols, scopes) = SemanticBuilder::new(source_text, source_type)
.build(program)
.semantic
.into_symbol_table_and_scope_tree();
let options = TransformOptions::default();
let result = Transformer::new(
&allocator,
Expand All @@ -250,7 +254,7 @@ impl Oxc {
ret.trivias.clone(),
options,
)
.build(program);
.build_with_symbols_and_scopes(symbols, scopes, program);
if !result.errors.is_empty() {
let errors = result.errors.into_iter().map(Error::from).collect::<Vec<_>>();
self.save_diagnostics(errors);
Expand Down
1 change: 1 addition & 0 deletions napi/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ oxc_parser = { workspace = true }
oxc_span = { workspace = true }
oxc_sourcemap = { workspace = true }
oxc_transformer = { workspace = true }
oxc_semantic = { workspace = true }

napi = { workspace = true }
napi-derive = { workspace = true }
Expand Down
7 changes: 6 additions & 1 deletion napi/transform/src/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use napi_derive::napi;
use crate::{context::TransformContext, isolated_declaration, SourceMap, TransformOptions};
use oxc_allocator::Allocator;
use oxc_codegen::CodegenReturn;
use oxc_semantic::SemanticBuilder;
use oxc_span::SourceType;
use oxc_transformer::Transformer;

Expand Down Expand Up @@ -97,6 +98,10 @@ pub fn transform(
}

fn transpile(ctx: &TransformContext<'_>) -> CodegenReturn {
let (symbols, scopes) = SemanticBuilder::new(ctx.source_text(), ctx.source_type())
.build(&ctx.program())
.semantic
.into_symbol_table_and_scope_tree();
let ret = Transformer::new(
ctx.allocator,
ctx.file_path(),
Expand All @@ -105,7 +110,7 @@ fn transpile(ctx: &TransformContext<'_>) -> CodegenReturn {
ctx.trivias.clone(),
ctx.oxc_options(),
)
.build(&mut ctx.program_mut());
.build_with_symbols_and_scopes(symbols, scopes, &mut ctx.program_mut());

ctx.add_diagnostics(ret.errors);
ctx.codegen().build(&ctx.program())
Expand Down
2 changes: 1 addition & 1 deletion tasks/benchmark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ codspeed_napi = ["criterion2/codspeed", "dep:serde", "dep:serde_json"]
# e.g. `cargo build --release -p oxc_benchmark --bench parser --no-default-features --features parser`
lexer = ["dep:oxc_allocator", "dep:oxc_parser", "dep:oxc_span", "dep:oxc_tasks_common"]
parser = ["dep:oxc_allocator", "dep:oxc_parser", "dep:oxc_span", "dep:oxc_tasks_common"]
transformer = ["dep:oxc_allocator", "dep:oxc_parser", "dep:oxc_span", "dep:oxc_tasks_common", "dep:oxc_transformer"]
transformer = ["dep:oxc_allocator", "dep:oxc_parser", "dep:oxc_span", "dep:oxc_tasks_common", "dep:oxc_transformer", "dep:oxc_semantic"]
semantic = ["dep:oxc_allocator", "dep:oxc_parser", "dep:oxc_semantic", "dep:oxc_span", "dep:oxc_tasks_common"]
minifier = ["dep:oxc_allocator", "dep:oxc_minifier", "dep:oxc_parser", "dep:oxc_span", "dep:oxc_tasks_common"]
codegen = ["dep:oxc_allocator", "dep:oxc_codegen", "dep:oxc_parser", "dep:oxc_span", "dep:oxc_tasks_common"]
Expand Down
7 changes: 6 additions & 1 deletion tasks/benchmark/benches/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::path::Path;
use oxc_allocator::Allocator;
use oxc_benchmark::{criterion_group, criterion_main, BenchmarkId, Criterion};
use oxc_parser::{Parser, ParserReturn};
use oxc_semantic::SemanticBuilder;
use oxc_span::SourceType;
use oxc_tasks_common::TestFiles;
use oxc_transformer::{TransformOptions, Transformer};
Expand All @@ -23,6 +24,10 @@ fn bench_transformer(criterion: &mut Criterion) {
Parser::new(&allocator, source_text, source_type).parse();
let transform_options = TransformOptions::default();
let program = allocator.alloc(program);
let (symbols, scopes) = SemanticBuilder::new(source_text, source_type)
.build(program)
.semantic
.into_symbol_table_and_scope_tree();
Transformer::new(
&allocator,
Path::new(&file.file_name),
Expand All @@ -31,7 +36,7 @@ fn bench_transformer(criterion: &mut Criterion) {
trivias,
transform_options,
)
.build(program);
.build_with_symbols_and_scopes(symbols, scopes, program);
allocator
});
});
Expand Down
Loading