Skip to content

Commit

Permalink
refactor(transformer): rename ReactJsx to Jsx (oxc-project#6883)
Browse files Browse the repository at this point in the history
This plugin is no longer about React.

The next PR will move out the React refresh plugin, which can be turned
on in isolation.
  • Loading branch information
Boshen committed Oct 25, 2024
1 parent 860cbca commit 1f29523
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 59 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,18 @@ use crate::TransformCtx;
use super::diagnostics;

pub use super::{
jsx_self::ReactJsxSelf,
jsx_source::ReactJsxSource,
jsx_self::JsxSelf,
jsx_source::JsxSource,
options::{JsxOptions, JsxRuntime},
};

pub struct ReactJsx<'a, 'ctx> {
pub struct JsxImpl<'a, 'ctx> {
options: JsxOptions,

ctx: &'ctx TransformCtx<'a>,

pub(super) jsx_self: ReactJsxSelf<'a, 'ctx>,
pub(super) jsx_source: ReactJsxSource<'a, 'ctx>,
pub(super) jsx_self: JsxSelf<'a, 'ctx>,
pub(super) jsx_source: JsxSource<'a, 'ctx>,

// States
bindings: Bindings<'a, 'ctx>,
Expand All @@ -128,6 +128,7 @@ enum Bindings<'a, 'ctx> {
AutomaticScript(AutomaticScriptBindings<'a, 'ctx>),
AutomaticModule(AutomaticModuleBindings<'a, 'ctx>),
}

impl<'a, 'ctx> Bindings<'a, 'ctx> {
#[inline]
fn is_classic(&self) -> bool {
Expand Down Expand Up @@ -367,7 +368,7 @@ impl<'a> Pragma<'a> {
}
}

impl<'a, 'ctx> ReactJsx<'a, 'ctx> {
impl<'a, 'ctx> JsxImpl<'a, 'ctx> {
pub fn new(options: JsxOptions, ast: AstBuilder<'a>, ctx: &'ctx TransformCtx<'a>) -> Self {
let bindings = match options.runtime {
JsxRuntime::Classic => {
Expand Down Expand Up @@ -434,14 +435,14 @@ impl<'a, 'ctx> ReactJsx<'a, 'ctx> {
Self {
options,
ctx,
jsx_self: ReactJsxSelf::new(ctx),
jsx_source: ReactJsxSource::new(ctx),
jsx_self: JsxSelf::new(ctx),
jsx_source: JsxSource::new(ctx),
bindings,
}
}
}

impl<'a, 'ctx> Traverse<'a> for ReactJsx<'a, 'ctx> {
impl<'a, 'ctx> Traverse<'a> for JsxImpl<'a, 'ctx> {
fn exit_program(&mut self, _program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
self.insert_filename_var_statement(ctx);
}
Expand All @@ -457,7 +458,7 @@ impl<'a, 'ctx> Traverse<'a> for ReactJsx<'a, 'ctx> {
}
}

impl<'a, 'ctx> ReactJsx<'a, 'ctx> {
impl<'a, 'ctx> JsxImpl<'a, 'ctx> {
fn is_script(&self) -> bool {
self.ctx.source_type.is_script()
}
Expand Down Expand Up @@ -613,7 +614,7 @@ impl<'a, 'ctx> ReactJsx<'a, 'ctx> {
if let Some(span) = self_attr_span {
self.jsx_self.report_error(span);
} else {
properties.push(ReactJsxSelf::get_object_property_kind_for_jsx_plugin(ctx));
properties.push(JsxSelf::get_object_property_kind_for_jsx_plugin(ctx));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ use crate::TransformCtx;

const SELF: &str = "__self";

pub struct ReactJsxSelf<'a, 'ctx> {
pub struct JsxSelf<'a, 'ctx> {
ctx: &'ctx TransformCtx<'a>,
}

impl<'a, 'ctx> ReactJsxSelf<'a, 'ctx> {
impl<'a, 'ctx> JsxSelf<'a, 'ctx> {
pub fn new(ctx: &'ctx TransformCtx<'a>) -> Self {
Self { ctx }
}
}

impl<'a, 'ctx> Traverse<'a> for ReactJsxSelf<'a, 'ctx> {
impl<'a, 'ctx> Traverse<'a> for JsxSelf<'a, 'ctx> {
fn enter_jsx_opening_element(
&mut self,
elem: &mut JSXOpeningElement<'a>,
Expand All @@ -57,7 +57,7 @@ impl<'a, 'ctx> Traverse<'a> for ReactJsxSelf<'a, 'ctx> {
}
}

impl<'a, 'ctx> ReactJsxSelf<'a, 'ctx> {
impl<'a, 'ctx> JsxSelf<'a, 'ctx> {
pub fn report_error(&self, span: Span) {
let error = OxcDiagnostic::warn("Duplicate __self prop found.").with_label(span);
self.ctx.error(error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,19 @@ use super::utils::get_line_column;
const SOURCE: &str = "__source";
const FILE_NAME_VAR: &str = "jsxFileName";

pub struct ReactJsxSource<'a, 'ctx> {
pub struct JsxSource<'a, 'ctx> {
filename_var: Option<BoundIdentifier<'a>>,
source_rope: Option<Rope>,
ctx: &'ctx TransformCtx<'a>,
}

impl<'a, 'ctx> ReactJsxSource<'a, 'ctx> {
impl<'a, 'ctx> JsxSource<'a, 'ctx> {
pub fn new(ctx: &'ctx TransformCtx<'a>) -> Self {
Self { filename_var: None, source_rope: None, ctx }
}
}

impl<'a, 'ctx> Traverse<'a> for ReactJsxSource<'a, 'ctx> {
impl<'a, 'ctx> Traverse<'a> for JsxSource<'a, 'ctx> {
fn exit_program(&mut self, _program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
if let Some(stmt) = self.get_filename_var_statement(ctx) {
self.ctx.top_level_statements.insert_statement(stmt);
Expand All @@ -76,7 +76,7 @@ impl<'a, 'ctx> Traverse<'a> for ReactJsxSource<'a, 'ctx> {
}
}

impl<'a, 'ctx> ReactJsxSource<'a, 'ctx> {
impl<'a, 'ctx> JsxSource<'a, 'ctx> {
pub fn get_line_column(&mut self, offset: u32) -> (usize, usize) {
let source_rope =
self.source_rope.get_or_insert_with(|| Rope::from_str(self.ctx.source_text));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::TransformCtx;
mod comments;
mod diagnostics;
mod display_name;
mod jsx;
mod jsx_impl;
mod jsx_self;
mod jsx_source;
mod options;
Expand All @@ -16,7 +16,7 @@ mod utils;
use refresh::ReactRefresh;

pub use display_name::ReactDisplayName;
pub use jsx::ReactJsx;
pub use jsx_impl::JsxImpl;
pub use options::{JsxOptions, JsxRuntime, ReactRefreshOptions};

pub(crate) use comments::update_options_with_comments;
Expand All @@ -29,19 +29,19 @@ pub(crate) use comments::update_options_with_comments;
/// * [plugin-transform-react-jsx-self](https://babeljs.io/docs/babel-plugin-transform-react-jsx-self)
/// * [plugin-transform-react-jsx-source](https://babel.dev/docs/babel-plugin-transform-react-jsx-source)
/// * [plugin-transform-react-display-name](https://babeljs.io/docs/babel-plugin-transform-react-display-name)
pub struct React<'a, 'ctx> {
jsx: ReactJsx<'a, 'ctx>,
pub struct Jsx<'a, 'ctx> {
implementation: JsxImpl<'a, 'ctx>,
display_name: ReactDisplayName<'a, 'ctx>,
refresh: ReactRefresh<'a, 'ctx>,
jsx_plugin: bool,
enable_jsx_plugin: bool,
display_name_plugin: bool,
jsx_self_plugin: bool,
jsx_source_plugin: bool,
self_plugin: bool,
source_plugin: bool,
refresh_plugin: bool,
}

// Constructors
impl<'a, 'ctx> React<'a, 'ctx> {
impl<'a, 'ctx> Jsx<'a, 'ctx> {
pub fn new(mut options: JsxOptions, ast: AstBuilder<'a>, ctx: &'ctx TransformCtx<'a>) -> Self {
if options.jsx_plugin || options.development {
options.conform();
Expand All @@ -51,21 +51,21 @@ impl<'a, 'ctx> React<'a, 'ctx> {
} = options;
let refresh = options.refresh.clone();
Self {
jsx: ReactJsx::new(options, ast, ctx),
implementation: JsxImpl::new(options, ast, ctx),
display_name: ReactDisplayName::new(ctx),
jsx_plugin,
enable_jsx_plugin: jsx_plugin,
display_name_plugin,
jsx_self_plugin,
jsx_source_plugin,
self_plugin: jsx_self_plugin,
source_plugin: jsx_source_plugin,
refresh_plugin: refresh.is_some(),
refresh: ReactRefresh::new(&refresh.unwrap_or_default(), ast, ctx),
}
}
}

impl<'a, 'ctx> Traverse<'a> for React<'a, 'ctx> {
impl<'a, 'ctx> Traverse<'a> for Jsx<'a, 'ctx> {
fn enter_program(&mut self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
if self.jsx_plugin {
if self.enable_jsx_plugin {
program.source_type = program.source_type.with_standard(true);
}
if self.refresh_plugin {
Expand All @@ -77,10 +77,10 @@ impl<'a, 'ctx> Traverse<'a> for React<'a, 'ctx> {
if self.refresh_plugin {
self.refresh.exit_program(program, ctx);
}
if self.jsx_plugin {
self.jsx.exit_program(program, ctx);
} else if self.jsx_source_plugin {
self.jsx.jsx_source.exit_program(program, ctx);
if self.enable_jsx_plugin {
self.implementation.exit_program(program, ctx);
} else if self.source_plugin {
self.implementation.jsx_source.exit_program(program, ctx);
}
}

Expand Down Expand Up @@ -115,19 +115,19 @@ impl<'a, 'ctx> Traverse<'a> for React<'a, 'ctx> {
elem: &mut JSXOpeningElement<'a>,
ctx: &mut TraverseCtx<'a>,
) {
if !self.jsx_plugin {
if self.jsx_self_plugin && self.jsx.jsx_self.can_add_self_attribute(ctx) {
self.jsx.jsx_self.enter_jsx_opening_element(elem, ctx);
if !self.enable_jsx_plugin {
if self.self_plugin && self.implementation.jsx_self.can_add_self_attribute(ctx) {
self.implementation.jsx_self.enter_jsx_opening_element(elem, ctx);
}
if self.jsx_source_plugin {
self.jsx.jsx_source.enter_jsx_opening_element(elem, ctx);
if self.source_plugin {
self.implementation.jsx_source.enter_jsx_opening_element(elem, ctx);
}
}
}

fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
if self.jsx_plugin {
self.jsx.exit_expression(expr, ctx);
if self.enable_jsx_plugin {
self.implementation.exit_expression(expr, ctx);
}
if self.refresh_plugin {
self.refresh.exit_expression(expr, ctx);
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
28 changes: 14 additions & 14 deletions crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ mod es2019;
mod es2020;
mod es2021;
mod es2022;
mod react;
mod jsx;
mod regexp;
mod typescript;

Expand All @@ -45,7 +45,7 @@ use es2019::ES2019;
use es2020::ES2020;
use es2021::ES2021;
use es2022::ES2022;
use react::React;
use jsx::Jsx;
use regexp::RegExp;
use typescript::TypeScript;

Expand All @@ -54,9 +54,9 @@ pub use crate::{
compiler_assumptions::CompilerAssumptions,
env::{EnvOptions, Targets},
es2015::{ArrowFunctionsOptions, ES2015Options},
jsx::{JsxOptions, JsxRuntime, ReactRefreshOptions},
options::{BabelOptions, TransformOptions},
plugins::*,
react::{JsxOptions, JsxRuntime, ReactRefreshOptions},
typescript::{RewriteExtensionsMode, TypeScriptOptions},
};

Expand Down Expand Up @@ -89,11 +89,11 @@ impl<'a> Transformer<'a> {

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

let mut transformer = TransformerImpl {
x0_typescript: TypeScript::new(&self.options.typescript, &self.ctx),
x1_react: React::new(self.options.react, ast_builder, &self.ctx),
x1_jsx: Jsx::new(self.options.react, ast_builder, &self.ctx),
x2_es2022: ES2022::new(self.options.es2022),
x2_es2021: ES2021::new(self.options.es2021, &self.ctx),
x2_es2020: ES2020::new(self.options.es2020, &self.ctx),
Expand All @@ -114,7 +114,7 @@ impl<'a> Transformer<'a> {
struct TransformerImpl<'a, 'ctx> {
// NOTE: all callbacks must run in order.
x0_typescript: TypeScript<'a, 'ctx>,
x1_react: React<'a, 'ctx>,
x1_jsx: Jsx<'a, 'ctx>,
x2_es2022: ES2022,
x2_es2021: ES2021<'a, 'ctx>,
x2_es2020: ES2020<'a, 'ctx>,
Expand All @@ -130,11 +130,11 @@ struct TransformerImpl<'a, 'ctx> {
impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> {
fn enter_program(&mut self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
self.x0_typescript.enter_program(program, ctx);
self.x1_react.enter_program(program, ctx);
self.x1_jsx.enter_program(program, ctx);
}

fn exit_program(&mut self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
self.x1_react.exit_program(program, ctx);
self.x1_jsx.exit_program(program, ctx);
self.x0_typescript.exit_program(program, ctx);
self.x3_es2015.exit_program(program, ctx);
self.common.exit_program(program, ctx);
Expand Down Expand Up @@ -164,7 +164,7 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> {

fn enter_call_expression(&mut self, expr: &mut CallExpression<'a>, ctx: &mut TraverseCtx<'a>) {
self.x0_typescript.enter_call_expression(expr, ctx);
self.x1_react.enter_call_expression(expr, ctx);
self.x1_jsx.enter_call_expression(expr, ctx);
}

fn enter_class(&mut self, class: &mut Class<'a>, ctx: &mut TraverseCtx<'a>) {
Expand Down Expand Up @@ -204,7 +204,7 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> {
}

fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
self.x1_react.exit_expression(expr, ctx);
self.x1_jsx.exit_expression(expr, ctx);
self.x2_es2017.exit_expression(expr, ctx);
self.x3_es2015.exit_expression(expr, ctx);
}
Expand Down Expand Up @@ -239,7 +239,7 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> {

fn exit_function(&mut self, func: &mut Function<'a>, ctx: &mut TraverseCtx<'a>) {
self.x0_typescript.exit_function(func, ctx);
self.x1_react.exit_function(func, ctx);
self.x1_jsx.exit_function(func, ctx);
self.x3_es2015.exit_function(func, ctx);
}

Expand Down Expand Up @@ -269,7 +269,7 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> {
ctx: &mut TraverseCtx<'a>,
) {
self.x0_typescript.enter_jsx_opening_element(elem, ctx);
self.x1_react.enter_jsx_opening_element(elem, ctx);
self.x1_jsx.enter_jsx_opening_element(elem, ctx);
}

fn enter_method_definition(
Expand Down Expand Up @@ -312,7 +312,7 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> {
fn enter_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) {
self.common.enter_statements(stmts, ctx);
self.x0_typescript.enter_statements(stmts, ctx);
self.x1_react.enter_statements(stmts, ctx);
self.x1_jsx.enter_statements(stmts, ctx);
}

fn exit_arrow_function_expression(
Expand Down Expand Up @@ -341,7 +341,7 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> {

fn exit_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) {
self.x0_typescript.exit_statements(stmts, ctx);
self.x1_react.exit_statements(stmts, ctx);
self.x1_jsx.exit_statements(stmts, ctx);
self.common.exit_statements(stmts, ctx);
}

Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_transformer/src/options/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use crate::{
es2020::ES2020Options,
es2021::ES2021Options,
es2022::ES2022Options,
jsx::JsxOptions,
options::babel::BabelOptions,
react::JsxOptions,
regexp::RegExpOptions,
typescript::TypeScriptOptions,
ReactRefreshOptions,
Expand Down

0 comments on commit 1f29523

Please sign in to comment.