From 51a78d594640a069db53940bdb838fb420ea698c Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Tue, 1 Oct 2024 03:44:53 +0000 Subject: [PATCH] feat(napi/transform): rename all mention of React to Jsx; remove mention of `Binding` (#6198) This does not alter field names nor file names. part of #6156 --- crates/oxc_transformer/src/lib.rs | 2 +- .../src/options/transformer.rs | 20 ++++---- crates/oxc_transformer/src/react/comments.rs | 10 ++-- crates/oxc_transformer/src/react/jsx.rs | 10 ++-- crates/oxc_transformer/src/react/mod.rs | 12 ++--- crates/oxc_transformer/src/react/options.rs | 14 +++--- napi/transform/index.d.ts | 12 ++--- napi/transform/src/options.rs | 49 +++++++++---------- tasks/coverage/src/tools/semantic.rs | 8 ++- tasks/coverage/src/tools/transformer.rs | 6 +-- 10 files changed, 67 insertions(+), 76 deletions(-) diff --git a/crates/oxc_transformer/src/lib.rs b/crates/oxc_transformer/src/lib.rs index 159017bb92b6f..2002b1c731f85 100644 --- a/crates/oxc_transformer/src/lib.rs +++ b/crates/oxc_transformer/src/lib.rs @@ -52,7 +52,7 @@ pub use crate::{ env::{EnvOptions, Targets}, es2015::{ArrowFunctionsOptions, ES2015Options}, options::{BabelOptions, TransformOptions}, - react::{ReactJsxRuntime, ReactOptions, ReactRefreshOptions}, + react::{JsxOptions, JsxRuntime, ReactRefreshOptions}, typescript::{RewriteExtensionsMode, TypeScriptOptions}, }; use crate::{context::TransformCtx, es2015::ES2015, react::React, typescript::TypeScript}; diff --git a/crates/oxc_transformer/src/options/transformer.rs b/crates/oxc_transformer/src/options/transformer.rs index 233db466627f6..fc559ea55c722 100644 --- a/crates/oxc_transformer/src/options/transformer.rs +++ b/crates/oxc_transformer/src/options/transformer.rs @@ -13,7 +13,7 @@ use crate::{ es2020::ES2020Options, es2021::ES2021Options, options::babel::BabelOptions, - react::ReactOptions, + react::JsxOptions, regexp::RegExpOptions, typescript::TypeScriptOptions, ReactRefreshOptions, @@ -38,7 +38,7 @@ pub struct TransformOptions { pub typescript: TypeScriptOptions, /// [preset-react](https://babeljs.io/docs/babel-preset-react) - pub react: ReactOptions, + pub react: JsxOptions, pub regexp: RegExpOptions, @@ -62,10 +62,10 @@ impl TransformOptions { cwd: PathBuf::new(), assumptions: CompilerAssumptions::default(), typescript: TypeScriptOptions::default(), - react: ReactOptions { + react: JsxOptions { development: true, refresh: Some(ReactRefreshOptions::default()), - ..ReactOptions::default() + ..JsxOptions::default() }, regexp: RegExpOptions { sticky_flag: true, @@ -155,11 +155,11 @@ impl TransformOptions { let preset_name = "react"; transformer_options.react = if let Some(value) = get_preset_options(preset_name, options) { - match from_value::(value) { + match from_value::(value) { Ok(res) => res, Err(err) => { report_error(preset_name, &err, true, &mut errors); - ReactOptions::default() + JsxOptions::default() } } } else { @@ -168,17 +168,17 @@ impl TransformOptions { let mut react_options = if has_jsx_plugin { let plugin_name = "transform-react-jsx"; - from_value::(get_plugin_options(plugin_name, options)) + from_value::(get_plugin_options(plugin_name, options)) .unwrap_or_else(|err| { report_error(plugin_name, &err, false, &mut errors); - ReactOptions::default() + JsxOptions::default() }) } else { let plugin_name = "transform-react-jsx-development"; - from_value::(get_plugin_options(plugin_name, options)) + from_value::(get_plugin_options(plugin_name, options)) .unwrap_or_else(|err| { report_error(plugin_name, &err, false, &mut errors); - ReactOptions::default() + JsxOptions::default() }) }; react_options.development = has_jsx_development_plugin; diff --git a/crates/oxc_transformer/src/react/comments.rs b/crates/oxc_transformer/src/react/comments.rs index 3a65d1693a404..355c67e4f7f9e 100644 --- a/crates/oxc_transformer/src/react/comments.rs +++ b/crates/oxc_transformer/src/react/comments.rs @@ -1,7 +1,7 @@ use oxc_ast::{Comment, CommentKind}; use oxc_syntax::identifier::is_irregular_whitespace; -use crate::{ReactJsxRuntime, ReactOptions, TransformCtx}; +use crate::{JsxOptions, JsxRuntime, TransformCtx}; /// Scan through all comments and find the following pragmas: /// @@ -14,13 +14,13 @@ use crate::{ReactJsxRuntime, ReactOptions, TransformCtx}; /// otherwise `JSDoc` could be used instead. /// /// This behavior is aligned with Babel. -pub(crate) fn update_options_with_comments(options: &mut ReactOptions, ctx: &TransformCtx) { +pub(crate) fn update_options_with_comments(options: &mut JsxOptions, ctx: &TransformCtx) { for comment in ctx.trivias.comments() { update_options_with_comment(options, comment, ctx.source_text); } } -fn update_options_with_comment(options: &mut ReactOptions, comment: &Comment, source_text: &str) { +fn update_options_with_comment(options: &mut JsxOptions, comment: &Comment, source_text: &str) { let Some((keyword, remainder)) = find_jsx_pragma(comment, source_text) else { return }; match keyword { @@ -31,8 +31,8 @@ fn update_options_with_comment(options: &mut ReactOptions, comment: &Comment, so // @jsxRuntime "Runtime" => { options.runtime = match remainder { - "classic" => ReactJsxRuntime::Classic, - "automatic" => ReactJsxRuntime::Automatic, + "classic" => JsxRuntime::Classic, + "automatic" => JsxRuntime::Automatic, _ => return, }; } diff --git a/crates/oxc_transformer/src/react/jsx.rs b/crates/oxc_transformer/src/react/jsx.rs index f3fa613abd495..15531e910e7b5 100644 --- a/crates/oxc_transformer/src/react/jsx.rs +++ b/crates/oxc_transformer/src/react/jsx.rs @@ -103,7 +103,7 @@ use super::diagnostics; pub use super::{ jsx_self::ReactJsxSelf, jsx_source::ReactJsxSource, - options::{ReactJsxRuntime, ReactOptions}, + options::{JsxOptions, JsxRuntime}, }; use crate::{ helpers::{bindings::BoundIdentifier, module_imports::NamedImport}, @@ -111,7 +111,7 @@ use crate::{ }; pub struct ReactJsx<'a, 'ctx> { - options: ReactOptions, + options: JsxOptions, ctx: &'ctx TransformCtx<'a>, @@ -370,9 +370,9 @@ impl<'a> Pragma<'a> { } impl<'a, 'ctx> ReactJsx<'a, 'ctx> { - pub fn new(options: ReactOptions, ctx: &'ctx TransformCtx<'a>) -> Self { + pub fn new(options: JsxOptions, ctx: &'ctx TransformCtx<'a>) -> Self { let bindings = match options.runtime { - ReactJsxRuntime::Classic => { + JsxRuntime::Classic => { if options.import_source.is_some() { ctx.error(diagnostics::import_source_cannot_be_set()); } @@ -380,7 +380,7 @@ impl<'a, 'ctx> ReactJsx<'a, 'ctx> { let pragma_frag = Pragma::parse(options.pragma_frag.as_ref(), "Fragment", ctx); Bindings::Classic(ClassicBindings { pragma, pragma_frag }) } - ReactJsxRuntime::Automatic => { + JsxRuntime::Automatic => { if options.pragma.is_some() || options.pragma_frag.is_some() { ctx.error(diagnostics::pragma_and_pragma_frag_cannot_be_set()); } diff --git a/crates/oxc_transformer/src/react/mod.rs b/crates/oxc_transformer/src/react/mod.rs index ea10e58ea2d5e..c7ecde43c0392 100644 --- a/crates/oxc_transformer/src/react/mod.rs +++ b/crates/oxc_transformer/src/react/mod.rs @@ -16,7 +16,7 @@ use refresh::ReactRefresh; pub use self::{ display_name::ReactDisplayName, jsx::ReactJsx, - options::{ReactJsxRuntime, ReactOptions, ReactRefreshOptions}, + options::{JsxOptions, JsxRuntime, ReactRefreshOptions}, }; use crate::TransformCtx; @@ -43,17 +43,13 @@ pub struct React<'a, 'ctx> { // Constructors impl<'a, 'ctx> React<'a, 'ctx> { - pub fn new(mut options: ReactOptions, ctx: &'ctx TransformCtx<'a>) -> Self { + pub fn new(mut options: JsxOptions, ctx: &'ctx TransformCtx<'a>) -> Self { if options.jsx_plugin || options.development { update_options_with_comments(&mut options, ctx); options.conform(); } - let ReactOptions { - jsx_plugin, - display_name_plugin, - jsx_self_plugin, - jsx_source_plugin, - .. + let JsxOptions { + jsx_plugin, display_name_plugin, jsx_self_plugin, jsx_source_plugin, .. } = options; let refresh = options.refresh.clone(); Self { diff --git a/crates/oxc_transformer/src/react/options.rs b/crates/oxc_transformer/src/react/options.rs index 91a827d4be61c..a7c0bf6b0aca4 100644 --- a/crates/oxc_transformer/src/react/options.rs +++ b/crates/oxc_transformer/src/react/options.rs @@ -11,14 +11,14 @@ fn default_as_true() -> bool { /// classic does not automatic import anything. #[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Deserialize)] #[serde(rename_all = "camelCase")] -pub enum ReactJsxRuntime { +pub enum JsxRuntime { Classic, /// The default runtime is switched to automatic in Babel 8. #[default] Automatic, } -impl ReactJsxRuntime { +impl JsxRuntime { pub fn is_classic(self) -> bool { self == Self::Classic } @@ -30,7 +30,7 @@ impl ReactJsxRuntime { #[derive(Debug, Clone, Deserialize)] #[serde(default, rename_all = "camelCase", deny_unknown_fields)] -pub struct ReactOptions { +pub struct JsxOptions { #[serde(skip)] pub jsx_plugin: bool, @@ -46,7 +46,7 @@ pub struct ReactOptions { // Both Runtimes // /// Decides which runtime to use. - pub runtime: ReactJsxRuntime, + pub runtime: JsxRuntime, /// This toggles behavior specific to development, such as adding __source and __self. /// @@ -107,14 +107,14 @@ pub struct ReactOptions { pub refresh: Option, } -impl Default for ReactOptions { +impl Default for JsxOptions { fn default() -> Self { Self { jsx_plugin: true, display_name_plugin: true, jsx_self_plugin: false, jsx_source_plugin: false, - runtime: ReactJsxRuntime::default(), + runtime: JsxRuntime::default(), development: false, throw_if_namespace: default_as_true(), pure: default_as_true(), @@ -128,7 +128,7 @@ impl Default for ReactOptions { } } -impl ReactOptions { +impl JsxOptions { pub fn conform(&mut self) { if self.development { self.jsx_plugin = true; diff --git a/napi/transform/index.d.ts b/napi/transform/index.d.ts index 4e78bfd82d6d1..6df4cae4d7c13 100644 --- a/napi/transform/index.d.ts +++ b/napi/transform/index.d.ts @@ -44,7 +44,7 @@ export interface IsolatedDeclarationsResult { * * @see {@link https://babeljs.io/docs/babel-plugin-transform-react-jsx#options} */ -export interface ReactBindingOptions { +export interface JsxOptions { /** * Decides which runtime to use. * @@ -131,10 +131,10 @@ export interface ReactBindingOptions { * * @default false */ - refresh?: boolean | ReactRefreshBindingOptions + refresh?: boolean | ReactRefreshOptions } -export interface ReactRefreshBindingOptions { +export interface ReactRefreshOptions { /** * Specify the identifier of the refresh registration variable. * @@ -188,9 +188,9 @@ export interface TransformOptions { */ cwd?: string /** Configure how TypeScript is transformed. */ - typescript?: TypeScriptBindingOptions + typescript?: TypeScriptOptions /** Configure how TSX and JSX are transformed. */ - react?: ReactBindingOptions + react?: JsxOptions /** Enable ES2015 transformations. */ es2015?: ES2015BindingOptions /** @@ -245,7 +245,7 @@ export interface TransformResult { errors: Array } -export interface TypeScriptBindingOptions { +export interface TypeScriptOptions { jsxPragma?: string jsxPragmaFrag?: string onlyRemoveTypeImports?: boolean diff --git a/napi/transform/src/options.rs b/napi/transform/src/options.rs index 2dc26f524dc65..8cd3de7be6967 100644 --- a/napi/transform/src/options.rs +++ b/napi/transform/src/options.rs @@ -4,10 +4,7 @@ use std::path::PathBuf; use napi::Either; use napi_derive::napi; -use oxc_transformer::{ - ArrowFunctionsOptions, ES2015Options, ReactJsxRuntime, ReactOptions, ReactRefreshOptions, - RewriteExtensionsMode, TypeScriptOptions, -}; +use oxc_transformer::{ArrowFunctionsOptions, ES2015Options, JsxRuntime, RewriteExtensionsMode}; use crate::IsolatedDeclarationsOptions; @@ -25,10 +22,10 @@ pub struct TransformOptions { pub cwd: Option, /// Configure how TypeScript is transformed. - pub typescript: Option, + pub typescript: Option, /// Configure how TSX and JSX are transformed. - pub react: Option, + pub react: Option, /// Enable ES2015 transformations. pub es2015: Option, @@ -57,7 +54,7 @@ impl From for oxc_transformer::TransformOptions { #[napi(object)] #[derive(Default)] -pub struct TypeScriptBindingOptions { +pub struct TypeScriptOptions { pub jsx_pragma: Option, pub jsx_pragma_frag: Option, pub only_remove_type_imports: Option, @@ -83,10 +80,10 @@ pub struct TypeScriptBindingOptions { pub rewrite_import_extensions: Option>, } -impl From for TypeScriptOptions { - fn from(options: TypeScriptBindingOptions) -> Self { - let ops = TypeScriptOptions::default(); - TypeScriptOptions { +impl From for oxc_transformer::TypeScriptOptions { + fn from(options: TypeScriptOptions) -> Self { + let ops = oxc_transformer::TypeScriptOptions::default(); + oxc_transformer::TypeScriptOptions { jsx_pragma: options.jsx_pragma.map(Into::into).unwrap_or(ops.jsx_pragma), jsx_pragma_frag: options.jsx_pragma_frag.map(Into::into).unwrap_or(ops.jsx_pragma_frag), only_remove_type_imports: options @@ -119,7 +116,7 @@ impl From for TypeScriptOptions { /// /// @see {@link https://babeljs.io/docs/babel-plugin-transform-react-jsx#options} #[napi(object)] -pub struct ReactBindingOptions { +pub struct JsxOptions { /// Decides which runtime to use. /// /// - 'automatic' - auto-import the correct JSX factories @@ -196,16 +193,16 @@ pub struct ReactBindingOptions { /// Conforms to the implementation in {@link https://github.com/facebook/react/tree/main/packages/react-refresh} /// /// @default false - pub refresh: Option>, + pub refresh: Option>, } -impl From for ReactOptions { - fn from(options: ReactBindingOptions) -> Self { - let ops = ReactOptions::default(); - ReactOptions { +impl From for oxc_transformer::JsxOptions { + fn from(options: JsxOptions) -> Self { + let ops = oxc_transformer::JsxOptions::default(); + oxc_transformer::JsxOptions { runtime: match options.runtime.as_deref() { - Some("classic") => ReactJsxRuntime::Classic, - /* "automatic" */ _ => ReactJsxRuntime::Automatic, + Some("classic") => JsxRuntime::Classic, + /* "automatic" */ _ => JsxRuntime::Automatic, }, development: options.development.unwrap_or(ops.development), throw_if_namespace: options.throw_if_namespace.unwrap_or(ops.throw_if_namespace), @@ -216,8 +213,8 @@ impl From for ReactOptions { use_built_ins: options.use_built_ins, use_spread: options.use_spread, refresh: options.refresh.and_then(|value| match value { - Either::A(b) => b.then(ReactRefreshOptions::default), - Either::B(options) => Some(ReactRefreshOptions::from(options)), + Either::A(b) => b.then(oxc_transformer::ReactRefreshOptions::default), + Either::B(options) => Some(oxc_transformer::ReactRefreshOptions::from(options)), }), ..Default::default() } @@ -225,7 +222,7 @@ impl From for ReactOptions { } #[napi(object)] -pub struct ReactRefreshBindingOptions { +pub struct ReactRefreshOptions { /// Specify the identifier of the refresh registration variable. /// /// @default `$RefreshReg$`. @@ -239,10 +236,10 @@ pub struct ReactRefreshBindingOptions { pub emit_full_signatures: Option, } -impl From for ReactRefreshOptions { - fn from(options: ReactRefreshBindingOptions) -> Self { - let ops = ReactRefreshOptions::default(); - ReactRefreshOptions { +impl From for oxc_transformer::ReactRefreshOptions { + fn from(options: ReactRefreshOptions) -> Self { + let ops = oxc_transformer::ReactRefreshOptions::default(); + oxc_transformer::ReactRefreshOptions { refresh_reg: options.refresh_reg.unwrap_or(ops.refresh_reg), refresh_sig: options.refresh_sig.unwrap_or(ops.refresh_sig), emit_full_signatures: options.emit_full_signatures.unwrap_or(ops.emit_full_signatures), diff --git a/tasks/coverage/src/tools/semantic.rs b/tasks/coverage/src/tools/semantic.rs index 94cc04295f663..84abd3446aef7 100644 --- a/tasks/coverage/src/tools/semantic.rs +++ b/tasks/coverage/src/tools/semantic.rs @@ -2,9 +2,7 @@ use std::path::{Path, PathBuf}; use oxc::{ span::SourceType, - transformer::{ - ES2015Options, ReactJsxRuntime, ReactOptions, TransformOptions, TypeScriptOptions, - }, + transformer::{ES2015Options, JsxOptions, JsxRuntime, TransformOptions, TypeScriptOptions}, }; use crate::{ @@ -20,7 +18,7 @@ fn get_default_transformer_options() -> TransformOptions { TransformOptions { typescript: TypeScriptOptions::default(), es2015: ES2015Options { arrow_function: None }, - react: ReactOptions { + react: JsxOptions { jsx_plugin: true, jsx_self_plugin: true, jsx_source_plugin: true, @@ -152,7 +150,7 @@ impl Case for SemanticTypeScriptCase { // handle @jsx: react, `react` of behavior is match babel following options if self.base.settings.jsx.last().is_some_and(|jsx| jsx == "react") { source_type = source_type.with_module(true); - options.react.runtime = ReactJsxRuntime::Classic; + options.react.runtime = JsxRuntime::Classic; } get_result(self.base.code(), source_type, self.path(), Some(options)) } diff --git a/tasks/coverage/src/tools/transformer.rs b/tasks/coverage/src/tools/transformer.rs index ea682444912a5..6d195c7314cfe 100644 --- a/tasks/coverage/src/tools/transformer.rs +++ b/tasks/coverage/src/tools/transformer.rs @@ -3,7 +3,7 @@ use std::path::{Path, PathBuf}; use oxc::{ span::SourceType, transformer::{ - ArrowFunctionsOptions, ES2015Options, ReactJsxRuntime, ReactOptions, TransformOptions, + ArrowFunctionsOptions, ES2015Options, JsxOptions, JsxRuntime, TransformOptions, TypeScriptOptions, }, }; @@ -50,7 +50,7 @@ fn get_default_transformer_options() -> TransformOptions { TransformOptions { typescript: TypeScriptOptions::default(), es2015: ES2015Options { arrow_function: Some(ArrowFunctionsOptions::default()) }, - react: ReactOptions { + react: JsxOptions { jsx_plugin: true, jsx_self_plugin: true, jsx_source_plugin: true, @@ -158,7 +158,7 @@ impl Case for TransformerTypeScriptCase { // handle @jsx: react, `react` of behavior is match babel following options if self.base.settings.jsx.last().is_some_and(|jsx| jsx == "react") { source_type = source_type.with_module(true); - options.react.runtime = ReactJsxRuntime::Classic; + options.react.runtime = JsxRuntime::Classic; } get_result(self.base.code(), source_type, self.path(), Some(options)) }