feat(transformer): add react preset (#2921)

This commit is contained in:
Boshen 2024-04-09 12:39:33 +08:00 committed by GitHub
parent 5abbb0cada
commit d65eab3b8b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 95 additions and 77 deletions

View file

@ -9,10 +9,7 @@
mod compiler_assumptions; mod compiler_assumptions;
// Plugins: <https://babeljs.io/docs/plugins-list> // Plugins: <https://babeljs.io/docs/plugins-list>
mod decorators; mod decorators;
mod react_display_name; mod react;
mod react_jsx;
mod react_jsx_self;
mod react_jsx_source;
mod typescript; mod typescript;
use oxc_allocator::Allocator; use oxc_allocator::Allocator;
@ -24,10 +21,10 @@ use oxc_span::SourceType;
pub use crate::{ pub use crate::{
compiler_assumptions::CompilerAssumptions, compiler_assumptions::CompilerAssumptions,
decorators::{Decorators, DecoratorsOptions}, decorators::{Decorators, DecoratorsOptions},
react_display_name::{ReactDisplayName, ReactDisplayNameOptions}, react::{
react_jsx::{ReactJsx, ReactJsxOptions}, React, ReactDisplayName, ReactDisplayNameOptions, ReactJsx, ReactJsxSelf, ReactJsxSource,
react_jsx_self::{ReactJsxSelf, ReactJsxSelfOptions}, ReactJsxSourceOptions, ReactOptions,
react_jsx_source::{ReactJsxSource, ReactJsxSourceOptions}, },
typescript::{TypeScript, TypeScriptOptions}, typescript::{TypeScript, TypeScriptOptions},
}; };
@ -40,12 +37,14 @@ pub struct TransformOptions {
pub assumptions: CompilerAssumptions, pub assumptions: CompilerAssumptions,
// Plugins // Plugins
/// [proposal-decorators](https://babeljs.io/docs/babel-plugin-proposal-decorators)
pub decorators: DecoratorsOptions, pub decorators: DecoratorsOptions,
/// [preset-typescript](https://babeljs.io/docs/babel-preset-typescript)
pub typescript: TypeScriptOptions, pub typescript: TypeScriptOptions,
pub react_jsx: ReactJsxOptions,
pub react_display_name: ReactDisplayNameOptions, /// [preset-react](https://babeljs.io/docs/babel-preset-react)
pub react_jsx_self: ReactJsxSelfOptions, pub react: ReactOptions,
pub react_jsx_source: ReactJsxSourceOptions,
} }
#[allow(unused)] #[allow(unused)]
@ -55,15 +54,9 @@ pub struct Transformer<'a> {
semantic: Semantic<'a>, semantic: Semantic<'a>,
options: TransformOptions, options: TransformOptions,
// Stage 3
decorators: Decorators, decorators: Decorators,
// [preset-typescript](https://babeljs.io/docs/babel-preset-typescript)
typescript: TypeScript, typescript: TypeScript,
// [preset-react](https://babeljs.io/docs/babel-preset-react) react: React,
react_display_name: ReactDisplayName,
react_jsx: ReactJsx,
react_jsx_self: ReactJsxSelf,
react_jsx_source: ReactJsxSource,
} }
impl<'a> Transformer<'a> { impl<'a> Transformer<'a> {
@ -80,10 +73,7 @@ impl<'a> Transformer<'a> {
options, options,
decorators: Decorators::default(), decorators: Decorators::default(),
typescript: TypeScript::default(), typescript: TypeScript::default(),
react_display_name: ReactDisplayName::default(), react: React::default(),
react_jsx: ReactJsx::default(),
react_jsx_self: ReactJsxSelf::default(),
react_jsx_source: ReactJsxSource::default(),
} }
} }

View file

@ -1,6 +1,4 @@
mod options; pub use super::options::ReactOptions;
pub use self::options::ReactJsxOptions;
/// [plugin-transform-react-jsx](https://babeljs.io/docs/babel-plugin-transform-react-jsx) /// [plugin-transform-react-jsx](https://babeljs.io/docs/babel-plugin-transform-react-jsx)
/// ///
@ -18,11 +16,11 @@ pub use self::options::ReactJsxOptions;
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct ReactJsx { pub struct ReactJsx {
#[allow(unused)] #[allow(unused)]
options: ReactJsxOptions, options: ReactOptions,
} }
impl ReactJsx { impl ReactJsx {
pub fn new(options: ReactJsxOptions) -> Self { pub fn new(options: ReactOptions) -> Self {
Self { options } Self { options }
} }
} }

View file

@ -0,0 +1,18 @@
use serde::Deserialize;
#[derive(Debug, Default, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ReactJsxSourceOptions;
/// [plugin-transform-react-jsx-source](https://babeljs.io/docs/babel-plugin-transform-react-jsx-source)
#[derive(Debug, Default)]
pub struct ReactJsxSource {
#[allow(unused)]
options: ReactJsxSourceOptions,
}
impl ReactJsxSource {
pub fn new(options: ReactJsxSourceOptions) -> Self {
Self { options }
}
}

View file

