mirror of
https://github.com/danbulant/oxc
synced 2026-05-25 04:42:10 +00:00
feat(transformer/decorators): support transform the class decorators in export declaration (#2145)
This commit is contained in:
parent
8e332b20a1
commit
04b401cbfd
4 changed files with 134 additions and 52 deletions
|
|
@ -2162,6 +2162,12 @@ pub struct ExportSpecifier {
|
||||||
pub export_kind: ImportOrExportKind, // `export type *`
|
pub export_kind: ImportOrExportKind, // `export type *`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ExportSpecifier {
|
||||||
|
pub fn new(span: Span, local: ModuleExportName, exported: ModuleExportName) -> Self {
|
||||||
|
Self { span, local, exported, export_kind: ImportOrExportKind::Value }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Hash)]
|
#[derive(Debug, Hash)]
|
||||||
#[cfg_attr(feature = "serde", derive(Serialize), serde(untagged))]
|
#[cfg_attr(feature = "serde", derive(Serialize), serde(untagged))]
|
||||||
pub enum ExportDefaultDeclarationKind<'a> {
|
pub enum ExportDefaultDeclarationKind<'a> {
|
||||||
|
|
|
||||||
|
|
@ -201,6 +201,7 @@ impl<'a> VisitMut<'a> for Transformer<'a> {
|
||||||
|
|
||||||
fn visit_statement(&mut self, stmt: &mut Statement<'a>) {
|
fn visit_statement(&mut self, stmt: &mut Statement<'a>) {
|
||||||
self.typescript.as_mut().map(|t| t.transform_statement(stmt));
|
self.typescript.as_mut().map(|t| t.transform_statement(stmt));
|
||||||
|
self.decorators.as_mut().map(|t| t.transform_statement(stmt));
|
||||||
self.visit_statement_match(stmt);
|
self.visit_statement_match(stmt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use oxc_allocator::Box;
|
use oxc_allocator::{Box, Vec};
|
||||||
use oxc_ast::{ast::*, AstBuilder};
|
use oxc_ast::{ast::*, AstBuilder};
|
||||||
use oxc_span::{Atom, SPAN};
|
use oxc_span::{Atom, SPAN};
|
||||||
use oxc_syntax::operator::{AssignmentOperator, LogicalOperator};
|
use oxc_syntax::operator::{AssignmentOperator, LogicalOperator};
|
||||||
|
|
@ -19,7 +19,10 @@ pub struct Decorators<'a> {
|
||||||
_ctx: TransformerCtx<'a>,
|
_ctx: TransformerCtx<'a>,
|
||||||
options: DecoratorsOptions,
|
options: DecoratorsOptions,
|
||||||
// Insert to the top of the program
|
// Insert to the top of the program
|
||||||
top_statements: Vec<Statement<'a>>,
|
top_statements: Vec<'a, Statement<'a>>,
|
||||||
|
// Insert to the bottom of the program
|
||||||
|
bottom_statements: Vec<'a, Statement<'a>>,
|
||||||
|
class_name_uid: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, Default, Deserialize)]
|
#[derive(Debug, Clone, Copy, Default, Deserialize)]
|
||||||
|
|
@ -48,23 +51,87 @@ impl<'a> Decorators<'a> {
|
||||||
ctx: TransformerCtx<'a>,
|
ctx: TransformerCtx<'a>,
|
||||||
options: &TransformOptions,
|
options: &TransformOptions,
|
||||||
) -> Option<Self> {
|
) -> Option<Self> {
|
||||||
|
let top_statements = ast.new_vec();
|
||||||
|
let bottom_statements = ast.new_vec();
|
||||||
options.decorators.map(|options| Self {
|
options.decorators.map(|options| Self {
|
||||||
ast,
|
ast,
|
||||||
_ctx: ctx,
|
_ctx: ctx,
|
||||||
options,
|
options,
|
||||||
top_statements: Vec::new(),
|
top_statements,
|
||||||
|
bottom_statements,
|
||||||
|
class_name_uid: 0,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_class_name(&mut self) -> Atom {
|
||||||
|
self.class_name_uid += 1;
|
||||||
|
if self.class_name_uid == 1 {
|
||||||
|
return "_class".into();
|
||||||
|
}
|
||||||
|
Atom::from(format!("_class{}", self.class_name_uid))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn transform_program(&mut self, program: &mut Program<'a>) {
|
pub fn transform_program(&mut self, program: &mut Program<'a>) {
|
||||||
program.body.splice(0..0, self.top_statements.drain(..));
|
program.body.splice(0..0, self.top_statements.drain(..));
|
||||||
|
program.body.append(&mut self.bottom_statements);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn transform_statement(&mut self, stmt: &mut Statement<'a>) {
|
||||||
|
if let Statement::ModuleDeclaration(decl) = stmt {
|
||||||
|
match &mut **decl {
|
||||||
|
ModuleDeclaration::ExportNamedDeclaration(export) => {
|
||||||
|
export.declaration.as_mut().map_or_else(
|
||||||
|
|| (),
|
||||||
|
|declaration| {
|
||||||
|
self.transform_declaration(declaration);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
ModuleDeclaration::ExportDefaultDeclaration(export) => {
|
||||||
|
if let ExportDefaultDeclarationKind::ClassDeclaration(class) =
|
||||||
|
&mut export.declaration
|
||||||
|
{
|
||||||
|
let class_name = class
|
||||||
|
.id
|
||||||
|
.clone()
|
||||||
|
.map(|id| id.name)
|
||||||
|
.or_else(|| Some(self.get_class_name()));
|
||||||
|
|
||||||
|
*stmt = Statement::Declaration(
|
||||||
|
self.transform_class_legacy(class, class_name.clone()),
|
||||||
|
);
|
||||||
|
self.bottom_statements.push(self.ast.module_declaration(
|
||||||
|
ModuleDeclaration::ExportNamedDeclaration(
|
||||||
|
self.ast.export_named_declaration(
|
||||||
|
SPAN,
|
||||||
|
None,
|
||||||
|
self.ast.new_vec_single(ExportSpecifier::new(
|
||||||
|
SPAN,
|
||||||
|
ModuleExportName::Identifier(IdentifierName::new(
|
||||||
|
SPAN,
|
||||||
|
class_name.unwrap(),
|
||||||
|
)),
|
||||||
|
ModuleExportName::Identifier(IdentifierName::new(
|
||||||
|
SPAN,
|
||||||
|
Atom::from("default"),
|
||||||
|
)),
|
||||||
|
)),
|
||||||
|
None,
|
||||||
|
ImportOrExportKind::Value,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
pub fn transform_declaration(&mut self, decl: &mut Declaration<'a>) {
|
pub fn transform_declaration(&mut self, decl: &mut Declaration<'a>) {
|
||||||
let new_decl = match decl {
|
let new_decl = match decl {
|
||||||
Declaration::ClassDeclaration(class) => {
|
Declaration::ClassDeclaration(class) => {
|
||||||
if self.options.version.is_legacy() {
|
if self.options.version.is_legacy() {
|
||||||
Some(self.transform_class_legacy(class))
|
Some(self.transform_class_legacy(class, None))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
@ -78,63 +145,72 @@ impl<'a> Decorators<'a> {
|
||||||
|
|
||||||
pub fn transform_class_legacy(
|
pub fn transform_class_legacy(
|
||||||
&mut self,
|
&mut self,
|
||||||
class: &mut Box<'a, oxc_ast::ast::Class<'a>>,
|
class: &mut Box<'a, Class<'a>>,
|
||||||
|
class_name: Option<Atom>,
|
||||||
) -> Declaration<'a> {
|
) -> Declaration<'a> {
|
||||||
let class_identifier_name: Atom = "_class".into();
|
let class_binding_identifier = &class.id.clone().unwrap_or_else(|| {
|
||||||
let class_identifier = IdentifierReference::new(SPAN, class_identifier_name.clone());
|
BindingIdentifier::new(SPAN, class_name.unwrap_or_else(|| self.get_class_name()))
|
||||||
|
});
|
||||||
|
let class_name = BindingPattern::new_with_kind(
|
||||||
|
self.ast.binding_pattern_identifier(self.ast.copy(class_binding_identifier)),
|
||||||
|
);
|
||||||
|
|
||||||
let decl = self.ast.variable_declaration(
|
let init = {
|
||||||
SPAN,
|
let class_identifier_name: Atom = self.get_class_name();
|
||||||
VariableDeclarationKind::Var,
|
let class_identifier = IdentifierReference::new(SPAN, class_identifier_name.clone());
|
||||||
self.ast.new_vec_single(self.ast.variable_declarator(
|
|
||||||
|
let decl = self.ast.variable_declaration(
|
||||||
SPAN,
|
SPAN,
|
||||||
VariableDeclarationKind::Var,
|
VariableDeclarationKind::Var,
|
||||||
BindingPattern::new_with_kind(self.ast.binding_pattern_identifier(
|
self.ast.new_vec_single(self.ast.variable_declarator(
|
||||||
BindingIdentifier::new(SPAN, class_identifier_name),
|
|
||||||
)),
|
|
||||||
None,
|
|
||||||
false,
|
|
||||||
)),
|
|
||||||
Modifiers::empty(),
|
|
||||||
);
|
|
||||||
self.top_statements.push(Statement::Declaration(Declaration::VariableDeclaration(decl)));
|
|
||||||
|
|
||||||
let left = AssignmentTarget::SimpleAssignmentTarget(
|
|
||||||
self.ast.simple_assignment_target_identifier(class_identifier.clone()),
|
|
||||||
);
|
|
||||||
let right = self.ast.class_expression(self.ast.copy(class));
|
|
||||||
let new_expr =
|
|
||||||
self.ast.assignment_expression(SPAN, AssignmentOperator::Assign, left, right);
|
|
||||||
|
|
||||||
let new_expr = class.decorators.drain(..).fold(new_expr, |new_expr, decorator| {
|
|
||||||
match &decorator.expression {
|
|
||||||
Expression::Identifier(identifier) => self.ast.call_expression(
|
|
||||||
SPAN,
|
SPAN,
|
||||||
self.ast.identifier_reference_expression(IdentifierReference::new(
|
VariableDeclarationKind::Var,
|
||||||
SPAN,
|
BindingPattern::new_with_kind(self.ast.binding_pattern_identifier(
|
||||||
identifier.name.clone(),
|
BindingIdentifier::new(SPAN, class_identifier_name),
|
||||||
)),
|
)),
|
||||||
self.ast.new_vec_single(Argument::Expression(self.ast.copy(&new_expr))),
|
|
||||||
false,
|
|
||||||
None,
|
None,
|
||||||
),
|
false,
|
||||||
_ => new_expr,
|
)),
|
||||||
}
|
Modifiers::empty(),
|
||||||
});
|
);
|
||||||
|
self.top_statements
|
||||||
|
.push(Statement::Declaration(Declaration::VariableDeclaration(decl)));
|
||||||
|
|
||||||
let init = self.ast.logical_expression(
|
let left = AssignmentTarget::SimpleAssignmentTarget(
|
||||||
SPAN,
|
self.ast.simple_assignment_target_identifier(class_identifier.clone()),
|
||||||
new_expr,
|
);
|
||||||
LogicalOperator::Or,
|
let right = self.ast.class_expression(self.ast.copy(class));
|
||||||
self.ast.identifier_reference_expression(class_identifier),
|
let new_expr =
|
||||||
);
|
self.ast.assignment_expression(SPAN, AssignmentOperator::Assign, left, right);
|
||||||
|
|
||||||
|
let new_expr = class.decorators.drain(..).fold(new_expr, |new_expr, decorator| {
|
||||||
|
match &decorator.expression {
|
||||||
|
Expression::Identifier(identifier) => self.ast.call_expression(
|
||||||
|
SPAN,
|
||||||
|
self.ast.identifier_reference_expression(IdentifierReference::new(
|
||||||
|
SPAN,
|
||||||
|
identifier.name.clone(),
|
||||||
|
)),
|
||||||
|
self.ast.new_vec_single(Argument::Expression(self.ast.copy(&new_expr))),
|
||||||
|
false,
|
||||||
|
None,
|
||||||
|
),
|
||||||
|
_ => new_expr,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
self.ast.logical_expression(
|
||||||
|
SPAN,
|
||||||
|
new_expr,
|
||||||
|
LogicalOperator::Or,
|
||||||
|
self.ast.identifier_reference_expression(class_identifier),
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
let declarator = self.ast.variable_declarator(
|
let declarator = self.ast.variable_declarator(
|
||||||
SPAN,
|
SPAN,
|
||||||
VariableDeclarationKind::Let,
|
VariableDeclarationKind::Let,
|
||||||
BindingPattern::new_with_kind(
|
class_name,
|
||||||
self.ast.binding_pattern_identifier(self.ast.copy(&class.id.clone().unwrap())),
|
|
||||||
),
|
|
||||||
Some(init),
|
Some(init),
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
Passed: 325/1369
|
Passed: 326/1369
|
||||||
|
|
||||||
# All Passed:
|
# All Passed:
|
||||||
* babel-plugin-transform-numeric-separator
|
* babel-plugin-transform-numeric-separator
|
||||||
|
|
@ -911,7 +911,7 @@ Passed: 325/1369
|
||||||
* spread-transform/transform-to-babel-extend/input.js
|
* spread-transform/transform-to-babel-extend/input.js
|
||||||
* spread-transform/transform-to-object-assign/input.js
|
* spread-transform/transform-to-object-assign/input.js
|
||||||
|
|
||||||
# babel-plugin-proposal-decorators (1/190)
|
# babel-plugin-proposal-decorators (2/190)
|
||||||
* 2018-09-transformation/async-generator-method/input.js
|
* 2018-09-transformation/async-generator-method/input.js
|
||||||
* 2018-09-transformation/class-decorators-yield-await/input.js
|
* 2018-09-transformation/class-decorators-yield-await/input.js
|
||||||
* 2021-12-accessors/context-name/input.js
|
* 2021-12-accessors/context-name/input.js
|
||||||
|
|
@ -1097,7 +1097,6 @@ Passed: 325/1369
|
||||||
* legacy-decl-to-expression/class-decorators/input.mjs
|
* legacy-decl-to-expression/class-decorators/input.mjs
|
||||||
* legacy-decl-to-expression/method-decorators/input.mjs
|
* legacy-decl-to-expression/method-decorators/input.mjs
|
||||||
* legacy-decl-to-expression/no-decorators/input.mjs
|
* legacy-decl-to-expression/no-decorators/input.mjs
|
||||||
* legacy-regression/10264/input.mjs
|
|
||||||
* legacy-regression/7030/input.js
|
* legacy-regression/7030/input.js
|
||||||
* legacy-regression/8041/input.mjs
|
* legacy-regression/8041/input.mjs
|
||||||
* legacy-regression/8559/input.mjs
|
* legacy-regression/8559/input.mjs
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue