Skip to content

Commit

Permalink
feat(transformer): add the most basic plugin toggles
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Apr 12, 2024
1 parent f7ddf02 commit ede866f
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 17 deletions.
1 change: 1 addition & 0 deletions crates/oxc_transformer/src/decorators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ impl<'a> Decorators<'a> {

// Transformers
impl<'a> Decorators<'a> {
#[allow(clippy::unused_self)]
pub fn transform_statement(&self, _stmt: &mut Statement<'_>) {}
}
6 changes: 3 additions & 3 deletions crates/oxc_transformer/src/react/jsx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ pub use super::options::ReactOptions;
/// * <https://github.com/babel/babel/tree/main/packages/babel-helper-builder-react-jsx>
#[allow(unused)]
pub struct ReactJsx<'a> {
options: ReactOptions,
options: Rc<ReactOptions>,
ctx: Ctx<'a>,
}

impl<'a> ReactJsx<'a> {
pub fn new(options: ReactOptions, ctx: &Ctx<'a>) -> Self {
Self { options, ctx: Rc::clone(ctx) }
pub fn new(options: &Rc<ReactOptions>, ctx: &Ctx<'a>) -> Self {
Self { options: Rc::clone(options), ctx: Rc::clone(ctx) }
}
}
29 changes: 22 additions & 7 deletions crates/oxc_transformer/src/react/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub use self::{
/// * [plugin-transform-react-display-name](https://babeljs.io/docs/babel-plugin-transform-react-display-name)
#[allow(unused)]
pub struct React<'a> {
options: Rc<ReactOptions>,
ctx: Ctx<'a>,
jsx: ReactJsx<'a>,
jsx_self: ReactJsxSelf<'a>,
Expand All @@ -37,9 +38,11 @@ pub struct React<'a> {
impl<'a> React<'a> {
pub fn new(options: ReactOptions, ctx: &Ctx<'a>) -> Self {
let development = options.development;
let options = Rc::new(options);
Self {
options: Rc::clone(&options),
ctx: Rc::clone(ctx),
jsx: ReactJsx::new(options, ctx),
jsx: ReactJsx::new(&options, ctx),
jsx_self: ReactJsxSelf::new(ctx),
jsx_source: ReactJsxSource::new(ctx),
display_name: ReactDisplayName::new(ctx),
Expand All @@ -53,7 +56,9 @@ impl<'a> React<'a> {
pub fn transform_expression(&self, expr: &mut Expression<'a>) {
match expr {
Expression::AssignmentExpression(e) => {
self.display_name.transform_assignment_expression(e);
if self.options.display_name_plugin {
self.display_name.transform_assignment_expression(e);
}
}
Expression::JSXElement(_e) => {
// *expr = unimplemented!();
Expand All @@ -66,21 +71,31 @@ impl<'a> React<'a> {
}

pub fn transform_variable_declarator(&self, declarator: &mut VariableDeclarator<'a>) {
self.display_name.transform_variable_declarator(declarator);
if self.options.display_name_plugin {
self.display_name.transform_variable_declarator(declarator);
}
}

pub fn transform_object_property(&self, prop: &mut ObjectProperty<'a>) {
self.display_name.transform_object_property(prop);
if self.options.display_name_plugin {
self.display_name.transform_object_property(prop);
}
}

pub fn transform_export_default_declaration(&self, decl: &mut ExportDefaultDeclaration<'a>) {
self.display_name.transform_export_default_declaration(decl);
if self.options.display_name_plugin {
self.display_name.transform_export_default_declaration(decl);
}
}

pub fn transform_jsx_opening_element(&self, elem: &mut JSXOpeningElement<'a>) {
if self.development {
self.jsx_self.transform_jsx_opening_element(elem);
self.jsx_source.transform_jsx_opening_element(elem);
if self.options.jsx_self_plugin {
self.jsx_self.transform_jsx_opening_element(elem);
}
if self.options.jsx_source_plugin {
self.jsx_source.transform_jsx_opening_element(elem);
}
}
}
}
12 changes: 12 additions & 0 deletions crates/oxc_transformer/src/react/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ use serde::Deserialize;
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ReactOptions {
#[serde(skip)]
pub display_name_plugin: bool,

#[serde(skip)]
pub jsx_self_plugin: bool,

#[serde(skip)]
pub jsx_source_plugin: bool,

// Both Runtimes
//
/// Decides which runtime to use.
Expand Down Expand Up @@ -56,6 +65,9 @@ pub struct ReactOptions {
impl Default for ReactOptions {
fn default() -> Self {
Self {
display_name_plugin: false,
jsx_self_plugin: false,
jsx_source_plugin: false,
runtime: ReactJsxRuntime::default(),
development: default_as_true(),
pure: default_as_true(),
Expand Down
5 changes: 2 additions & 3 deletions tasks/transform_conformance/babel.snap.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Passed: 76/178
Passed: 77/178

# All Passed:
* babel-plugin-transform-react-jsx-source
Expand Down Expand Up @@ -107,8 +107,7 @@ Passed: 76/178
# babel-plugin-transform-react-display-name (15/16)
* display-name/nested/input.js

# babel-plugin-transform-react-jsx-self (0/3)
# babel-plugin-transform-react-jsx-self (1/3)
* react-source/arrow-function/input.js
* react-source/basic-sample/input.js
* react-source/disable-with-super/input.js

14 changes: 10 additions & 4 deletions tasks/transform_conformance/src/test_case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ pub trait TestCase {
value.and_then(|v| serde_json::from_value::<T>(v).ok()).unwrap_or_default()
}
let options = self.options();

let mut react = options
.get_plugin("transform-react-jsx")
.map(get_options::<ReactOptions>)
.unwrap_or_default();
react.display_name_plugin = options.get_plugin("transform-react-display-name").is_some();
react.jsx_self_plugin = options.get_plugin("transform-react-jsx-self").is_some();
react.jsx_source_plugin = options.get_plugin("transform-react-jsx-source").is_some();

TransformOptions {
assumptions: serde_json::from_value(options.assumptions.clone()).unwrap_or_default(),
decorators: options
Expand All @@ -96,10 +105,7 @@ pub trait TestCase {
.get_plugin("transform-typescript")
.map(get_options::<TypeScriptOptions>)
.unwrap_or_default(),
react: options
.get_plugin("transform-react-jsx")
.map(get_options::<ReactOptions>)
.unwrap_or_default(),
react,
}
}

Expand Down

0 comments on commit ede866f

Please sign in to comment.