Skip to content

Commit

Permalink
feat(napi/transform): rename all mention of React to Jsx; remove ment…
Browse files Browse the repository at this point in the history
…ion of `Binding` (#6198)

This does not alter field names nor file names.

part of #6156
  • Loading branch information
Boshen committed Oct 1, 2024
1 parent f6a3450 commit 51a78d5
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 76 deletions.
2 changes: 1 addition & 1 deletion crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
20 changes: 10 additions & 10 deletions crates/oxc_transformer/src/options/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
es2020::ES2020Options,
es2021::ES2021Options,
options::babel::BabelOptions,
react::ReactOptions,
react::JsxOptions,
regexp::RegExpOptions,
typescript::TypeScriptOptions,
ReactRefreshOptions,
Expand All @@ -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,

Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions crates/oxc_transformer/src/react/comments.rs
Original file line number Diff line number Diff line change
@@ -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:
///
Expand All @@ -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 {
Expand All @@ -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,
};
}
Expand Down
10 changes: 5 additions & 5 deletions crates/oxc_transformer/src/react/jsx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ 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},
TransformCtx,
};

pub struct ReactJsx<'a, 'ctx> {
options: ReactOptions,
options: JsxOptions,

ctx: &'ctx TransformCtx<'a>,

Expand Down Expand Up @@ -370,17 +370,17 @@ 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());
}
let pragma = Pragma::parse(options.pragma.as_ref(), "createElement", 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());
}
Expand Down
12 changes: 4 additions & 8 deletions crates/oxc_transformer/src/react/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 {
Expand Down
14 changes: 7 additions & 7 deletions crates/oxc_transformer/src/react/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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,

Expand All @@ -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.
///
Expand Down Expand Up @@ -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(),
Expand All @@ -128,7 +128,7 @@ impl Default for ReactOptions {
}
}

impl ReactOptions {
impl JsxOptions {
pub fn conform(&mut self) {
if self.development {
self.jsx_plugin = true;
Expand Down
12 changes: 6 additions & 6 deletions napi/transform/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
/**
Expand Down Expand Up @@ -245,7 +245,7 @@ export interface TransformResult {
errors: Array<string>
}

export interface TypeScriptBindingOptions {
export interface TypeScriptOptions {
jsxPragma?: string
jsxPragmaFrag?: string
onlyRemoveTypeImports?: boolean
Expand Down
Loading

0 comments on commit 51a78d5

Please sign in to comment.