Skip to content

Commit

Permalink
fix(isolated-declarations): keep literal value for readonly property (#…
Browse files Browse the repository at this point in the history
…4106)

close: #4036
  • Loading branch information
Dunqing committed Jul 8, 2024
1 parent e67c7d1 commit 5c31236
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
60 changes: 32 additions & 28 deletions crates/oxc_isolated_declarations/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,40 +54,44 @@ impl<'a> IsolatedDeclarations<'a> {
&self,
property: &PropertyDefinition<'a>,
) -> ClassElement<'a> {
let type_annotations = if property.accessibility.is_some_and(|a| a.is_private()) {
None
} else {
property
.type_annotation
.as_ref()
.map(|type_annotation| self.ast.copy(type_annotation))
.or_else(|| {
property
.value
.as_ref()
.and_then(|expr| {
let ts_type = if property.readonly {
self.transform_expression_to_ts_type(expr)
} else {
self.infer_type_from_expression(expr)
};
if ts_type.is_none() {
self.error(property_must_have_explicit_type(property.key.span()));
}
ts_type
})
.map(|ts_type| self.ast.ts_type_annotation(SPAN, ts_type))
})
};
let mut type_annotations = None;
let mut value = None;

if property.accessibility.map_or(true, |a| !a.is_private()) {
if property.type_annotation.is_some() {
type_annotations = self.ast.copy(&property.type_annotation);
} else if let Some(expr) = property.value.as_ref() {
let ts_type = if property.readonly {
// `field = 'string'` remain `field = 'string'` instead of `field: 'string'`
if Self::is_need_to_infer_type_from_expression(expr) {
self.transform_expression_to_ts_type(expr)
} else {
if let Expression::TemplateLiteral(lit) = expr {
value = self
.transform_template_to_string(lit)
.map(Expression::StringLiteral);
} else {
value = Some(self.ast.copy(expr));
}
None
}
} else {
self.infer_type_from_expression(expr)
};

// TODO if inferred type_annotations is TSLiteral, it should stand as value,
// so `field = 'string'` remain `field = 'string'` instead of `field: 'string'`
type_annotations = ts_type.map(|t| self.ast.ts_type_annotation(SPAN, t));
}

if type_annotations.is_none() && value.is_none() {
self.error(property_must_have_explicit_type(property.key.span()));
}
}

self.ast.class_property(
property.r#type,
property.span,
self.ast.copy(&property.key),
None,
value,
property.computed,
property.r#static,
property.declare,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export declare abstract class Qux {
baz(): void;
}
export declare class Baz {
readonly prop1: 'some string';
readonly prop1 = 'some string';
prop2: string;
private prop3;
private prop4;
Expand Down

0 comments on commit 5c31236

Please sign in to comment.