Skip to content

Commit

Permalink
Merging...
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Feb 17, 2021
1 parent e87b947 commit 878ec00
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 106 deletions.
105 changes: 0 additions & 105 deletions ecmascript/transforms/react/src/jsx/jsx2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,111 +26,6 @@ use swc_ecma_visit::{
#[cfg(test)]
mod tests;

/// https://babeljs.io/docs/en/babel-plugin-transform-react-jsx#runtime
#[derive(StringEnum, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
pub enum Runtime {
/// `automatic`
Automatic,
/// `classic`
Classic,
}

/// Note: This will changed in v2
impl Default for Runtime {
fn default() -> Self {
Runtime::Classic
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub struct Options {
#[serde(default)]
pub runtime: Option<Runtime>,

/// For automatic runtime
#[serde(default = "default_import_source")]
pub import_source: String,

#[serde(default = "default_pragma")]
pub pragma: String,

#[serde(default = "default_pragma_frag")]
pub pragma_frag: String,

#[serde(default = "default_throw_if_namespace")]
pub throw_if_namespace: bool,

#[serde(default)]
pub development: bool,

#[serde(default)]
pub use_builtins: bool,

#[serde(default)]
pub use_spread: bool,
}

impl Default for Options {
fn default() -> Self {
Options {
runtime: Default::default(),
import_source: default_import_source(),
pragma: default_pragma(),
pragma_frag: default_pragma_frag(),
throw_if_namespace: default_throw_if_namespace(),
development: false,
use_builtins: false,
use_spread: false,
}
}
}

fn default_import_source() -> String {
"react".into()
}

fn default_pragma() -> String {
"React.createElement".into()
}

fn default_pragma_frag() -> String {
"React.Fragment".into()
}

fn default_throw_if_namespace() -> bool {
true
}

fn parse_classic_option(cm: &SourceMap, name: &str, src: String) -> Box<Expr> {
static CACHE: Lazy<DashMap<String, Box<Expr>>> = Lazy::new(|| DashMap::with_capacity(2));

let fm = cm.new_source_file(FileName::Custom(format!("<jsx-config-{}.js>", name)), src);
if let Some(expr) = CACHE.get(&**fm.src) {
return expr.clone();
}

let expr = Parser::new(Syntax::default(), StringInput::from(&*fm), None)
.parse_expr()
.map_err(|e| {
if HANDLER.is_set() {
HANDLER.with(|h| e.into_diagnostic(h).emit())
}
})
.map(drop_span)
.unwrap_or_else(|()| {
panic!(
"faield to parse jsx option {}: '{}' is not an expression",
name, fm.src,
)
});

CACHE.insert((*fm.src).clone(), expr.clone());

expr
}

/// `@babel/plugin-transform-react-jsx`
///
/// Turn JSX into React function calls
Expand Down
31 changes: 30 additions & 1 deletion ecmascript/transforms/react/src/jsx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,28 @@ mod jsx2;
#[cfg(test)]
mod tests;

/// https://babeljs.io/docs/en/babel-plugin-transform-react-jsx#runtime
#[derive(StringEnum, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
pub enum Runtime {
/// `automatic`
Automatic,
/// `classic`
Classic,
}

/// Note: This will changed in v2
impl Default for Runtime {
fn default() -> Self {
Runtime::Classic
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Options {
#[serde(default)]
pub runtime: Option<Runtime>,

#[serde(default = "default_pragma")]
pub pragma: String,
#[serde(default = "default_pragma_frag")]
Expand All @@ -44,20 +63,30 @@ pub struct Options {

#[serde(default)]
pub use_builtins: bool,

#[serde(default)]
pub use_spread: bool,
}

impl Default for Options {
fn default() -> Self {
Options {
runtime: Default::default(),
import_source: default_import_source(),
pragma: default_pragma(),
pragma_frag: default_pragma_frag(),
throw_if_namespace: default_throw_if_namespace(),
development: false,
use_builtins: false,
use_spread: false,
}
}
}

fn default_import_source() -> String {
"react".into()
}

fn default_pragma() -> String {
"React.createElement".into()
}
Expand All @@ -70,7 +99,7 @@ fn default_throw_if_namespace() -> bool {
true
}

fn parse_option(cm: &SourceMap, name: &str, src: String) -> Box<Expr> {
fn parse_classic_option(cm: &SourceMap, name: &str, src: String) -> Box<Expr> {
static CACHE: Lazy<DashMap<String, Box<Expr>>> = Lazy::new(|| DashMap::with_capacity(2));

let fm = cm.new_source_file(FileName::Custom(format!("<jsx-config-{}.js>", name)), src);
Expand Down

0 comments on commit 878ec00

Please sign in to comment.