From e651e50bda7e5c69666983d25c5a7183c2a999c7 Mon Sep 17 00:00:00 2001 From: Boshen Date: Fri, 12 Apr 2024 20:25:34 +0800 Subject: [PATCH] feat(transformer): add the most basic plugin toggles (#2950) --- crates/oxc_transformer/src/react/jsx/mod.rs | 6 ++-- crates/oxc_transformer/src/react/mod.rs | 29 +++++++++++++++----- crates/oxc_transformer/src/react/options.rs | 12 ++++++++ tasks/transform_conformance/babel.snap.md | 5 ++-- tasks/transform_conformance/src/test_case.rs | 14 +++++++--- 5 files changed, 49 insertions(+), 17 deletions(-) diff --git a/crates/oxc_transformer/src/react/jsx/mod.rs b/crates/oxc_transformer/src/react/jsx/mod.rs index f1007b0af..580dd94d3 100644 --- a/crates/oxc_transformer/src/react/jsx/mod.rs +++ b/crates/oxc_transformer/src/react/jsx/mod.rs @@ -19,12 +19,12 @@ pub use super::options::ReactOptions; /// * #[allow(unused)] pub struct ReactJsx<'a> { - options: ReactOptions, + options: Rc, 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, ctx: &Ctx<'a>) -> Self { + Self { options: Rc::clone(options), ctx: Rc::clone(ctx) } } } diff --git a/crates/oxc_transformer/src/react/mod.rs b/crates/oxc_transformer/src/react/mod.rs index 4c2d100e0..cfdc90087 100644 --- a/crates/oxc_transformer/src/react/mod.rs +++ b/crates/oxc_transformer/src/react/mod.rs @@ -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, ctx: Ctx<'a>, jsx: ReactJsx<'a>, jsx_self: ReactJsxSelf<'a>, @@ -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), @@ -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!(); @@ -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); + } } } } diff --git a/crates/oxc_transformer/src/react/options.rs b/crates/oxc_transformer/src/react/options.rs index f765599a4..d2c123ed6 100644 --- a/crates/oxc_transformer/src/react/options.rs +++ b/crates/oxc_transformer/src/react/options.rs @@ -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. @@ -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(), diff --git a/tasks/transform_conformance/babel.snap.md b/tasks/transform_conformance/babel.snap.md index b363a5004..51a2f7a8d 100644 --- a/tasks/transform_conformance/babel.snap.md +++ b/tasks/transform_conformance/babel.snap.md @@ -1,4 +1,4 @@ -Passed: 76/178 +Passed: 77/178 # All Passed: * babel-plugin-transform-react-jsx-source @@ -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 diff --git a/tasks/transform_conformance/src/test_case.rs b/tasks/transform_conformance/src/test_case.rs index 42bc963e1..fc40b38a6 100644 --- a/tasks/transform_conformance/src/test_case.rs +++ b/tasks/transform_conformance/src/test_case.rs @@ -86,6 +86,15 @@ pub trait TestCase { value.and_then(|v| serde_json::from_value::(v).ok()).unwrap_or_default() } let options = self.options(); + + let mut react = options + .get_plugin("transform-react-jsx") + .map(get_options::) + .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 @@ -96,10 +105,7 @@ pub trait TestCase { .get_plugin("transform-typescript") .map(get_options::) .unwrap_or_default(), - react: options - .get_plugin("transform-react-jsx") - .map(get_options::) - .unwrap_or_default(), + react, } }