@ -0,0 +1,51 @@
mod display_name;
mod jsx;
mod jsx_self;
mod jsx_source;
mod options;
pub use self::{
display_name::{ReactDisplayName, ReactDisplayNameOptions},
jsx::ReactJsx,
jsx_self::{ReactJsxSelf, ReactJsxSelfOptions},
jsx_source::{ReactJsxSource, ReactJsxSourceOptions},
options::ReactOptions,
};
/// [Preset React](https://babel.dev/docs/babel-preset-react)
///
/// This preset includes the following plugins:
///
/// * [plugin-transform-react-jsx](https://babeljs.io/docs/babel-plugin-transform-react-jsx)
/// * [plugin-transform-react-jsx-self](https://babeljs.io/docs/babel-plugin-transform-react-jsx-self)
/// * [plugin-transform-react-jsx](https://babeljs.io/docs/babel-plugin-transform-react-jsx)
/// * [plugin-transform-react-display-name](https://babeljs.io/docs/babel-plugin-transform-react-display-name)
#[derive(Default)]
pub struct React {
jsx: ReactJsx,
jsx_self: ReactJsxSelf,
jsx_source: ReactJsxSource,
display_name: ReactDisplayName,
}
impl React {
pub fn new(&mut self, options: ReactOptions) -> &mut Self {
self.jsx = ReactJsx::new(options);
self
}
pub fn with_jsx_self(&mut self, options: ReactJsxSelfOptions) -> &mut Self {
self.jsx_self = ReactJsxSelf::new(options);
self
}
pub fn with_jsx_source(&mut self, options: ReactJsxSourceOptions) -> &mut Self {
self.jsx_source = ReactJsxSource::new(options);
self
}
pub fn with_display_name(&mut self, options: ReactDisplayNameOptions) -> &mut Self {
self.display_name = ReactDisplayName::new(options);
self
}
}

View file

@ -4,7 +4,7 @@ use serde::Deserialize;
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct ReactJsxOptions { pub struct ReactOptions {
// Both Runtimes // Both Runtimes
// //
/// Decides which runtime to use. /// Decides which runtime to use.
@ -53,7 +53,7 @@ pub struct ReactJsxOptions {
// `useBuiltIns` and `useSpread` are deprecated in babel 8. // `useBuiltIns` and `useSpread` are deprecated in babel 8.
} }
impl Default for ReactJsxOptions { impl Default for ReactOptions {
fn default() -> Self { fn default() -> Self {
Self { Self {
runtime: ReactJsxRuntime::default(), runtime: ReactJsxRuntime::default(),

View file

@ -1,30 +0,0 @@
use serde::Deserialize;
#[derive(Debug, Default, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ReactJsxSourceOptions;
/// [plugin-transform-react-jsx](https://babeljs.io/docs/babel-plugin-transform-react-jsx)
///
/// This plugin generates production-ready JS code.
///
/// If you are developing a React app in a development environment,
/// please use @babel/plugin-transform-react-jsx-development for a better debugging experience.
///
/// This plugin is included in `preset-react`.
///
/// ## Example
///
/// In: `<sometag />`
/// Out: `<sometag __source={ { fileName: 'this/file.js', lineNumber: 10, columnNumber: 1 } } />`
#[derive(Debug, Default)]
pub struct ReactJsxSource {
#[allow(unused)]
options: ReactJsxSourceOptions,
}
impl ReactJsxSource {
pub fn new(options: ReactJsxSourceOptions) -> Self {
Self { options }
}
}

View file

@ -4,7 +4,11 @@ use serde::Deserialize;
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct TypeScriptOptions; pub struct TypeScriptOptions;
/// [plugin-transform-typescript](https://babeljs.io/docs/babel-plugin-transform-typescript) /// [Preset TypeScript](https://babeljs.io/docs/babel-preset-typescript)
///
/// This preset includes the following plugins:
///
/// * [transform-typescript](https://babeljs.io/docs/babel-plugin-transform-typescript)
/// ///
/// This plugin adds support for the types syntax used by the TypeScript programming language. /// This plugin adds support for the types syntax used by the TypeScript programming language.
/// However, this plugin does not add the ability to type-check the JavaScript passed to it. /// However, this plugin does not add the ability to type-check the JavaScript passed to it.

View file

@ -14,8 +14,7 @@ use oxc_semantic::SemanticBuilder;
use oxc_span::{SourceType, VALID_EXTENSIONS}; use oxc_span::{SourceType, VALID_EXTENSIONS};
use oxc_tasks_common::{normalize_path, print_diff_in_terminal, BabelOptions}; use oxc_tasks_common::{normalize_path, print_diff_in_terminal, BabelOptions};
use oxc_transformer::{ use oxc_transformer::{
DecoratorsOptions, ReactDisplayNameOptions, ReactJsxOptions, ReactJsxSelfOptions, DecoratorsOptions, ReactOptions, TransformOptions, Transformer, TypeScriptOptions,
ReactJsxSourceOptions, TransformOptions, Transformer, TypeScriptOptions,
}; };
use crate::{fixture_root, root, TestRunnerEnv}; use crate::{fixture_root, root, TestRunnerEnv};
@ -96,21 +95,9 @@ pub trait TestCase {
.get_plugin("transform-typescript") .get_plugin("transform-typescript")
.map(get_options::<TypeScriptOptions>) .map(get_options::<TypeScriptOptions>)
.unwrap_or_default(), .unwrap_or_default(),
react_display_name: options react: options
.get_plugin("transform-react-display-name")
.map(get_options::<ReactDisplayNameOptions>)
.unwrap_or_default(),
react_jsx: options
.get_plugin("transform-react-jsx") .get_plugin("transform-react-jsx")
.map(get_options::<ReactJsxOptions>) .map(get_options::<ReactOptions>)
.unwrap_or_default(),
react_jsx_self: options
.get_plugin("transform-react-jsx-self")
.map(get_options::<ReactJsxSelfOptions>)
.unwrap_or_default(),
react_jsx_source: options
.get_plugin("transform-react-jsx-source")
.map(get_options::<ReactJsxSourceOptions>)
.unwrap_or_default(), .unwrap_or_default(),
} }
} }