feat(transformer): add proposal-decorators (#2868)

This commit is contained in:
Boshen 2024-03-30 21:07:36 +08:00 committed by GitHub
parent ffadcb08d9
commit 7034bcc47d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 34 additions and 4 deletions

View file

@ -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 }
}
}

View file

@ -6,6 +6,7 @@
//! * <https://github.com/microsoft/TypeScript/blob/main/src/compiler/transformer.ts> //! * <https://github.com/microsoft/TypeScript/blob/main/src/compiler/transformer.ts>
// Plugins: <https://babeljs.io/docs/plugins-list> // Plugins: <https://babeljs.io/docs/plugins-list>
mod decorators;
mod react_display_name; mod react_display_name;
mod react_jsx; mod react_jsx;
mod react_jsx_self; mod react_jsx_self;
@ -19,6 +20,7 @@ use oxc_semantic::Semantic;
use oxc_span::SourceType; use oxc_span::SourceType;
pub use crate::{ pub use crate::{
decorators::{Decorators, DecoratorsOptions},
react_display_name::{ReactDisplayName, ReactDisplayNameOptions}, react_display_name::{ReactDisplayName, ReactDisplayNameOptions},
react_jsx::{ReactJsx, ReactJsxOptions}, react_jsx::{ReactJsx, ReactJsxOptions},
react_jsx_self::{ReactJsxSelf, ReactJsxSelfOptions}, react_jsx_self::{ReactJsxSelf, ReactJsxSelfOptions},
@ -29,6 +31,7 @@ pub use crate::{
#[allow(unused)] #[allow(unused)]
#[derive(Debug, Default, Clone)] #[derive(Debug, Default, Clone)]
pub struct TransformOptions { pub struct TransformOptions {
pub decorators: DecoratorsOptions,
pub typescript: TypeScriptOptions, pub typescript: TypeScriptOptions,
pub react_jsx: ReactJsxOptions, pub react_jsx: ReactJsxOptions,
pub react_display_name: ReactDisplayNameOptions, pub react_display_name: ReactDisplayNameOptions,
@ -43,6 +46,8 @@ pub struct Transformer<'a> {
semantic: Semantic<'a>, semantic: Semantic<'a>,
options: TransformOptions, options: TransformOptions,
// Stage 3
decorators: Decorators,
// [preset-typescript](https://babeljs.io/docs/babel-preset-typescript) // [preset-typescript](https://babeljs.io/docs/babel-preset-typescript)
typescript: TypeScript, typescript: TypeScript,
// [preset-react](https://babeljs.io/docs/babel-preset-react) // [preset-react](https://babeljs.io/docs/babel-preset-react)
@ -64,6 +69,7 @@ impl<'a> Transformer<'a> {
source_type, source_type,
semantic, semantic,
options, options,
decorators: Decorators::default(),
typescript: TypeScript::default(), typescript: TypeScript::default(),
react_display_name: ReactDisplayName::default(), react_display_name: ReactDisplayName::default(),
react_jsx: ReactJsx::default(), react_jsx: ReactJsx::default(),

View file

@ -3,6 +3,9 @@ use std::{
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use serde::de::DeserializeOwned;
use serde_json::Value;
use oxc_allocator::Allocator; use oxc_allocator::Allocator;
use oxc_codegen::{Codegen, CodegenOptions}; use oxc_codegen::{Codegen, CodegenOptions};
use oxc_diagnostics::Error; use oxc_diagnostics::Error;
@ -11,11 +14,9 @@ use oxc_semantic::SemanticBuilder;
use oxc_span::{SourceType, VALID_EXTENSIONS}; use oxc_span::{SourceType, VALID_EXTENSIONS};
use oxc_tasks_common::{normalize_path, print_diff_in_terminal, BabelOptions}; use oxc_tasks_common::{normalize_path, print_diff_in_terminal, BabelOptions};
use oxc_transformer::{ use oxc_transformer::{
ReactDisplayNameOptions, ReactJsxOptions, ReactJsxSelfOptions, ReactJsxSourceOptions, DecoratorsOptions, ReactDisplayNameOptions, ReactJsxOptions, ReactJsxSelfOptions,
TransformOptions, Transformer, TypeScriptOptions, ReactJsxSourceOptions, TransformOptions, Transformer, TypeScriptOptions,
}; };
use serde::de::DeserializeOwned;
use serde_json::Value;
use crate::{fixture_root, root, TestRunnerEnv}; use crate::{fixture_root, root, TestRunnerEnv};
@ -86,6 +87,10 @@ pub trait TestCase {
} }
let options = self.options(); let options = self.options();
TransformOptions { TransformOptions {
decorators: options
.get_plugin("proposal-decorators")
.map(get_options::<DecoratorsOptions>)
.unwrap_or_default(),
typescript: options typescript: options
.get_plugin("transform-typescript") .get_plugin("transform-typescript")
.map(get_options::<TypeScriptOptions>) .map(get_options::<TypeScriptOptions>)