fix(transformer): turn on react preset by default (#2968)

This commit is contained in:
Boshen 2024-04-14 19:04:59 +08:00 committed by GitHub
parent d57526103c
commit 10814d5331
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 35 additions and 35 deletions

View file

@ -2,6 +2,24 @@ use std::borrow::Cow;
use serde::Deserialize;
#[inline]
fn default_as_true() -> bool {
true
}
#[inline]
fn default_for_import_source() -> Cow<'static, str> {
Cow::Borrowed("react")
}
fn default_for_pragma() -> Cow<'static, str> {
Cow::Borrowed("React.createElement")
}
fn default_for_pragma_frag() -> Cow<'static, str> {
Cow::Borrowed("React.Fragment")
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ReactOptions {
@ -68,10 +86,10 @@ pub struct ReactOptions {
impl Default for ReactOptions {
fn default() -> Self {
Self {
jsx_plugin: false,
display_name_plugin: false,
jsx_self_plugin: false,
jsx_source_plugin: false,
jsx_plugin: true,
display_name_plugin: true,
jsx_self_plugin: true,
jsx_source_plugin: true,
runtime: ReactJsxRuntime::default(),
development: default_as_true(),
pure: default_as_true(),
@ -82,22 +100,14 @@ impl Default for ReactOptions {
}
}
#[inline]
fn default_as_true() -> bool {
true
}
impl ReactOptions {
pub fn is_jsx_self_plugin_enabled(&self) -> bool {
self.jsx_self_plugin && self.development
}
#[inline]
fn default_for_import_source() -> Cow<'static, str> {
Cow::Borrowed("react")
}
fn default_for_pragma() -> Cow<'static, str> {
Cow::Borrowed("React.createElement")
}
fn default_for_pragma_frag() -> Cow<'static, str> {
Cow::Borrowed("React.Fragment")
pub fn is_jsx_source_plugin_enabled(&self) -> bool {
self.jsx_source_plugin && self.development
}
}
/// Decides which runtime to use.

View file

@ -89,14 +89,11 @@ pub trait TestCase {
let react = options.get_preset("react").map_or_else(
|| {
let mut react_options = options
.get_plugin("transform-react-jsx")
.map(|options| {
let mut options = get_options::<ReactOptions>(options);
options.jsx_plugin = true;
options
})
.unwrap_or_default();
let jsx_plugin = options.get_plugin("transform-react-jsx");
let has_jsx_plugin = jsx_plugin.as_ref().is_some();
let mut react_options =
jsx_plugin.map(get_options::<ReactOptions>).unwrap_or_default();
react_options.jsx_plugin = has_jsx_plugin;
react_options.display_name_plugin =
options.get_plugin("transform-react-display-name").is_some();
react_options.jsx_self_plugin =
@ -105,14 +102,7 @@ pub trait TestCase {
options.get_plugin("transform-react-jsx-source").is_some();
react_options
},
|options| {
let mut react_options = get_options::<ReactOptions>(options);
react_options.jsx_plugin = true;
react_options.jsx_self_plugin = true;
react_options.jsx_source_plugin = true;
react_options.display_name_plugin = true;
react_options
},
get_options::<ReactOptions>,
);
TransformOptions {