feat(transformer/react): enable jsx plugin when development is true (#3141)

This commit is contained in:
Dunqing 2024-04-30 15:48:17 +08:00 committed by GitHub
parent a63a45d5b2
commit be8fabedab
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 27 additions and 19 deletions

View file

@ -199,8 +199,12 @@ pub struct JSXAttribute<'a> {
}
impl<'a> JSXAttribute<'a> {
pub fn is_identifier(&self, name: &str) -> bool {
matches!(&self.name, JSXAttributeName::Identifier(ident) if ident.name == name)
}
pub fn is_key(&self) -> bool {
matches!(&self.name, JSXAttributeName::Identifier(ident) if ident.name == "key")
self.is_identifier("key")
}
}

View file

@ -296,26 +296,26 @@ impl<'a> ReactJsx<'a> {
continue;
}
}
JSXAttributeItem::Attribute(attr) if attr.is_key() => {
if matches!(&attr.name, JSXAttributeName::Identifier(ident) if ident.name == "__self")
{
JSXAttributeItem::Attribute(attr) => {
if attr.is_identifier("__self") {
self_attr_span = Some(attr.name.span());
} else if matches!(&attr.name, JSXAttributeName::Identifier(ident) if ident.name == "__source")
{
} else if attr.is_identifier("__source") {
source_attr_span = Some(attr.name.span());
}
if attr.value.is_none() {
self.ctx.error(ValuelessKey(attr.name.span()));
}
// In automatic mode, extract the key before spread prop,
// and add it to the third argument later.
if is_automatic && !has_key_after_props_spread {
key_prop = attr.value.as_ref();
continue;
if attr.is_key() {
if attr.value.is_none() {
self.ctx.error(ValuelessKey(attr.name.span()));
}
// In automatic mode, extract the key before spread prop,
// and add it to the third argument later.
if is_automatic && !has_key_after_props_spread {
key_prop = attr.value.as_ref();
continue;
}
}
}
_ => {}
JSXAttributeItem::SpreadAttribute(_) => {}
}
// Add attribute to prop object

View file

@ -30,7 +30,7 @@ pub struct React<'a> {
impl<'a> React<'a> {
pub fn new(options: ReactOptions, ctx: &Ctx<'a>) -> Self {
let mut options = options;
if options.jsx_plugin {
if options.is_jsx_plugin_enabled() {
options.update_with_comments(ctx);
}
let options = Rc::new(options);
@ -47,7 +47,7 @@ impl<'a> React<'a> {
pub fn transform_program_on_exit(&mut self, program: &mut Program<'a>) {
// TODO: PERF: These two transforms reallocathe program.statements,
// they should be combined so that allocation is computed only once for program.statements.
if self.options.jsx_plugin {
if self.options.is_jsx_plugin_enabled() {
self.jsx.transform_program_on_exit(program);
}
if self.options.is_jsx_source_plugin_enabled() {
@ -63,12 +63,12 @@ impl<'a> React<'a> {
}
}
Expression::JSXElement(e) => {
if self.options.jsx_plugin {
if self.options.is_jsx_plugin_enabled() {
*expr = self.jsx.transform_jsx_element(e);
}
}
Expression::JSXFragment(e) => {
if self.options.jsx_plugin {
if self.options.is_jsx_plugin_enabled() {
*expr = self.jsx.transform_jsx_fragment(e);
}
}

View file

@ -142,6 +142,10 @@ impl Default for ReactOptions {
}
impl ReactOptions {
pub fn is_jsx_plugin_enabled(&self) -> bool {
self.jsx_plugin || self.development
}
pub fn is_jsx_self_plugin_enabled(&self) -> bool {
self.jsx_self_plugin || self.development
}