feat(transformer/decorators): transform getter function (#2473)

This commit is contained in:
Dunqing 2024-02-23 10:11:45 +08:00 committed by GitHub
parent 6527bfd249
commit 2628c97eda
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 41 additions and 29 deletions

View file

@ -117,7 +117,7 @@ impl<'a> Decorators<'a> {
Atom::from(format!("_{name}{}", if *uid == 1 { String::new() } else { uid.to_string() })) Atom::from(format!("_{name}{}", if *uid == 1 { String::new() } else { uid.to_string() }))
} }
pub fn get_call_with_this(&self, name: Atom) -> Statement<'a> { pub fn get_call_with_this(&self, name: Atom) -> Expression<'a> {
self.get_call_with_arguments( self.get_call_with_arguments(
name, name,
self.ast.new_vec_single(Argument::Expression(self.ast.this_expression(SPAN))), self.ast.new_vec_single(Argument::Expression(self.ast.this_expression(SPAN))),
@ -128,16 +128,13 @@ impl<'a> Decorators<'a> {
&self, &self,
name: Atom, name: Atom,
arguments: Vec<'a, Argument<'a>>, arguments: Vec<'a, Argument<'a>>,
) -> Statement<'a> { ) -> Expression<'a> {
self.ast.expression_statement( self.ast.call_expression(
SPAN, SPAN,
self.ast.call_expression( self.ast.identifier_reference_expression(IdentifierReference::new(SPAN, name)),
SPAN, arguments,
self.ast.identifier_reference_expression(IdentifierReference::new(SPAN, name)), false,
arguments, None,
false,
None,
),
) )
} }
@ -429,6 +426,9 @@ impl<'a> Decorators<'a> {
"v".into(), "v".into(),
)), )),
)); ));
let is_setter = def.kind == MethodDefinitionKind::Set;
def.value = self.ast.function( def.value = self.ast.function(
def.value.r#type, def.value.r#type,
def.value.span, def.value.span,
@ -439,27 +439,39 @@ impl<'a> Decorators<'a> {
self.ast.formal_parameters( self.ast.formal_parameters(
SPAN, SPAN,
FormalParameterKind::FormalParameter, FormalParameterKind::FormalParameter,
self.ast.new_vec_single(self.ast.formal_parameter( if is_setter {
SPAN, self.ast.new_vec_single(self.ast.formal_parameter(
self.ast.binding_pattern( SPAN,
self.ast.binding_pattern_identifier( self.ast.binding_pattern(
BindingIdentifier::new(SPAN, "v".into()), self.ast.binding_pattern_identifier(
BindingIdentifier::new(SPAN, "v".into()),
),
None,
false,
), ),
None, None,
false, false,
), self.ast.new_vec(),
None, ))
false, } else {
self.ast.new_vec(), self.ast.new_vec()
)), },
None, None,
), ),
Some(self.ast.function_body( Some(self.ast.function_body(
SPAN, SPAN,
self.ast.new_vec(), self.ast.new_vec(),
self.ast.new_vec_single( self.ast.new_vec_single(if is_setter {
self.get_call_with_arguments(name.clone(), arguments), self.ast.expression_statement(
), SPAN,
self.get_call_with_arguments(name.clone(), arguments),
)
} else {
self.ast.return_statement(
SPAN,
Some(self.get_call_with_this(name.clone())),
)
}),
)), )),
self.ast.copy(&def.value.type_parameters), self.ast.copy(&def.value.type_parameters),
self.ast.copy(&def.value.return_type), self.ast.copy(&def.value.return_type),
@ -527,7 +539,7 @@ impl<'a> Decorators<'a> {
) { ) {
if let ClassElement::MethodDefinition(def) = constructor_element { if let ClassElement::MethodDefinition(def) = constructor_element {
if let Some(body) = &mut def.value.body { if let Some(body) = &mut def.value.body {
body.statements.insert(0, self.get_call_with_this(name)); body.statements.insert(0, self.ast.expression_statement(SPAN, self.get_call_with_this(name)));
} }
} else { } else {
unreachable!(); unreachable!();
@ -554,7 +566,9 @@ impl<'a> Decorators<'a> {
Some(self.ast.function_body( Some(self.ast.function_body(
SPAN, SPAN,
self.ast.new_vec(), self.ast.new_vec(),
self.ast.new_vec_single(self.get_call_with_this(name)), self.ast.new_vec_single(
self.ast.expression_statement(SPAN, self.get_call_with_this(name))
),
)), )),
None, None,
None, None,

View file

@ -1,4 +1,4 @@
Passed: 342/1369 Passed: 344/1369
# All Passed: # All Passed:
* babel-plugin-transform-numeric-separator * babel-plugin-transform-numeric-separator
@ -911,7 +911,7 @@ Passed: 342/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 (18/190) # babel-plugin-proposal-decorators (20/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
@ -1028,14 +1028,12 @@ Passed: 342/1369
* 2023-05-fields--to-es2015/static-public/input.js * 2023-05-fields--to-es2015/static-public/input.js
* 2023-05-getters/context-name/input.js * 2023-05-getters/context-name/input.js
* 2023-05-getters/private/input.js * 2023-05-getters/private/input.js
* 2023-05-getters/static-private/input.js
* 2023-05-getters--to-es2015/context-name/input.js * 2023-05-getters--to-es2015/context-name/input.js
* 2023-05-getters--to-es2015/private/input.js * 2023-05-getters--to-es2015/private/input.js
* 2023-05-getters--to-es2015/public/input.js * 2023-05-getters--to-es2015/public/input.js
* 2023-05-getters--to-es2015/static-private/input.js * 2023-05-getters--to-es2015/static-private/input.js
* 2023-05-getters--to-es2015/static-public/input.js * 2023-05-getters--to-es2015/static-public/input.js
* 2023-05-getters-and-setters/private/input.js * 2023-05-getters-and-setters/private/input.js
* 2023-05-getters-and-setters/static-private/input.js
* 2023-05-getters-and-setters--to-es2015/private/input.js * 2023-05-getters-and-setters--to-es2015/private/input.js
* 2023-05-getters-and-setters--to-es2015/public/input.js * 2023-05-getters-and-setters--to-es2015/public/input.js
* 2023-05-getters-and-setters--to-es2015/static-private/input.js * 2023-05-getters-and-setters--to-es2015/static-private/input.js