mirror of
https://github.com/danbulant/oxc
synced 2026-05-25 04:42:10 +00:00
feat(transformer): add transform context to all plugins (#2931)
This commit is contained in:
parent
0a77d621e2
commit
255c74ccc5
9 changed files with 140 additions and 107 deletions
37
crates/oxc_transformer/src/context.rs
Normal file
37
crates/oxc_transformer/src/context.rs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
use std::{cell::RefCell, mem, rc::Rc};
|
||||
|
||||
use oxc_allocator::Allocator;
|
||||
use oxc_ast::AstBuilder;
|
||||
use oxc_diagnostics::Error;
|
||||
use oxc_semantic::Semantic;
|
||||
use oxc_span::SourceType;
|
||||
|
||||
pub type Ctx<'a> = Rc<TransformCtx<'a>>;
|
||||
|
||||
pub struct TransformCtx<'a> {
|
||||
pub ast: AstBuilder<'a>,
|
||||
pub source_type: SourceType,
|
||||
pub semantic: Semantic<'a>,
|
||||
errors: RefCell<Vec<Error>>,
|
||||
}
|
||||
|
||||
impl<'a> TransformCtx<'a> {
|
||||
pub fn new(allocator: &'a Allocator, source_type: SourceType, semantic: Semantic<'a>) -> Self {
|
||||
Self {
|
||||
ast: AstBuilder::new(allocator),
|
||||
source_type,
|
||||
semantic,
|
||||
errors: RefCell::new(vec![]),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn take_errors(&self) -> Vec<Error> {
|
||||
mem::take(&mut self.errors.borrow_mut())
|
||||
}
|
||||
|
||||
/// Add an Error
|
||||
#[allow(unused)]
|
||||
pub fn error<T: Into<Error>>(&self, error: T) {
|
||||
self.errors.borrow_mut().push(error.into());
|
||||
}
|
||||
}
|
||||
|
|
@ -1,26 +1,30 @@
|
|||
use std::rc::Rc;
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
use oxc_ast::ast::*;
|
||||
|
||||
use crate::context::Ctx;
|
||||
|
||||
/// Only `"2023-11"` will be implemented because Babel 8 will only support "2023-11" and "legacy".
|
||||
#[derive(Debug, Default, Clone, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct DecoratorsOptions;
|
||||
|
||||
/// [proposal-decorators](https://babeljs.io/docs/babel-plugin-proposal-decorators)
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Decorators {
|
||||
#[allow(unused)]
|
||||
#[allow(unused)]
|
||||
pub struct Decorators<'a> {
|
||||
options: DecoratorsOptions,
|
||||
ctx: Ctx<'a>,
|
||||
}
|
||||
|
||||
impl Decorators {
|
||||
pub fn new(options: DecoratorsOptions) -> Self {
|
||||
Self { options }
|
||||
impl<'a> Decorators<'a> {
|
||||
pub fn new(options: DecoratorsOptions, ctx: &Ctx<'a>) -> Self {
|
||||
Self { options, ctx: Rc::clone(ctx) }
|
||||
}
|
||||
}
|
||||
|
||||
// Transformers
|
||||
impl Decorators {
|
||||
impl<'a> Decorators<'a> {
|
||||
pub fn transform_statement(&mut self, _stmt: &mut Statement<'_>) {}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,11 +9,14 @@
|
|||
|
||||
// Core
|
||||
mod compiler_assumptions;
|
||||
// Plugins: <https://babeljs.io/docs/plugins-list>
|
||||
mod context;
|
||||
// Presets: <https://babel.dev/docs/presets>
|
||||
mod decorators;
|
||||
mod react;
|
||||
mod typescript;
|
||||
|
||||
use std::rc::Rc;
|
||||
|
||||
use oxc_allocator::Allocator;
|
||||
use oxc_ast::{
|
||||
ast::*,
|
||||
|
|
@ -26,13 +29,12 @@ use oxc_span::SourceType;
|
|||
pub use crate::{
|
||||
compiler_assumptions::CompilerAssumptions,
|
||||
decorators::{Decorators, DecoratorsOptions},
|
||||
react::{
|
||||
React, ReactDisplayName, ReactDisplayNameOptions, ReactJsx, ReactJsxSelf, ReactJsxSource,
|
||||
ReactJsxSourceOptions, ReactOptions,
|
||||
},
|
||||
react::{React, ReactDisplayName, ReactJsx, ReactJsxSelf, ReactJsxSource, ReactOptions},
|
||||
typescript::{TypeScript, TypeScriptOptions},
|
||||
};
|
||||
|
||||
use crate::context::{Ctx, TransformCtx};
|
||||
|
||||
#[allow(unused)]
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct TransformOptions {
|
||||
|
|
@ -54,15 +56,11 @@ pub struct TransformOptions {
|
|||
|
||||
#[allow(unused)]
|
||||
pub struct Transformer<'a> {
|
||||
allocator: &'a Allocator,
|
||||
source_type: SourceType,
|
||||
semantic: Semantic<'a>,
|
||||
options: TransformOptions,
|
||||
|
||||
ctx: Ctx<'a>,
|
||||
// NOTE: all callbacks must run in order.
|
||||
x0_typescript: TypeScript,
|
||||
x1_react: React,
|
||||
x2_decorators: Decorators,
|
||||
x0_typescript: TypeScript<'a>,
|
||||
x1_react: React<'a>,
|
||||
x2_decorators: Decorators<'a>,
|
||||
}
|
||||
|
||||
impl<'a> Transformer<'a> {
|
||||
|
|
@ -72,14 +70,12 @@ impl<'a> Transformer<'a> {
|
|||
semantic: Semantic<'a>,
|
||||
options: TransformOptions,
|
||||
) -> Self {
|
||||
let ctx = Rc::new(TransformCtx::new(allocator, source_type, semantic));
|
||||
Self {
|
||||
allocator,
|
||||
source_type,
|
||||
semantic,
|
||||
options,
|
||||
x0_typescript: TypeScript::default(),
|
||||
x1_react: React::default(),
|
||||
x2_decorators: Decorators::default(),
|
||||
ctx: Rc::clone(&ctx),
|
||||
x0_typescript: TypeScript::new(options.typescript, &ctx),
|
||||
x1_react: React::new(options.react, &ctx),
|
||||
x2_decorators: Decorators::new(options.decorators, &ctx),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -88,7 +84,12 @@ impl<'a> Transformer<'a> {
|
|||
/// Returns `Vec<Error>` if any errors were collected during the transformation.
|
||||
pub fn build(mut self, program: &mut Program<'a>) -> Result<(), Vec<Error>> {
|
||||
self.visit_program(program);
|
||||
Ok(())
|
||||
let errors = self.ctx.take_errors();
|
||||
if errors.is_empty() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(errors)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,8 +1,6 @@
|
|||
use serde::Deserialize;
|
||||
use std::rc::Rc;
|
||||
|
||||
#[derive(Debug, Default, Clone, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ReactDisplayNameOptions;
|
||||
use crate::context::Ctx;
|
||||
|
||||
/// [plugin-transform-react-display-name](https://babeljs.io/docs/babel-plugin-transform-react-display-name)
|
||||
///
|
||||
|
|
@ -12,14 +10,13 @@ pub struct ReactDisplayNameOptions;
|
|||
///
|
||||
/// In: `var bar = createReactClass({});`
|
||||
/// Out: `var bar = createReactClass({ displayName: "bar" });`
|
||||
#[derive(Debug, Default)]
|
||||
pub struct ReactDisplayName {
|
||||
#[allow(unused)]
|
||||
options: ReactDisplayNameOptions,
|
||||
#[allow(unused)]
|
||||
pub struct ReactDisplayName<'a> {
|
||||
ctx: Ctx<'a>,
|
||||
}
|
||||
|
||||
impl ReactDisplayName {
|
||||
pub fn new(options: ReactDisplayNameOptions) -> Self {
|
||||
Self { options }
|
||||
impl<'a> ReactDisplayName<'a> {
|
||||
pub fn new(ctx: &Ctx<'a>) -> Self {
|
||||
Self { ctx: Rc::clone(ctx) }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,7 @@
|
|||
use std::rc::Rc;
|
||||
|
||||
use crate::context::Ctx;
|
||||
|
||||
pub use super::options::ReactOptions;
|
||||
|
||||
/// [plugin-transform-react-jsx](https://babeljs.io/docs/babel-plugin-transform-react-jsx)
|
||||
|
|
@ -13,14 +17,14 @@ pub use super::options::ReactOptions;
|
|||
///
|
||||
/// * <https://babeljs.io/docs/babel-plugin-transform-react-jsx>
|
||||
/// * <https://github.com/babel/babel/tree/main/packages/babel-helper-builder-react-jsx>
|
||||
#[derive(Debug, Default)]
|
||||
pub struct ReactJsx {
|
||||
#[allow(unused)]
|
||||
#[allow(unused)]
|
||||
pub struct ReactJsx<'a> {
|
||||
options: ReactOptions,
|
||||
ctx: Ctx<'a>,
|
||||
}
|
||||
|
||||
impl ReactJsx {
|
||||
pub fn new(options: ReactOptions) -> Self {
|
||||
Self { options }
|
||||
impl<'a> ReactJsx<'a> {
|
||||
pub fn new(options: ReactOptions, ctx: &Ctx<'a>) -> Self {
|
||||
Self { options, ctx: Rc::clone(ctx) }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,6 @@
|
|||
use serde::Deserialize;
|
||||
use std::rc::Rc;
|
||||
|
||||
#[derive(Debug, Default, Clone, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ReactJsxSelfOptions;
|
||||
use crate::context::Ctx;
|
||||
|
||||
/// [plugin-transform-react-jsx-self](https://babeljs.io/docs/babel-plugin-transform-react-jsx-self)
|
||||
///
|
||||
|
|
@ -12,14 +10,13 @@ pub struct ReactJsxSelfOptions;
|
|||
///
|
||||
/// In: `<sometag />`
|
||||
/// Out: `<sometag __self={this} />`
|
||||
#[derive(Debug, Default)]
|
||||
pub struct ReactJsxSelf {
|
||||
#[allow(unused)]
|
||||
options: ReactJsxSelfOptions,
|
||||
#[allow(unused)]
|
||||
pub struct ReactJsxSelf<'a> {
|
||||
ctx: Ctx<'a>,
|
||||
}
|
||||
|
||||
impl ReactJsxSelf {
|
||||
pub fn new(options: ReactJsxSelfOptions) -> Self {
|
||||
Self { options }
|
||||
impl<'a> ReactJsxSelf<'a> {
|
||||
pub fn new(ctx: &Ctx<'a>) -> Self {
|
||||
Self { ctx: Rc::clone(ctx) }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,18 +1,15 @@
|
|||
use serde::Deserialize;
|
||||
use std::rc::Rc;
|
||||
|
||||
#[derive(Debug, Default, Clone, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ReactJsxSourceOptions;
|
||||
use crate::context::Ctx;
|
||||
|
||||
/// [plugin-transform-react-jsx-source](https://babeljs.io/docs/babel-plugin-transform-react-jsx-source)
|
||||
#[derive(Debug, Default)]
|
||||
pub struct ReactJsxSource {
|
||||
#[allow(unused)]
|
||||
options: ReactJsxSourceOptions,
|
||||
#[allow(unused)]
|
||||
pub struct ReactJsxSource<'a> {
|
||||
ctx: Ctx<'a>,
|
||||
}
|
||||
|
||||
impl ReactJsxSource {
|
||||
pub fn new(options: ReactJsxSourceOptions) -> Self {
|
||||
Self { options }
|
||||
impl<'a> ReactJsxSource<'a> {
|
||||
pub fn new(ctx: &Ctx<'a>) -> Self {
|
||||
Self { ctx: Rc::clone(ctx) }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,14 +4,15 @@ mod jsx_self;
|
|||
mod jsx_source;
|
||||
mod options;
|
||||
|
||||
use std::rc::Rc;
|
||||
|
||||
use oxc_ast::ast::*;
|
||||
|
||||
use crate::context::Ctx;
|
||||
|
||||
pub use self::{
|
||||
display_name::{ReactDisplayName, ReactDisplayNameOptions},
|
||||
jsx::ReactJsx,
|
||||
jsx_self::{ReactJsxSelf, ReactJsxSelfOptions},
|
||||
jsx_source::{ReactJsxSource, ReactJsxSourceOptions},
|
||||
options::ReactOptions,
|
||||
display_name::ReactDisplayName, jsx::ReactJsx, jsx_self::ReactJsxSelf,
|
||||
jsx_source::ReactJsxSource, options::ReactOptions,
|
||||
};
|
||||
|
||||
/// [Preset React](https://babel.dev/docs/babel-preset-react)
|
||||
|
|
@ -22,40 +23,31 @@ pub use self::{
|
|||
/// * [plugin-transform-react-jsx-self](https://babeljs.io/docs/babel-plugin-transform-react-jsx-self)
|
||||
/// * [plugin-transform-react-jsx](https://babeljs.io/docs/babel-plugin-transform-react-jsx)
|
||||
/// * [plugin-transform-react-display-name](https://babeljs.io/docs/babel-plugin-transform-react-display-name)
|
||||
#[derive(Default)]
|
||||
pub struct React {
|
||||
jsx: ReactJsx,
|
||||
jsx_self: ReactJsxSelf,
|
||||
jsx_source: ReactJsxSource,
|
||||
display_name: ReactDisplayName,
|
||||
#[allow(unused)]
|
||||
pub struct React<'a> {
|
||||
ctx: Ctx<'a>,
|
||||
jsx: ReactJsx<'a>,
|
||||
jsx_self: ReactJsxSelf<'a>,
|
||||
jsx_source: ReactJsxSource<'a>,
|
||||
display_name: ReactDisplayName<'a>,
|
||||
}
|
||||
|
||||
// Constructors
|
||||
impl React {
|
||||
pub fn new(&mut self, options: ReactOptions) -> &mut Self {
|
||||
self.jsx = ReactJsx::new(options);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_jsx_self(&mut self, options: ReactJsxSelfOptions) -> &mut Self {
|
||||
self.jsx_self = ReactJsxSelf::new(options);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_jsx_source(&mut self, options: ReactJsxSourceOptions) -> &mut Self {
|
||||
self.jsx_source = ReactJsxSource::new(options);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_display_name(&mut self, options: ReactDisplayNameOptions) -> &mut Self {
|
||||
self.display_name = ReactDisplayName::new(options);
|
||||
self
|
||||
impl<'a> React<'a> {
|
||||
pub fn new(options: ReactOptions, ctx: &Ctx<'a>) -> Self {
|
||||
Self {
|
||||
ctx: Rc::clone(ctx),
|
||||
jsx: ReactJsx::new(options, ctx),
|
||||
jsx_self: ReactJsxSelf::new(ctx),
|
||||
jsx_source: ReactJsxSource::new(ctx),
|
||||
display_name: ReactDisplayName::new(ctx),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Transformers
|
||||
impl React {
|
||||
pub fn transform_expression(&mut self, expr: &mut Expression<'_>) {
|
||||
impl<'a> React<'a> {
|
||||
pub fn transform_expression(&mut self, expr: &mut Expression<'a>) {
|
||||
match expr {
|
||||
Expression::JSXElement(_e) => {
|
||||
// *expr = unimplemented!();
|
||||
|
|
|
|||
|
|
@ -1,7 +1,11 @@
|
|||
use std::rc::Rc;
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
use oxc_ast::ast::*;
|
||||
|
||||
use crate::context::Ctx;
|
||||
|
||||
#[derive(Debug, Default, Clone, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct TypeScriptOptions;
|
||||
|
|
@ -27,19 +31,19 @@ pub struct TypeScriptOptions;
|
|||
///
|
||||
/// In: `const x: number = 0;`
|
||||
/// Out: `const x = 0;`
|
||||
#[derive(Debug, Default)]
|
||||
pub struct TypeScript {
|
||||
#[allow(unused)]
|
||||
#[allow(unused)]
|
||||
pub struct TypeScript<'a> {
|
||||
options: TypeScriptOptions,
|
||||
ctx: Ctx<'a>,
|
||||
}
|
||||
|
||||
impl TypeScript {
|
||||
pub fn new(options: TypeScriptOptions) -> Self {
|
||||
Self { options }
|
||||
impl<'a> TypeScript<'a> {
|
||||
pub fn new(options: TypeScriptOptions, ctx: &Ctx<'a>) -> Self {
|
||||
Self { options, ctx: Rc::clone(ctx) }
|
||||
}
|
||||
}
|
||||
|
||||
// Transformers
|
||||
impl TypeScript {
|
||||
pub fn transform_statement(&mut self, _stmt: &mut Statement<'_>) {}
|
||||
impl<'a> TypeScript<'a> {
|
||||
pub fn transform_statement(&mut self, _stmt: &mut Statement<'a>) {}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue