Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ mod regexp;
mod typescript;

mod decorator;
mod plugins;

use common::Common;
use context::{TransformCtx, TraverseCtx};
Expand All @@ -56,6 +57,7 @@ use rustc_hash::FxHashMap;
use state::TransformState;
use typescript::TypeScript;

use crate::plugins::Plugins;
pub use crate::{
common::helper_loader::{Helper, HelperLoaderMode, HelperLoaderOptions},
compiler_assumptions::CompilerAssumptions,
Expand All @@ -73,6 +75,7 @@ pub use crate::{
ESTarget, Engine, EngineTargets, EnvOptions, Module, TransformOptions,
babel::{BabelEnvOptions, BabelOptions},
},
plugins::{PluginsOptions, StyledComponentsOptions},
proposals::ProposalOptions,
typescript::{RewriteExtensionsMode, TypeScriptOptions},
};
Expand All @@ -92,6 +95,7 @@ pub struct Transformer<'a> {

typescript: TypeScriptOptions,
decorator: DecoratorOptions,
plugins: PluginsOptions,
jsx: JsxOptions,
env: EnvOptions,
proposals: ProposalOptions,
Expand All @@ -105,6 +109,7 @@ impl<'a> Transformer<'a> {
allocator,
typescript: options.typescript.clone(),
decorator: options.decorator,
plugins: options.plugins.clone(),
jsx: options.jsx.clone(),
env: options.env,
proposals: options.proposals,
Expand Down Expand Up @@ -134,6 +139,7 @@ impl<'a> Transformer<'a> {
let mut transformer = TransformerImpl {
common: Common::new(&self.env, &self.ctx),
decorator: Decorator::new(self.decorator, &self.ctx),
plugins: Plugins::new(self.plugins, &self.ctx),
explicit_resource_management: self
.proposals
.explicit_resource_management
Expand Down Expand Up @@ -171,6 +177,7 @@ struct TransformerImpl<'a, 'ctx> {
// NOTE: all callbacks must run in order.
x0_typescript: Option<TypeScript<'a, 'ctx>>,
decorator: Decorator<'a, 'ctx>,
plugins: Plugins<'a, 'ctx>,
explicit_resource_management: Option<ExplicitResourceManagement<'a, 'ctx>>,
x1_jsx: Jsx<'a, 'ctx>,
x2_es2022: ES2022<'a, 'ctx>,
Expand All @@ -191,6 +198,7 @@ impl<'a> Traverse<'a, TransformState<'a>> for TransformerImpl<'a, '_> {
if let Some(typescript) = self.x0_typescript.as_mut() {
typescript.enter_program(program, ctx);
}
self.plugins.enter_program(program, ctx);
self.x1_jsx.enter_program(program, ctx);
if let Some(explicit_resource_management) = self.explicit_resource_management.as_mut() {
explicit_resource_management.enter_program(program, ctx);
Expand Down Expand Up @@ -236,6 +244,7 @@ impl<'a> Traverse<'a, TransformState<'a>> for TransformerImpl<'a, '_> {
if let Some(typescript) = self.x0_typescript.as_mut() {
typescript.enter_variable_declarator(decl, ctx);
}
self.plugins.enter_variable_declarator(decl, ctx);
}

fn enter_big_int_literal(&mut self, node: &mut BigIntLiteral<'a>, ctx: &mut TraverseCtx<'a>) {
Expand Down Expand Up @@ -268,6 +277,7 @@ impl<'a> Traverse<'a, TransformState<'a>> for TransformerImpl<'a, '_> {
if let Some(typescript) = self.x0_typescript.as_mut() {
typescript.enter_call_expression(expr, ctx);
}
self.plugins.enter_call_expression(expr, ctx);
self.x1_jsx.enter_call_expression(expr, ctx);
}

Expand Down Expand Up @@ -318,6 +328,7 @@ impl<'a> Traverse<'a, TransformState<'a>> for TransformerImpl<'a, '_> {
if let Some(typescript) = self.x0_typescript.as_mut() {
typescript.enter_expression(expr, ctx);
}
self.plugins.enter_expression(expr, ctx);
self.x2_es2022.enter_expression(expr, ctx);
self.x2_es2021.enter_expression(expr, ctx);
self.x2_es2020.enter_expression(expr, ctx);
Expand Down
9 changes: 9 additions & 0 deletions crates/oxc_transformer/src/options/babel/plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use serde::Deserialize;
use crate::{
DecoratorOptions, TypeScriptOptions, es2015::ArrowFunctionsOptions,
es2018::ObjectRestSpreadOptions, es2022::ClassPropertiesOptions, jsx::JsxOptions,
plugins::StyledComponentsOptions,
};

use super::PluginPresetEntries;
Expand Down Expand Up @@ -73,6 +74,8 @@ pub struct BabelPlugins {
pub legacy_decorator: Option<DecoratorOptions>,
// Proposals
pub explicit_resource_management: bool,
// Built-in plugins
pub styled_components: Option<StyledComponentsOptions>,
}

impl TryFrom<PluginPresetEntries> for BabelPlugins {
Expand Down Expand Up @@ -161,6 +164,12 @@ impl TryFrom<PluginPresetEntries> for BabelPlugins {
entry.value::<DecoratorOptions>().map_err(|err| p.errors.push(err)).ok();
}
"proposal-explicit-resource-management" => p.explicit_resource_management = true,
"styled-components" => {
p.styled_components = entry
.value::<StyledComponentsOptions>()
.map_err(|err| p.errors.push(err))
.ok();
}
s => p.unsupported.push(s.to_string()),
}
}
Expand Down
11 changes: 11 additions & 0 deletions crates/oxc_transformer/src/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{
es2021::ES2021Options,
es2022::ES2022Options,
jsx::JsxOptions,
plugins::{PluginsOptions, StyledComponentsOptions},
proposals::ProposalOptions,
regexp::RegExpOptions,
typescript::TypeScriptOptions,
Expand Down Expand Up @@ -69,6 +70,9 @@ pub struct TransformOptions {
/// Proposals
pub proposals: ProposalOptions,

/// Plugins
pub plugins: PluginsOptions,

pub helper_loader: HelperLoaderOptions,
}

Expand All @@ -90,6 +94,7 @@ impl TransformOptions {
},
env: EnvOptions::enable_all(/* include_unfinished_plugins */ false),
proposals: ProposalOptions::default(),
plugins: PluginsOptions { styled_components: Some(StyledComponentsOptions::default()) },
helper_loader: HelperLoaderOptions {
mode: HelperLoaderMode::Runtime,
..Default::default()
Expand Down Expand Up @@ -258,6 +263,11 @@ impl TryFrom<&BabelOptions> for TransformOptions {
..HelperLoaderOptions::default()
};

let mut plugins = PluginsOptions::default();
if let Some(styled_components) = &options.plugins.styled_components {
plugins.styled_components = Some(styled_components.clone());
}

Ok(Self {
cwd: options.cwd.clone().unwrap_or_default(),
assumptions: options.assumptions,
Expand All @@ -280,6 +290,7 @@ impl TryFrom<&BabelOptions> for TransformOptions {
explicit_resource_management: options.plugins.explicit_resource_management,
},
helper_loader,
plugins,
})
}
}
62 changes: 62 additions & 0 deletions crates/oxc_transformer/src/plugins/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
mod options;
mod styled_components;

pub use options::PluginsOptions;
use oxc_ast::ast::*;
use oxc_traverse::Traverse;
pub use styled_components::StyledComponentsOptions;

use crate::{
context::{TransformCtx, TraverseCtx},
plugins::styled_components::StyledComponents,
state::TransformState,
};

pub struct Plugins<'a, 'ctx> {
styled_components: StyledComponents<'a, 'ctx>,
options: PluginsOptions,
}

impl<'a, 'ctx> Plugins<'a, 'ctx> {
pub fn new(options: PluginsOptions, ctx: &'ctx TransformCtx<'a>) -> Self {
Self {
styled_components: StyledComponents::new(
options.styled_components.clone().unwrap_or_default(),
ctx,
),
options,
}
}
}

impl<'a> Traverse<'a, TransformState<'a>> for Plugins<'a, '_> {
fn enter_program(&mut self, node: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
if self.options.styled_components.is_some() {
self.styled_components.enter_program(node, ctx);
}
}

fn enter_variable_declarator(
&mut self,
node: &mut VariableDeclarator<'a>,
ctx: &mut TraverseCtx<'a>,
) {
if self.options.styled_components.is_some() {
self.styled_components.enter_variable_declarator(node, ctx);
}
}

fn enter_expression(
&mut self,
node: &mut Expression<'a>,
ctx: &mut oxc_traverse::TraverseCtx<'a, TransformState<'a>>,
) {
self.styled_components.enter_expression(node, ctx);
}

fn enter_call_expression(&mut self, node: &mut CallExpression<'a>, ctx: &mut TraverseCtx<'a>) {
if self.options.styled_components.is_some() {
self.styled_components.enter_call_expression(node, ctx);
}
}
}
6 changes: 6 additions & 0 deletions crates/oxc_transformer/src/plugins/options.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use super::StyledComponentsOptions;

#[derive(Default, Debug, Clone)]
pub struct PluginsOptions {
pub styled_components: Option<StyledComponentsOptions>,
}
Loading
Loading