From 7034bcc47d38a64b6a8e4f4c661d608590310c56 Mon Sep 17 00:00:00 2001 From: Boshen Date: Sat, 30 Mar 2024 21:07:36 +0800 Subject: [PATCH] feat(transformer): add proposal-decorators (#2868) --- crates/oxc_transformer/src/decorators/mod.rs | 19 +++++++++++++++++++ crates/oxc_transformer/src/lib.rs | 6 ++++++ tasks/transform_conformance/src/test_case.rs | 13 +++++++++---- 3 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 crates/oxc_transformer/src/decorators/mod.rs diff --git a/crates/oxc_transformer/src/decorators/mod.rs b/crates/oxc_transformer/src/decorators/mod.rs new file mode 100644 index 000000000..8fbe511f7 --- /dev/null +++ b/crates/oxc_transformer/src/decorators/mod.rs @@ -0,0 +1,19 @@ +use serde::Deserialize; + +/// 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)] + options: DecoratorsOptions, +} + +impl Decorators { + pub fn new(options: DecoratorsOptions) -> Self { + Self { options } + } +} diff --git a/crates/oxc_transformer/src/lib.rs b/crates/oxc_transformer/src/lib.rs index 3401d9a89..e9735ae5c 100644 --- a/crates/oxc_transformer/src/lib.rs +++ b/crates/oxc_transformer/src/lib.rs @@ -6,6 +6,7 @@ //! * // Plugins: +mod decorators; mod react_display_name; mod react_jsx; mod react_jsx_self; @@ -19,6 +20,7 @@ use oxc_semantic::Semantic; use oxc_span::SourceType; pub use crate::{ + decorators::{Decorators, DecoratorsOptions}, react_display_name::{ReactDisplayName, ReactDisplayNameOptions}, react_jsx::{ReactJsx, ReactJsxOptions}, react_jsx_self::{ReactJsxSelf, ReactJsxSelfOptions}, @@ -29,6 +31,7 @@ pub use crate::{ #[allow(unused)] #[derive(Debug, Default, Clone)] pub struct TransformOptions { + pub decorators: DecoratorsOptions, pub typescript: TypeScriptOptions, pub react_jsx: ReactJsxOptions, pub react_display_name: ReactDisplayNameOptions, @@ -43,6 +46,8 @@ pub struct Transformer<'a> { semantic: Semantic<'a>, options: TransformOptions, + // Stage 3 + decorators: Decorators, // [preset-typescript](https://babeljs.io/docs/babel-preset-typescript) typescript: TypeScript, // [preset-react](https://babeljs.io/docs/babel-preset-react) @@ -64,6 +69,7 @@ impl<'a> Transformer<'a> { source_type, semantic, options, + decorators: Decorators::default(), typescript: TypeScript::default(), react_display_name: ReactDisplayName::default(), react_jsx: ReactJsx::default(), diff --git a/tasks/transform_conformance/src/test_case.rs b/tasks/transform_conformance/src/test_case.rs index 219d29f11..89b99217b 100644 --- a/tasks/transform_conformance/src/test_case.rs +++ b/tasks/transform_conformance/src/test_case.rs @@ -3,6 +3,9 @@ use std::{ path::{Path, PathBuf}, }; +use serde::de::DeserializeOwned; +use serde_json::Value; + use oxc_allocator::Allocator; use oxc_codegen::{Codegen, CodegenOptions}; use oxc_diagnostics::Error; @@ -11,11 +14,9 @@ use oxc_semantic::SemanticBuilder; use oxc_span::{SourceType, VALID_EXTENSIONS}; use oxc_tasks_common::{normalize_path, print_diff_in_terminal, BabelOptions}; use oxc_transformer::{ - ReactDisplayNameOptions, ReactJsxOptions, ReactJsxSelfOptions, ReactJsxSourceOptions, - TransformOptions, Transformer, TypeScriptOptions, + DecoratorsOptions, ReactDisplayNameOptions, ReactJsxOptions, ReactJsxSelfOptions, + ReactJsxSourceOptions, TransformOptions, Transformer, TypeScriptOptions, }; -use serde::de::DeserializeOwned; -use serde_json::Value; use crate::{fixture_root, root, TestRunnerEnv}; @@ -86,6 +87,10 @@ pub trait TestCase { } let options = self.options(); TransformOptions { + decorators: options + .get_plugin("proposal-decorators") + .map(get_options::) + .unwrap_or_default(), typescript: options .get_plugin("transform-typescript") .map(get_options::)