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

refactor(transformer): use AstBuilder instance from TraverseCtx #6209

Merged
merged 1 commit into from
Oct 1, 2024
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
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),
Comment on lines -93 to +100
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shulaoda This is how I solved the problem. This is pretty hacky, but it works.

We are intending to revamp the interface to Transformer soon, so hopefully we can come up with a more ergonomic solution then.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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
Loading