mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 20:32:10 +00:00
feat(transformer): implement react-jsx-self (#2946)
This commit is contained in:
parent
ba2121f17d
commit
f903a225a8
6 changed files with 61 additions and 4 deletions
|
|
@ -227,6 +227,12 @@ pub struct JSXIdentifier<'a> {
|
||||||
pub name: Atom<'a>,
|
pub name: Atom<'a>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a> JSXIdentifier<'a> {
|
||||||
|
pub fn new(span: Span, name: Atom<'a>) -> Self {
|
||||||
|
Self { span, name }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 1.4 JSX Children
|
// 1.4 JSX Children
|
||||||
|
|
||||||
/// JSX Child
|
/// JSX Child
|
||||||
|
|
|
||||||
|
|
@ -122,4 +122,9 @@ impl<'a> VisitMut<'a> for Transformer<'a> {
|
||||||
self.x1_react.transform_export_default_declaration(decl);
|
self.x1_react.transform_export_default_declaration(decl);
|
||||||
walk_mut::walk_export_default_declaration_mut(self, decl);
|
walk_mut::walk_export_default_declaration_mut(self, decl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn visit_jsx_opening_element(&mut self, elem: &mut JSXOpeningElement<'a>) {
|
||||||
|
self.x1_react.transform_jsx_opening_element(elem);
|
||||||
|
walk_mut::walk_jsx_opening_element_mut(self, elem);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,8 @@
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
use oxc_ast::ast::*;
|
||||||
|
use oxc_span::SPAN;
|
||||||
|
|
||||||
use crate::context::Ctx;
|
use crate::context::Ctx;
|
||||||
|
|
||||||
/// [plugin-transform-react-jsx-self](https://babeljs.io/docs/babel-plugin-transform-react-jsx-self)
|
/// [plugin-transform-react-jsx-self](https://babeljs.io/docs/babel-plugin-transform-react-jsx-self)
|
||||||
|
|
@ -10,13 +13,46 @@ use crate::context::Ctx;
|
||||||
///
|
///
|
||||||
/// In: `<sometag />`
|
/// In: `<sometag />`
|
||||||
/// Out: `<sometag __self={this} />`
|
/// Out: `<sometag __self={this} />`
|
||||||
|
///
|
||||||
|
/// TODO:
|
||||||
|
/// *. Omit adding `this` in some conditions
|
||||||
|
/// <https://github.com/babel/babel/blob/9cd048b5ad45eafd157c4f9968343e36170a66c1/packages/babel-plugin-transform-react-jsx-self/src/index.ts#L78>
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
pub struct ReactJsxSelf<'a> {
|
pub struct ReactJsxSelf<'a> {
|
||||||
|
development: bool,
|
||||||
|
|
||||||
ctx: Ctx<'a>,
|
ctx: Ctx<'a>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> ReactJsxSelf<'a> {
|
impl<'a> ReactJsxSelf<'a> {
|
||||||
pub fn new(ctx: &Ctx<'a>) -> Self {
|
pub fn new(development: bool, ctx: &Ctx<'a>) -> Self {
|
||||||
Self { ctx: Rc::clone(ctx) }
|
Self { development, ctx: Rc::clone(ctx) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn transform_jsx_opening_element(&self, elem: &mut JSXOpeningElement<'a>) {
|
||||||
|
if self.development {
|
||||||
|
self.add_self_this_attribute(elem);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> ReactJsxSelf<'a> {
|
||||||
|
/// `<div __self={this} />`
|
||||||
|
/// ^^^^^^^^^^^^^
|
||||||
|
fn add_self_this_attribute(&self, elem: &mut JSXOpeningElement<'a>) {
|
||||||
|
let name = {
|
||||||
|
let name = self.ctx.ast.new_atom("__self");
|
||||||
|
JSXAttributeName::Identifier(JSXIdentifier::new(SPAN, name))
|
||||||
|
};
|
||||||
|
let value = {
|
||||||
|
let jsx_expr = JSXExpression::Expression(self.ctx.ast.this_expression(SPAN));
|
||||||
|
let container = self.ctx.ast.jsx_expression_container(SPAN, jsx_expr);
|
||||||
|
JSXAttributeValue::ExpressionContainer(container)
|
||||||
|
};
|
||||||
|
let attribute = {
|
||||||
|
let attribute = self.ctx.ast.jsx_attribute(SPAN, name, Some(value));
|
||||||
|
JSXAttributeItem::Attribute(attribute)
|
||||||
|
};
|
||||||
|
elem.attributes.push(attribute);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -35,10 +35,11 @@ pub struct React<'a> {
|
||||||
// Constructors
|
// Constructors
|
||||||
impl<'a> React<'a> {
|
impl<'a> React<'a> {
|
||||||
pub fn new(options: ReactOptions, ctx: &Ctx<'a>) -> Self {
|
pub fn new(options: ReactOptions, ctx: &Ctx<'a>) -> Self {
|
||||||
|
let development = options.development;
|
||||||
Self {
|
Self {
|
||||||
ctx: Rc::clone(ctx),
|
ctx: Rc::clone(ctx),
|
||||||
jsx: ReactJsx::new(options, ctx),
|
jsx: ReactJsx::new(options, ctx),
|
||||||
jsx_self: ReactJsxSelf::new(ctx),
|
jsx_self: ReactJsxSelf::new(development, ctx),
|
||||||
jsx_source: ReactJsxSource::new(ctx),
|
jsx_source: ReactJsxSource::new(ctx),
|
||||||
display_name: ReactDisplayName::new(ctx),
|
display_name: ReactDisplayName::new(ctx),
|
||||||
}
|
}
|
||||||
|
|
@ -76,4 +77,8 @@ impl<'a> React<'a> {
|
||||||
) {
|
) {
|
||||||
self.display_name.transform_export_default_declaration(decl);
|
self.display_name.transform_export_default_declaration(decl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn transform_jsx_opening_element(&mut self, elem: &mut JSXOpeningElement<'a>) {
|
||||||
|
self.jsx_self.transform_jsx_opening_element(elem);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
Passed: 75/174
|
Passed: 76/177
|
||||||
|
|
||||||
# All Passed:
|
# All Passed:
|
||||||
|
|
||||||
|
|
@ -107,3 +107,7 @@ Passed: 75/174
|
||||||
# babel-plugin-transform-react-display-name (15/16)
|
# babel-plugin-transform-react-display-name (15/16)
|
||||||
* display-name/nested/input.js
|
* display-name/nested/input.js
|
||||||
|
|
||||||
|
# babel-plugin-transform-react-jsx-self (1/3)
|
||||||
|
* react-source/arrow-function/input.js
|
||||||
|
* react-source/disable-with-super/input.js
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -94,6 +94,7 @@ const CASES: &[&str] = &[
|
||||||
// React
|
// React
|
||||||
// "babel-plugin-transform-react-jsx",
|
// "babel-plugin-transform-react-jsx",
|
||||||
"babel-plugin-transform-react-display-name",
|
"babel-plugin-transform-react-display-name",
|
||||||
|
"babel-plugin-transform-react-jsx-self",
|
||||||
// // Proposal
|
// // Proposal
|
||||||
// "babel-plugin-proposal-decorators",
|
// "babel-plugin-proposal-decorators",
|
||||||
];
|
];
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue