Skip to content

Commit

Permalink
feat(transformer-dts): remove type annotation from private field (#3689)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Jun 15, 2024
1 parent 0e6d3ce commit 35c382e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 15 deletions.
6 changes: 6 additions & 0 deletions crates/oxc_ast/src/ast/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,12 @@ pub enum TSAccessibility {
Public,
}

impl TSAccessibility {
pub fn is_private(&self) -> bool {
matches!(self, TSAccessibility::Private)
}
}

#[visited_node]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
Expand Down
76 changes: 61 additions & 15 deletions crates/oxc_transformer_dts/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,26 @@ impl<'a> TransformerDts<'a> {
&self,
property: &PropertyDefinition<'a>,
) -> ClassElement<'a> {
let type_annotations = property
.type_annotation
.as_ref()
.map(|type_annotation| self.ctx.ast.copy(type_annotation))
.or_else(|| {
let new_type = property
.value
.as_ref()
.and_then(|expr| self.infer_type_from_expression(expr))
.unwrap_or_else(|| {
// report error for has no type annotation
self.ctx.ast.ts_unknown_keyword(property.span)
});
let type_annotations = if property.accessibility.is_some_and(|a| a.is_private()) {
None
} else {
property
.type_annotation
.as_ref()
.map(|type_annotation| self.ctx.ast.copy(type_annotation))
.or_else(|| {
let new_type = property
.value
.as_ref()
.and_then(|expr| self.infer_type_from_expression(expr))
.unwrap_or_else(|| {
// report error for has no type annotation
self.ctx.ast.ts_unknown_keyword(property.span)
});

Some(self.ctx.ast.ts_type_annotation(SPAN, new_type))
});
Some(self.ctx.ast.ts_type_annotation(SPAN, new_type))
})
};

self.ctx.ast.class_property(
property.r#type,
Expand All @@ -83,6 +87,23 @@ impl<'a> TransformerDts<'a> {
params: Box<'a, FormalParameters<'a>>,
) -> ClassElement<'a> {
let function = &definition.value;
if definition.accessibility.is_some_and(|a| a.is_private()) {
let r#type = match definition.r#type {
MethodDefinitionType::MethodDefinition => {
PropertyDefinitionType::PropertyDefinition
}
MethodDefinitionType::TSAbstractMethodDefinition => {
PropertyDefinitionType::TSAbstractPropertyDefinition
}
};
return self.create_class_property(
r#type,
self.ctx.ast.copy(&definition.key),
definition.r#override,
self.transform_accessibility(definition.accessibility),
);
}

let type_annotation = self.infer_function_return_type(function);

let value = self.ctx.ast.function(
Expand Down Expand Up @@ -113,6 +134,31 @@ impl<'a> TransformerDts<'a> {
)
}

pub fn create_class_property(
&self,
r#type: PropertyDefinitionType,
key: PropertyKey<'a>,
r#override: bool,
accessibility: Option<TSAccessibility>,
) -> ClassElement<'a> {
self.ctx.ast.class_property(
r#type,
SPAN,
key,
None,
false,
false,
false,
r#override,
false,
false,
false,
None,
accessibility,
self.ctx.ast.new_vec(),
)
}

pub fn transform_formal_parameter_to_class_property(
&self,
param: &FormalParameter<'a>,
Expand Down

0 comments on commit 35c382e

Please sign in to comment.