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
This commit is contained in:
Boshen 2024-10-01 03:44:53 +00:00 committed by Boshen
parent f6a3450710
commit 51a78d5946
No known key found for this signature in database
GPG key ID: 67715A371E534061
10 changed files with 67 additions and 76 deletions

View file

@ -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};

View file

@ -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::<ReactOptions>(value) {
match from_value::<JsxOptions>(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::<ReactOptions>(get_plugin_options(plugin_name, options))
from_value::<JsxOptions>(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::<ReactOptions>(get_plugin_options(plugin_name, options))
from_value::<JsxOptions>(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;

View file

@ -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,
};
}

View file

@ -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());
}

View file

@ -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 {

View file

@ -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<ReactRefreshOptions>,
}
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;

View file

@ -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<string>
}
export interface TypeScriptBindingOptions {
export interface TypeScriptOptions {
jsxPragma?: string
jsxPragmaFrag?: string
onlyRemoveTypeImports?: boolean

View file

@ -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<String>,
/// Configure how TypeScript is transformed.
pub typescript: Option<TypeScriptBindingOptions>,
pub typescript: Option<TypeScriptOptions>,
/// Configure how TSX and JSX are transformed.
pub react: Option<ReactBindingOptions>,
pub react: Option<JsxOptions>,
/// Enable ES2015 transformations.
pub es2015: Option<ES2015BindingOptions>,
@ -57,7 +54,7 @@ impl From<TransformOptions> for oxc_transformer::TransformOptions {
#[napi(object)]
#[derive(Default)]
pub struct TypeScriptBindingOptions {
pub struct TypeScriptOptions {
pub jsx_pragma: Option<String>,
pub jsx_pragma_frag: Option<String>,
pub only_remove_type_imports: Option<bool>,
@ -83,10 +80,10 @@ pub struct TypeScriptBindingOptions {
pub rewrite_import_extensions: Option<Either<bool, String>>,
}
impl From<TypeScriptBindingOptions> for TypeScriptOptions {
fn from(options: TypeScriptBindingOptions) -> Self {
let ops = TypeScriptOptions::default();
TypeScriptOptions {
impl From<TypeScriptOptions> 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<TypeScriptBindingOptions> 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<Either<bool, ReactRefreshBindingOptions>>,
pub refresh: Option<Either<bool, ReactRefreshOptions>>,
}
impl From<ReactBindingOptions> for ReactOptions {
fn from(options: ReactBindingOptions) -> Self {
let ops = ReactOptions::default();
ReactOptions {
impl From<JsxOptions> 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<ReactBindingOptions> 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<ReactBindingOptions> 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<bool>,
}
impl From<ReactRefreshBindingOptions> for ReactRefreshOptions {
fn from(options: ReactRefreshBindingOptions) -> Self {
let ops = ReactRefreshOptions::default();
ReactRefreshOptions {
impl From<ReactRefreshOptions> 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),

View file

@ -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))
}

View file

@ -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))
}