Skip to content

Commit

Permalink
refactor(transformer): use AstBuilder instance from TraverseCtx (#6209)
Browse files Browse the repository at this point in the history
A small modification of #6173. Closes #6172.
  • Loading branch information
overlookmotel committed Oct 1, 2024
1 parent da2b2a4 commit 235cdba
Show file tree
Hide file tree
Showing 12 changed files with 274 additions and 279 deletions.
7 changes: 1 addition & 6 deletions crates/oxc_transformer/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use std::{
path::{Path, PathBuf},
};

use oxc_allocator::Allocator;
use oxc_ast::{AstBuilder, Trivias};
use oxc_ast::Trivias;
use oxc_diagnostics::OxcDiagnostic;
use oxc_span::SourceType;

Expand All @@ -22,8 +21,6 @@ pub struct TransformCtx<'a> {

pub trivias: Trivias,

pub ast: AstBuilder<'a>,

/// <https://babeljs.io/docs/options#filename>
pub filename: String,

Expand All @@ -45,7 +42,6 @@ pub struct TransformCtx<'a> {

impl<'a> TransformCtx<'a> {
pub fn new(
allocator: &'a Allocator,
source_path: &Path,
source_type: SourceType,
source_text: &'a str,
Expand All @@ -62,7 +58,6 @@ impl<'a> TransformCtx<'a> {

Self {
errors: RefCell::new(vec![]),
ast: AstBuilder::new(allocator),
filename,
source_path,
source_type,
Expand Down
13 changes: 8 additions & 5 deletions crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
//! * <https://babel.dev/docs/presets>
//! * <https://github.com/microsoft/TypeScript/blob/main/src/compiler/transformer.ts>

use oxc_ast::AstBuilder;

// Core
mod common;
mod compiler_assumptions;
Expand Down Expand Up @@ -68,6 +70,7 @@ pub struct TransformerReturn {
pub struct Transformer<'a> {
ctx: TransformCtx<'a>,
options: TransformOptions,
allocator: &'a Allocator,
}

impl<'a> Transformer<'a> {
Expand All @@ -79,9 +82,8 @@ impl<'a> Transformer<'a> {
trivias: Trivias,
options: TransformOptions,
) -> Self {
let ctx =
TransformCtx::new(allocator, source_path, source_type, source_text, trivias, &options);
Self { ctx, options }
let ctx = TransformCtx::new(source_path, source_type, source_text, trivias, &options);
Self { ctx, options, allocator }
}

pub fn build_with_symbols_and_scopes(
Expand All @@ -90,11 +92,12 @@ impl<'a> Transformer<'a> {
scopes: ScopeTree,
program: &mut Program<'a>,
) -> TransformerReturn {
let allocator = self.ctx.ast.allocator;
let allocator = self.allocator;
let ast_builder = AstBuilder::new(allocator);

let mut transformer = TransformerImpl {
x0_typescript: TypeScript::new(self.options.typescript, &self.ctx),
x1_react: React::new(self.options.react, &self.ctx),
x1_react: React::new(self.options.react, ast_builder, &self.ctx),
x2_es2021: ES2021::new(self.options.es2021, &self.ctx),
x2_es2020: ES2020::new(self.options.es2020, &self.ctx),
x2_es2019: ES2019::new(self.options.es2019),
Expand Down
14 changes: 9 additions & 5 deletions crates/oxc_transformer/src/react/display_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl<'a, 'ctx> Traverse<'a> for ReactDisplayName<'a, 'ctx> {
}
};

self.add_display_name(obj_expr, name);
Self::add_display_name(obj_expr, name, ctx);
}
}

Expand Down Expand Up @@ -155,7 +155,11 @@ impl<'a, 'ctx> ReactDisplayName<'a, 'ctx> {
}

/// Add key value `displayName: name` to the `React.createClass` object.
fn add_display_name(&self, obj_expr: &mut ObjectExpression<'a>, name: Atom<'a>) {
fn add_display_name(
obj_expr: &mut ObjectExpression<'a>,
name: Atom<'a>,
ctx: &TraverseCtx<'a>,
) {
const DISPLAY_NAME: &str = "displayName";
// Not safe with existing display name.
let not_safe = obj_expr.properties.iter().any(|prop| {
Expand All @@ -166,11 +170,11 @@ impl<'a, 'ctx> ReactDisplayName<'a, 'ctx> {
}
obj_expr.properties.insert(
0,
self.ctx.ast.object_property_kind_object_property(
ctx.ast.object_property_kind_object_property(
SPAN,
PropertyKind::Init,
self.ctx.ast.property_key_identifier_name(SPAN, DISPLAY_NAME),
self.ctx.ast.expression_string_literal(SPAN, name),
ctx.ast.property_key_identifier_name(SPAN, DISPLAY_NAME),
ctx.ast.expression_string_literal(SPAN, name),
None,
false,
false,
Expand Down
Loading

0 comments on commit 235cdba

Please sign in to comment.