Skip to content

Commit

Permalink
refactor(transformer)!: remove source_type param from `Transformer:…
Browse files Browse the repository at this point in the history
…:new` (#6251)

Closes #6248.
  • Loading branch information
overlookmotel committed Oct 3, 2024
1 parent 115ccc9 commit 4f6bc79
Show file tree
Hide file tree
Showing 10 changed files with 401 additions and 768 deletions.
4 changes: 1 addition & 3 deletions crates/oxc/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ pub trait CompilerInterface {
&mut program,
source_path,
source_text,
source_type,
&trivias,
symbols,
scopes,
Expand Down Expand Up @@ -208,12 +207,11 @@ pub trait CompilerInterface {
program: &mut Program<'a>,
source_path: &Path,
source_text: &'a str,
source_type: SourceType,
trivias: &Trivias,
symbols: SymbolTable,
scopes: ScopeTree,
) -> TransformerReturn {
Transformer::new(allocator, source_path, source_type, source_text, trivias.clone(), options)
Transformer::new(allocator, source_path, source_text, trivias.clone(), options)
.build_with_symbols_and_scopes(symbols, scopes, program)
}

Expand Down
11 changes: 2 additions & 9 deletions crates/oxc_transformer/examples/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,8 @@ fn main() {
TransformOptions::enable_all()
};

let ret = Transformer::new(
&allocator,
path,
source_type,
&source_text,
trivias.clone(),
transform_options,
)
.build_with_symbols_and_scopes(symbols, scopes, &mut program);
let ret = Transformer::new(&allocator, path, &source_text, trivias.clone(), transform_options)
.build_with_symbols_and_scopes(symbols, scopes, &mut program);

if !ret.errors.is_empty() {
println!("Transformer Errors:");
Expand Down
3 changes: 1 addition & 2 deletions crates/oxc_transformer/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ pub struct TransformCtx<'a> {
impl<'a> TransformCtx<'a> {
pub fn new(
source_path: &Path,
source_type: SourceType,
source_text: &'a str,
trivias: Trivias,
options: &TransformOptions,
Expand All @@ -60,7 +59,7 @@ impl<'a> TransformCtx<'a> {
errors: RefCell::new(vec![]),
filename,
source_path,
source_type,
source_type: SourceType::default(),
source_text,
trivias,
module_imports: ModuleImportsStore::new(),
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use oxc_allocator::{Allocator, Vec};
use oxc_ast::{ast::*, Trivias};
use oxc_diagnostics::OxcDiagnostic;
use oxc_semantic::{ScopeTree, SymbolTable};
use oxc_span::{SourceType, SPAN};
use oxc_span::SPAN;
use oxc_traverse::{traverse_mut, Traverse, TraverseCtx};
use regexp::RegExp;

Expand Down Expand Up @@ -76,12 +76,11 @@ impl<'a> Transformer<'a> {
pub fn new(
allocator: &'a Allocator,
source_path: &Path,
source_type: SourceType,
source_text: &'a str,
trivias: Trivias,
options: TransformOptions,
) -> Self {
let ctx = TransformCtx::new(source_path, source_type, source_text, trivias, &options);
let ctx = TransformCtx::new(source_path, source_text, trivias, &options);
Self { ctx, options, allocator }
}

Expand All @@ -94,6 +93,7 @@ impl<'a> Transformer<'a> {
let allocator = self.allocator;
let ast_builder = AstBuilder::new(allocator);

self.ctx.source_type = program.source_type;
react::update_options_with_comments(&mut self.options, &self.ctx);

let mut transformer = TransformerImpl {
Expand Down
12 changes: 3 additions & 9 deletions crates/oxc_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,9 @@ impl Oxc {
targets: Targets::from_query("chrome 51"),
..EnvOptions::default()
}) {
let result = Transformer::new(
&allocator,
&path,
source_type,
source_text,
trivias.clone(),
options,
)
.build_with_symbols_and_scopes(symbols, scopes, &mut program);
let result =
Transformer::new(&allocator, &path, source_text, trivias.clone(), options)
.build_with_symbols_and_scopes(symbols, scopes, &mut program);
if !result.errors.is_empty() {
self.save_diagnostics(
result.errors.into_iter().map(Error::from).collect::<Vec<_>>(),
Expand Down
5 changes: 0 additions & 5 deletions napi/transform/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,6 @@ impl<'a> TransformContext<'a> {
self.declarations.as_ref()
}

#[inline]
pub fn source_type(&self) -> SourceType {
self.source_type
}

#[inline]
pub fn program(&self) -> Ref<'_, Program<'a>> {
self.program.borrow()
Expand Down
1 change: 0 additions & 1 deletion napi/transform/src/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ fn transpile(ctx: &TransformContext<'_>, options: Option<TransformOptions>) -> C
let ret = Transformer::new(
ctx.allocator,
ctx.file_path(),
ctx.source_type(),
ctx.source_text(),
ctx.trivias.clone(),
options,
Expand Down
1 change: 0 additions & 1 deletion tasks/benchmark/benches/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ fn bench_transformer(criterion: &mut Criterion) {
let ret = Transformer::new(
&allocator,
Path::new(&file.file_name),
source_type,
source_text,
trivias,
options,
Expand Down
5 changes: 2 additions & 3 deletions tasks/coverage/snapshots/semantic_babel.snap
Original file line number Diff line number Diff line change
Expand Up @@ -762,9 +762,8 @@ semantic error: `export = <value>;` is only supported when compiling modules to
Please consider using `export default <value>;`, or add @babel/plugin-transform-modules-commonjs to your Babel config.

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/export/equals-in-unambiguous/input.ts
semantic error: Unresolved references mismatch:
after transform: ["f"]
rebuilt : []
semantic error: `export = <value>;` is only supported when compiling modules to CommonJS.
Please consider using `export default <value>;`, or add @babel/plugin-transform-modules-commonjs to your Babel config.

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/export/export-type/input.ts
semantic error: Bindings mismatch:
Expand Down
Loading

0 comments on commit 4f6bc79

Please sign in to comment.