feat(transformer): add the most basic plugin toggles (#2950)

This commit is contained in:
Boshen 2024-04-12 20:25:34 +08:00 committed by GitHub
parent 60ccbb105c
commit e651e50bda
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 49 additions and 17 deletions

View file

@ -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) }
}
}

View file

@ -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>,
@ -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);
}
}
}
}

View file

@ -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(),

View file

@ -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

View file

@ -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
@ -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,
}
}