mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
refactor(transform): deserialize BabelPreests::env directly (#7051)
This commit is contained in:
parent
a3b68b4224
commit
7f1d1fe065
4 changed files with 90 additions and 91 deletions
|
|
@ -56,7 +56,7 @@ pub use crate::{
|
||||||
jsx::{JsxOptions, JsxRuntime, ReactRefreshOptions},
|
jsx::{JsxOptions, JsxRuntime, ReactRefreshOptions},
|
||||||
options::{
|
options::{
|
||||||
babel::{BabelEnvOptions, BabelOptions, Targets},
|
babel::{BabelEnvOptions, BabelOptions, Targets},
|
||||||
TransformOptions,
|
EnvOptions, TransformOptions,
|
||||||
},
|
},
|
||||||
plugins::*,
|
plugins::*,
|
||||||
typescript::{RewriteExtensionsMode, TypeScriptOptions},
|
typescript::{RewriteExtensionsMode, TypeScriptOptions},
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use super::{BabelEnvOptions, PluginPresetEntries};
|
use super::PluginPresetEntries;
|
||||||
|
|
||||||
use crate::{JsxOptions, TypeScriptOptions};
|
use crate::{EnvOptions, JsxOptions, TypeScriptOptions};
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone, Deserialize)]
|
#[derive(Debug, Default, Clone, Deserialize)]
|
||||||
#[serde(try_from = "PluginPresetEntries")]
|
#[serde(try_from = "PluginPresetEntries")]
|
||||||
|
|
@ -10,7 +10,7 @@ pub struct BabelPresets {
|
||||||
pub errors: Vec<String>,
|
pub errors: Vec<String>,
|
||||||
pub unsupported: Vec<String>,
|
pub unsupported: Vec<String>,
|
||||||
|
|
||||||
pub env: Option<BabelEnvOptions>,
|
pub env: Option<EnvOptions>,
|
||||||
|
|
||||||
pub jsx: Option<JsxOptions>,
|
pub jsx: Option<JsxOptions>,
|
||||||
|
|
||||||
|
|
@ -25,7 +25,7 @@ impl TryFrom<PluginPresetEntries> for BabelPresets {
|
||||||
for entry in entries.0 {
|
for entry in entries.0 {
|
||||||
match entry.name() {
|
match entry.name() {
|
||||||
"env" => {
|
"env" => {
|
||||||
p.env = entry.value::<BabelEnvOptions>().map_err(|err| p.errors.push(err)).ok();
|
p.env = entry.value::<EnvOptions>().map_err(|err| p.errors.push(err)).ok();
|
||||||
}
|
}
|
||||||
"typescript" => {
|
"typescript" => {
|
||||||
p.typescript =
|
p.typescript =
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use oxc_diagnostics::Error;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
es2015::ES2015Options, es2016::ES2016Options, es2017::ES2017Options, es2018::ES2018Options,
|
es2015::ES2015Options, es2016::ES2016Options, es2017::ES2017Options, es2018::ES2018Options,
|
||||||
|
|
@ -6,9 +6,10 @@ use crate::{
|
||||||
regexp::RegExpOptions,
|
regexp::RegExpOptions,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::babel::{BabelEnvOptions, BabelOptions};
|
use super::babel::BabelEnvOptions;
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone)]
|
#[derive(Debug, Default, Clone, Deserialize)]
|
||||||
|
#[serde(try_from = "BabelEnvOptions")]
|
||||||
pub struct EnvOptions {
|
pub struct EnvOptions {
|
||||||
pub regexp: RegExpOptions,
|
pub regexp: RegExpOptions,
|
||||||
|
|
||||||
|
|
@ -66,11 +67,11 @@ impl EnvOptions {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<&BabelEnvOptions> for EnvOptions {
|
impl TryFrom<BabelEnvOptions> for EnvOptions {
|
||||||
type Error = Vec<Error>;
|
type Error = String;
|
||||||
|
|
||||||
/// If there are any errors in the `options.targets``, they will be returned as a list of errors.
|
/// If there are any errors in the `options.targets``, they will be returned as a list of errors.
|
||||||
fn try_from(o: &BabelEnvOptions) -> Result<Self, Self::Error> {
|
fn try_from(o: BabelEnvOptions) -> Result<Self, Self::Error> {
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
regexp: RegExpOptions {
|
regexp: RegExpOptions {
|
||||||
sticky_flag: o.can_enable_plugin("transform-sticky-regex"),
|
sticky_flag: o.can_enable_plugin("transform-sticky-regex"),
|
||||||
|
|
@ -120,75 +121,3 @@ impl TryFrom<&BabelEnvOptions> for EnvOptions {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<&BabelOptions> for EnvOptions {
|
|
||||||
type Error = Vec<Error>;
|
|
||||||
|
|
||||||
/// If the `options` contains any unknown fields, they will be returned as a list of errors.
|
|
||||||
fn try_from(options: &BabelOptions) -> Result<Self, Self::Error> {
|
|
||||||
let env = options
|
|
||||||
.presets
|
|
||||||
.env
|
|
||||||
.as_ref()
|
|
||||||
.and_then(|env_options| EnvOptions::try_from(env_options).ok())
|
|
||||||
.unwrap_or_default();
|
|
||||||
|
|
||||||
let regexp = RegExpOptions {
|
|
||||||
sticky_flag: env.regexp.sticky_flag || options.plugins.sticky_flag,
|
|
||||||
unicode_flag: env.regexp.unicode_flag || options.plugins.unicode_flag,
|
|
||||||
dot_all_flag: env.regexp.dot_all_flag || options.plugins.dot_all_flag,
|
|
||||||
look_behind_assertions: env.regexp.look_behind_assertions
|
|
||||||
|| options.plugins.look_behind_assertions,
|
|
||||||
named_capture_groups: env.regexp.named_capture_groups
|
|
||||||
|| options.plugins.named_capture_groups,
|
|
||||||
unicode_property_escapes: env.regexp.unicode_property_escapes
|
|
||||||
|| options.plugins.unicode_property_escapes,
|
|
||||||
match_indices: env.regexp.match_indices,
|
|
||||||
set_notation: env.regexp.set_notation || options.plugins.set_notation,
|
|
||||||
};
|
|
||||||
|
|
||||||
let es2015 = ES2015Options {
|
|
||||||
arrow_function: options.plugins.arrow_function.or(env.es2015.arrow_function),
|
|
||||||
};
|
|
||||||
|
|
||||||
let es2016 = ES2016Options {
|
|
||||||
exponentiation_operator: options.plugins.exponentiation_operator
|
|
||||||
|| env.es2016.exponentiation_operator,
|
|
||||||
};
|
|
||||||
|
|
||||||
let es2017 = ES2017Options {
|
|
||||||
async_to_generator: options.plugins.async_to_generator || env.es2017.async_to_generator,
|
|
||||||
};
|
|
||||||
|
|
||||||
let es2018 = ES2018Options {
|
|
||||||
object_rest_spread: options
|
|
||||||
.plugins
|
|
||||||
.object_rest_spread
|
|
||||||
.or(env.es2018.object_rest_spread),
|
|
||||||
async_generator_functions: options.plugins.async_generator_functions
|
|
||||||
|| env.es2018.async_generator_functions,
|
|
||||||
};
|
|
||||||
|
|
||||||
let es2019 = ES2019Options {
|
|
||||||
optional_catch_binding: options.plugins.optional_catch_binding
|
|
||||||
|| env.es2019.optional_catch_binding,
|
|
||||||
};
|
|
||||||
|
|
||||||
let es2020 = ES2020Options {
|
|
||||||
nullish_coalescing_operator: options.plugins.nullish_coalescing_operator
|
|
||||||
|| env.es2020.nullish_coalescing_operator,
|
|
||||||
};
|
|
||||||
|
|
||||||
let es2021 = ES2021Options {
|
|
||||||
logical_assignment_operators: options.plugins.logical_assignment_operators
|
|
||||||
|| env.es2021.logical_assignment_operators,
|
|
||||||
};
|
|
||||||
|
|
||||||
let es2022 = ES2022Options {
|
|
||||||
class_static_block: options.plugins.class_static_block || env.es2022.class_static_block,
|
|
||||||
class_properties: options.plugins.class_properties.or(env.es2022.class_properties),
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(Self { regexp, es2015, es2016, es2017, es2018, es2019, es2020, es2021, es2022 })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -3,13 +3,23 @@ mod env;
|
||||||
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use env::EnvOptions;
|
|
||||||
use oxc_diagnostics::Error;
|
use oxc_diagnostics::Error;
|
||||||
|
|
||||||
|
pub use env::EnvOptions;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
common::helper_loader::{HelperLoaderMode, HelperLoaderOptions},
|
common::helper_loader::{HelperLoaderMode, HelperLoaderOptions},
|
||||||
compiler_assumptions::CompilerAssumptions,
|
compiler_assumptions::CompilerAssumptions,
|
||||||
|
es2015::ES2015Options,
|
||||||
|
es2016::ES2016Options,
|
||||||
|
es2017::ES2017Options,
|
||||||
|
es2018::ES2018Options,
|
||||||
|
es2019::ES2019Options,
|
||||||
|
es2020::ES2020Options,
|
||||||
|
es2021::ES2021Options,
|
||||||
|
es2022::ES2022Options,
|
||||||
jsx::JsxOptions,
|
jsx::JsxOptions,
|
||||||
|
regexp::RegExpOptions,
|
||||||
typescript::TypeScriptOptions,
|
typescript::TypeScriptOptions,
|
||||||
ReactRefreshOptions,
|
ReactRefreshOptions,
|
||||||
};
|
};
|
||||||
|
|
@ -100,12 +110,62 @@ impl TryFrom<&BabelOptions> for TransformOptions {
|
||||||
jsx_options
|
jsx_options
|
||||||
};
|
};
|
||||||
|
|
||||||
let env = match EnvOptions::try_from(options) {
|
let env = options.presets.env.clone().unwrap_or_default();
|
||||||
Ok(env) => Some(env),
|
|
||||||
Err(errs) => {
|
let regexp = RegExpOptions {
|
||||||
errors.extend(errs);
|
sticky_flag: env.regexp.sticky_flag || options.plugins.sticky_flag,
|
||||||
None
|
unicode_flag: env.regexp.unicode_flag || options.plugins.unicode_flag,
|
||||||
}
|
dot_all_flag: env.regexp.dot_all_flag || options.plugins.dot_all_flag,
|
||||||
|
look_behind_assertions: env.regexp.look_behind_assertions
|
||||||
|
|| options.plugins.look_behind_assertions,
|
||||||
|
named_capture_groups: env.regexp.named_capture_groups
|
||||||
|
|| options.plugins.named_capture_groups,
|
||||||
|
unicode_property_escapes: env.regexp.unicode_property_escapes
|
||||||
|
|| options.plugins.unicode_property_escapes,
|
||||||
|
match_indices: env.regexp.match_indices,
|
||||||
|
set_notation: env.regexp.set_notation || options.plugins.set_notation,
|
||||||
|
};
|
||||||
|
|
||||||
|
let es2015 = ES2015Options {
|
||||||
|
arrow_function: options.plugins.arrow_function.or(env.es2015.arrow_function),
|
||||||
|
};
|
||||||
|
|
||||||
|
let es2016 = ES2016Options {
|
||||||
|
exponentiation_operator: options.plugins.exponentiation_operator
|
||||||
|
|| env.es2016.exponentiation_operator,
|
||||||
|
};
|
||||||
|
|
||||||
|
let es2017 = ES2017Options {
|
||||||
|
async_to_generator: options.plugins.async_to_generator || env.es2017.async_to_generator,
|
||||||
|
};
|
||||||
|
|
||||||
|
let es2018 = ES2018Options {
|
||||||
|
object_rest_spread: options
|
||||||
|
.plugins
|
||||||
|
.object_rest_spread
|
||||||
|
.or(env.es2018.object_rest_spread),
|
||||||
|
async_generator_functions: options.plugins.async_generator_functions
|
||||||
|
|| env.es2018.async_generator_functions,
|
||||||
|
};
|
||||||
|
|
||||||
|
let es2019 = ES2019Options {
|
||||||
|
optional_catch_binding: options.plugins.optional_catch_binding
|
||||||
|
|| env.es2019.optional_catch_binding,
|
||||||
|
};
|
||||||
|
|
||||||
|
let es2020 = ES2020Options {
|
||||||
|
nullish_coalescing_operator: options.plugins.nullish_coalescing_operator
|
||||||
|
|| env.es2020.nullish_coalescing_operator,
|
||||||
|
};
|
||||||
|
|
||||||
|
let es2021 = ES2021Options {
|
||||||
|
logical_assignment_operators: options.plugins.logical_assignment_operators
|
||||||
|
|| env.es2021.logical_assignment_operators,
|
||||||
|
};
|
||||||
|
|
||||||
|
let es2022 = ES2022Options {
|
||||||
|
class_static_block: options.plugins.class_static_block || env.es2022.class_static_block,
|
||||||
|
class_properties: options.plugins.class_properties.or(env.es2022.class_properties),
|
||||||
};
|
};
|
||||||
|
|
||||||
if !errors.is_empty() {
|
if !errors.is_empty() {
|
||||||
|
|
@ -126,7 +186,17 @@ impl TryFrom<&BabelOptions> for TransformOptions {
|
||||||
assumptions: options.assumptions,
|
assumptions: options.assumptions,
|
||||||
typescript,
|
typescript,
|
||||||
jsx,
|
jsx,
|
||||||
env: env.unwrap_or_default(),
|
env: EnvOptions {
|
||||||
|
regexp,
|
||||||
|
es2015,
|
||||||
|
es2016,
|
||||||
|
es2017,
|
||||||
|
es2018,
|
||||||
|
es2019,
|
||||||
|
es2020,
|
||||||
|
es2021,
|
||||||
|
es2022,
|
||||||
|
},
|
||||||
helper_loader,
|
helper_loader,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue