mirror of
https://github.com/danbulant/oxc
synced 2026-05-25 04:42:10 +00:00
feat(transformer/decorators): ensure property key consistency (#2233)
This commit is contained in:
parent
5279e8955f
commit
3b85e1813b
2 changed files with 50 additions and 50 deletions
|
|
@ -341,14 +341,28 @@ impl<'a> Decorators<'a> {
|
|||
if def.decorators.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some((index, def.key.name(), def.computed, &def.decorators))
|
||||
Some((
|
||||
index,
|
||||
def.key.name(),
|
||||
None,
|
||||
def.r#static,
|
||||
def.computed,
|
||||
&def.decorators,
|
||||
))
|
||||
}
|
||||
}
|
||||
ClassElement::PropertyDefinition(def) => {
|
||||
if def.decorators.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some((index, def.key.name(), def.computed, &def.decorators))
|
||||
Some((
|
||||
index,
|
||||
def.key.name(),
|
||||
self.ast.copy(&def.value),
|
||||
def.r#static,
|
||||
def.computed,
|
||||
&def.decorators,
|
||||
))
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
|
|
@ -358,38 +372,23 @@ impl<'a> Decorators<'a> {
|
|||
|
||||
let mut replace_elements = HashMap::new();
|
||||
|
||||
for (index, name, computed, decorators) in elements {
|
||||
for (index, name, value, r#static, computed, decorators) in elements {
|
||||
let init_name = self.get_unique_name(&if computed {
|
||||
"init_computedKey".into()
|
||||
} else {
|
||||
format!("init_{}", name.clone().unwrap()).into()
|
||||
});
|
||||
decorators.iter().for_each(|decorator| {
|
||||
let dec_name = self.get_unique_name(&"dec".into());
|
||||
declarations.push(self.get_variable_declarator(dec_name.clone()));
|
||||
|
||||
let left = self.ast.simple_assignment_target_identifier(
|
||||
IdentifierReference::new(SPAN, dec_name.clone()),
|
||||
);
|
||||
let right = self.ast.copy(&decorator.expression);
|
||||
let dec_expr = self.ast.assignment_expression(
|
||||
SPAN,
|
||||
AssignmentOperator::Assign,
|
||||
left,
|
||||
right,
|
||||
);
|
||||
e_elements.push(self.get_assignment_target_maybe_default(init_name.clone()));
|
||||
let mut decorator_elements = self.ast.new_vec_with_capacity(2);
|
||||
decorator_elements.push(ArrayExpressionElement::Expression(
|
||||
self.ast.identifier_reference_expression(IdentifierReference::new(
|
||||
SPAN, dec_name,
|
||||
)),
|
||||
self.ast.copy(&decorator.expression),
|
||||
));
|
||||
decorator_elements.push(ArrayExpressionElement::Expression(
|
||||
self.ast.literal_number_expression(NumberLiteral::new(
|
||||
SPAN,
|
||||
0f64,
|
||||
"0",
|
||||
if r#static { "8" } else { "0" },
|
||||
oxc_syntax::NumberBase::Decimal,
|
||||
)),
|
||||
));
|
||||
|
|
@ -401,42 +400,44 @@ impl<'a> Decorators<'a> {
|
|||
member_decorators_vec.push(ArrayExpressionElement::Expression(
|
||||
self.ast.array_expression(SPAN, decorator_elements, None),
|
||||
));
|
||||
|
||||
self.top_statements.push(self.ast.expression_statement(SPAN, dec_expr));
|
||||
});
|
||||
|
||||
declarations.push(self.get_variable_declarator(init_name.clone()));
|
||||
|
||||
if let Some(name) = name.clone() {
|
||||
replace_elements.insert(
|
||||
index,
|
||||
self.ast.class_property(
|
||||
SPAN,
|
||||
self.ast
|
||||
.property_key_identifier(IdentifierName::new(SPAN, name.clone())),
|
||||
Some(self.ast.call_expression(
|
||||
SPAN,
|
||||
self.ast.identifier_reference_expression(IdentifierReference::new(
|
||||
SPAN, init_name,
|
||||
)),
|
||||
self.ast.new_vec_single(Argument::Expression(
|
||||
self.ast.this_expression(SPAN),
|
||||
)),
|
||||
false,
|
||||
None,
|
||||
)),
|
||||
false,
|
||||
false,
|
||||
self.ast.new_vec(),
|
||||
),
|
||||
);
|
||||
let mut arguments =
|
||||
self.ast.new_vec_single(Argument::Expression(self.ast.this_expression(SPAN)));
|
||||
|
||||
if let Some(default_value) = value {
|
||||
arguments.push(Argument::Expression(default_value));
|
||||
}
|
||||
|
||||
replace_elements.insert(
|
||||
index,
|
||||
self.ast.call_expression(
|
||||
SPAN,
|
||||
self.ast.identifier_reference_expression(IdentifierReference::new(
|
||||
SPAN, init_name,
|
||||
)),
|
||||
arguments,
|
||||
false,
|
||||
None,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// replace the element with `name = init_name(this)`
|
||||
for (index, element) in class.body.body.iter_mut().enumerate() {
|
||||
if let Some(new_element) = replace_elements.remove(&index) {
|
||||
*element = new_element;
|
||||
match element {
|
||||
ClassElement::PropertyDefinition(definition) => {
|
||||
definition.decorators.clear();
|
||||
definition.value = Some(new_element);
|
||||
}
|
||||
ClassElement::MethodDefinition(definition) => {
|
||||
definition.decorators.clear();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
Passed: 330/1369
|
||||
Passed: 331/1369
|
||||
|
||||
# All Passed:
|
||||
* babel-plugin-transform-numeric-separator
|
||||
|
|
@ -911,7 +911,7 @@ Passed: 330/1369
|
|||
* spread-transform/transform-to-babel-extend/input.js
|
||||
* spread-transform/transform-to-object-assign/input.js
|
||||
|
||||
# babel-plugin-proposal-decorators (6/190)
|
||||
# babel-plugin-proposal-decorators (7/190)
|
||||
* 2018-09-transformation/async-generator-method/input.js
|
||||
* 2018-09-transformation/class-decorators-yield-await/input.js
|
||||
* 2021-12-accessors/context-name/input.js
|
||||
|
|
@ -1019,11 +1019,10 @@ Passed: 330/1369
|
|||
* 2023-05-duplicated-keys--to-es2015/method-and-field/input.js
|
||||
* 2023-05-duplicated-keys--to-es2015/methods-with-same-key/input.js
|
||||
* 2023-05-exported/default-named/input.mjs
|
||||
* 2023-05-exported/member-decorator/input.mjs
|
||||
* 2023-05-fields/context-name/input.js
|
||||
* 2023-05-fields/private/input.js
|
||||
* 2023-05-fields/public/input.js
|
||||
* 2023-05-fields/static-private/input.js
|
||||
* 2023-05-fields/static-public/input.js
|
||||
* 2023-05-fields--to-es2015/context-name/input.js
|
||||
* 2023-05-fields--to-es2015/private/input.js
|
||||
* 2023-05-fields--to-es2015/public/input.js
|
||||
|
|
|
|||
Loading…
Reference in a new issue