Skip to content

Commit

Permalink
feat(rome_js_parser): support property init in ambient context (new n…
Browse files Browse the repository at this point in the history
…ode) (rome#4225)
  • Loading branch information
Conaclos authored Feb 22, 2023
1 parent c0556ff commit 62c7738
Show file tree
Hide file tree
Showing 24 changed files with 1,148 additions and 89 deletions.
44 changes: 44 additions & 0 deletions crates/rome_js_factory/src/generated/node_factory.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions crates/rome_js_factory/src/generated/syntax_factory.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions crates/rome_js_formatter/src/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4005,6 +4005,19 @@ impl IntoFormat<JsFormatContext> for rome_js_syntax::TsPropertySignatureClassMem
FormatOwnedWithRule :: new (self , crate :: ts :: classes :: property_signature_class_member :: FormatTsPropertySignatureClassMember :: default ())
}
}
impl FormatRule < rome_js_syntax :: TsInitializedPropertySignatureClassMember > for crate :: ts :: classes :: initialized_property_signature_class_member :: FormatTsInitializedPropertySignatureClassMember { type Context = JsFormatContext ; # [inline (always)] fn fmt (& self , node : & rome_js_syntax :: TsInitializedPropertySignatureClassMember , f : & mut JsFormatter) -> FormatResult < () > { FormatNodeRule :: < rome_js_syntax :: TsInitializedPropertySignatureClassMember > :: fmt (self , node , f) } }
impl AsFormat<JsFormatContext> for rome_js_syntax::TsInitializedPropertySignatureClassMember {
type Format < 'a > = FormatRefWithRule < 'a , rome_js_syntax :: TsInitializedPropertySignatureClassMember , crate :: ts :: classes :: initialized_property_signature_class_member :: FormatTsInitializedPropertySignatureClassMember > ;
fn format(&self) -> Self::Format<'_> {
FormatRefWithRule :: new (self , crate :: ts :: classes :: initialized_property_signature_class_member :: FormatTsInitializedPropertySignatureClassMember :: default ())
}
}
impl IntoFormat<JsFormatContext> for rome_js_syntax::TsInitializedPropertySignatureClassMember {
type Format = FormatOwnedWithRule < rome_js_syntax :: TsInitializedPropertySignatureClassMember , crate :: ts :: classes :: initialized_property_signature_class_member :: FormatTsInitializedPropertySignatureClassMember > ;
fn into_format(self) -> Self::Format {
FormatOwnedWithRule :: new (self , crate :: ts :: classes :: initialized_property_signature_class_member :: FormatTsInitializedPropertySignatureClassMember :: default ())
}
}
impl FormatRule<rome_js_syntax::TsMethodSignatureClassMember>
for crate::ts::classes::method_signature_class_member::FormatTsMethodSignatureClassMember
{
Expand Down
3 changes: 3 additions & 0 deletions crates/rome_js_formatter/src/js/any/class_member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ impl FormatRule<AnyJsClassMember> for FormatAnyJsClassMember {
AnyJsClassMember::JsSetterClassMember(node) => node.format().fmt(f),
AnyJsClassMember::TsConstructorSignatureClassMember(node) => node.format().fmt(f),
AnyJsClassMember::TsPropertySignatureClassMember(node) => node.format().fmt(f),
AnyJsClassMember::TsInitializedPropertySignatureClassMember(node) => {
node.format().fmt(f)
}
AnyJsClassMember::TsMethodSignatureClassMember(node) => node.format().fmt(f),
AnyJsClassMember::TsGetterSignatureClassMember(node) => node.format().fmt(f),
AnyJsClassMember::TsSetterSignatureClassMember(node) => node.format().fmt(f),
Expand Down
17 changes: 14 additions & 3 deletions crates/rome_js_formatter/src/js/classes/property_class_member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::utils::{AnyJsAssignmentLike, FormatSemicolon};
use rome_formatter::write;
use rome_js_syntax::{
AnyJsClassMember, AnyJsClassMemberName, JsInitializerClause, JsPropertyClassMember,
JsSyntaxToken, TsPropertySignatureClassMember,
JsSyntaxToken, TsInitializedPropertySignatureClassMember, TsPropertySignatureClassMember,
};
use rome_rowan::{declare_node_union, SyntaxResult};

Expand All @@ -28,21 +28,27 @@ impl FormatNodeRule<JsPropertyClassMember> for FormatJsPropertyClassMember {
}

declare_node_union! {
pub(crate) AnyJsPropertyClassMember = JsPropertyClassMember | TsPropertySignatureClassMember
pub(crate) AnyJsPropertyClassMember = JsPropertyClassMember | TsPropertySignatureClassMember | TsInitializedPropertySignatureClassMember
}

impl AnyJsPropertyClassMember {
fn name(&self) -> SyntaxResult<AnyJsClassMemberName> {
match self {
AnyJsPropertyClassMember::JsPropertyClassMember(property) => property.name(),
AnyJsPropertyClassMember::TsPropertySignatureClassMember(property) => property.name(),
AnyJsPropertyClassMember::TsInitializedPropertySignatureClassMember(property) => {
property.name()
}
}
}

fn value(&self) -> Option<JsInitializerClause> {
match self {
AnyJsPropertyClassMember::JsPropertyClassMember(property) => property.value(),
AnyJsPropertyClassMember::TsPropertySignatureClassMember(_) => None,
AnyJsPropertyClassMember::TsInitializedPropertySignatureClassMember(property) => {
property.value().ok()
}
}
}

Expand All @@ -54,6 +60,7 @@ impl AnyJsPropertyClassMember {
AnyJsPropertyClassMember::TsPropertySignatureClassMember(property) => {
property.property_annotation().is_some()
}
AnyJsPropertyClassMember::TsInitializedPropertySignatureClassMember(_) => false,
}
}
}
Expand Down Expand Up @@ -130,7 +137,8 @@ fn needs_semicolon(property: &AnyJsPropertyClassMember) -> SyntaxResult<bool> {

// Computed members may be misinterpreted as array accessors/array types
member @ AnyJsClassMember::JsPropertyClassMember(_)
| member @ AnyJsClassMember::TsPropertySignatureClassMember(_) => match member.name()? {
| member @ AnyJsClassMember::TsPropertySignatureClassMember(_)
| member @ AnyJsClassMember::TsInitializedPropertySignatureClassMember(_) => match member.name()? {
Some(name) => name.is_computed(),
None => false,
},
Expand Down Expand Up @@ -175,6 +183,9 @@ fn has_modifiers(member: &AnyJsClassMember) -> bool {
AnyJsClassMember::TsPropertySignatureClassMember(property) => {
property.modifiers().is_empty()
}
AnyJsClassMember::TsInitializedPropertySignatureClassMember(property) => {
property.modifiers().is_empty()
}
AnyJsClassMember::TsSetterSignatureClassMember(setter) => setter.modifiers().is_empty(),
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use crate::js::classes::property_class_member::{
AnyJsPropertyClassMember, FormatClassPropertySemicolon,
};
use crate::prelude::*;
use crate::utils::AnyJsAssignmentLike;
use rome_formatter::write;
use rome_js_syntax::TsInitializedPropertySignatureClassMember;
#[derive(Debug, Clone, Default)]
pub(crate) struct FormatTsInitializedPropertySignatureClassMember;
impl FormatNodeRule<TsInitializedPropertySignatureClassMember>
for FormatTsInitializedPropertySignatureClassMember
{
fn fmt_fields(
&self,
node: &TsInitializedPropertySignatureClassMember,
f: &mut JsFormatter,
) -> FormatResult<()> {
let semicolon_token = node.semicolon_token();

write!(
f,
[
AnyJsAssignmentLike::from(node.clone()),
FormatClassPropertySemicolon::new(
&AnyJsPropertyClassMember::from(node.clone()),
semicolon_token.as_ref()
)
]
)
}
}
1 change: 1 addition & 0 deletions crates/rome_js_formatter/src/ts/classes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub(crate) mod constructor_signature_class_member;
pub(crate) mod extends_clause;
pub(crate) mod getter_signature_class_member;
pub(crate) mod index_signature_class_member;
pub(crate) mod initialized_property_signature_class_member;
pub(crate) mod method_signature_class_member;
pub(crate) mod property_signature_class_member;
pub(crate) mod setter_signature_class_member;
58 changes: 56 additions & 2 deletions crates/rome_js_formatter/src/utils/assignment_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ use rome_js_syntax::{
AnyTsVariableAnnotation, JsAssignmentExpression, JsInitializerClause, JsLiteralMemberName,
JsObjectAssignmentPattern, JsObjectAssignmentPatternProperty, JsObjectBindingPattern,
JsPropertyClassMember, JsPropertyClassMemberFields, JsPropertyObjectMember, JsSyntaxKind,
JsVariableDeclarator, TsIdentifierBinding, TsPropertySignatureClassMember,
JsVariableDeclarator, TsIdentifierBinding, TsInitializedPropertySignatureClassMember,
TsInitializedPropertySignatureClassMemberFields, TsPropertySignatureClassMember,
TsPropertySignatureClassMemberFields, TsTypeAliasDeclaration, TsTypeArguments,
};
use rome_rowan::{declare_node_union, AstNode, SyntaxNodeOptionExt, SyntaxResult};
Expand All @@ -27,7 +28,8 @@ declare_node_union! {
JsVariableDeclarator |
TsTypeAliasDeclaration |
JsPropertyClassMember |
TsPropertySignatureClassMember
TsPropertySignatureClassMember |
TsInitializedPropertySignatureClassMember
}

declare_node_union! {
Expand Down Expand Up @@ -337,6 +339,10 @@ impl AnyJsAssignmentLike {
AnyJsAssignmentLike::TsPropertySignatureClassMember(_) => {
unreachable!("TsPropertySignatureClassMember doesn't have any right side. If you're here, `has_only_left_hand_side` hasn't been called")
}
AnyJsAssignmentLike::TsInitializedPropertySignatureClassMember(n) => {
// SAFETY: Calling `unwrap` here is safe because we check `has_only_left_hand_side` variant at the beginning of the `layout` function
n.value().unwrap().into()
}
};

Ok(right)
Expand All @@ -363,6 +369,9 @@ impl AnyJsAssignmentLike {
AnyJsAssignmentLike::TsPropertySignatureClassMember(
property_signature_class_member,
) => Ok(property_signature_class_member.name()?.into()),
AnyJsAssignmentLike::TsInitializedPropertySignatureClassMember(
property_signature_class_member,
) => Ok(property_signature_class_member.name()?.into()),
}
}

Expand Down Expand Up @@ -470,6 +479,26 @@ impl AnyJsAssignmentLike {
(u8::from(f.options().tab_width()) + MIN_OVERLAP_FOR_BREAK) as usize;
Ok(width < text_width_for_break)
}
AnyJsAssignmentLike::TsInitializedPropertySignatureClassMember(
property_signature_class_member,
) => {
let TsInitializedPropertySignatureClassMemberFields {
modifiers,
name,
question_mark_token,
value: _,
semicolon_token: _,
} = property_signature_class_member.as_fields();

write!(f, [modifiers.format(), space(),])?;

let width = write_member_name(&name?.into(), f)?;

write!(f, [question_mark_token.format()])?;
let text_width_for_break =
(u8::from(f.options().tab_width()) + MIN_OVERLAP_FOR_BREAK) as usize;
Ok(width < text_width_for_break)
}
}
}

Expand Down Expand Up @@ -507,6 +536,13 @@ impl AnyJsAssignmentLike {
}
// this variant doesn't have any operator
AnyJsAssignmentLike::TsPropertySignatureClassMember(_) => Ok(()),
AnyJsAssignmentLike::TsInitializedPropertySignatureClassMember(
property_class_member,
) => {
let initializer = property_class_member.value()?;
let eq_token = initializer.eq_token()?;
write!(f, [space(), eq_token.format()])
}
}
}

Expand Down Expand Up @@ -574,12 +610,30 @@ impl AnyJsAssignmentLike {
}
// this variant doesn't have any right part
AnyJsAssignmentLike::TsPropertySignatureClassMember(_) => Ok(()),
AnyJsAssignmentLike::TsInitializedPropertySignatureClassMember(
property_class_member,
) => {
let initializer = property_class_member.value()?;
let expression = initializer.expression()?;
write!(
f,
[
space(),
format_leading_comments(initializer.syntax()),
with_assignment_layout(&expression, Some(layout)),
format_trailing_comments(initializer.syntax())
]
)
}
}
}

fn write_suppressed_initializer(&self, f: &mut JsFormatter) -> FormatResult<()> {
let initializer = match self {
AnyJsAssignmentLike::JsPropertyClassMember(class_member) => class_member.value(),
AnyJsAssignmentLike::TsInitializedPropertySignatureClassMember(class_member) => {
Some(class_member.value()?)
}
AnyJsAssignmentLike::JsVariableDeclarator(variable_declarator) => {
variable_declarator.initializer()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
declare class A {
readonly prop = "value";


}
export class B {

declare readonly prop = "value__value__value__value__value__value__value";

}
Loading

0 comments on commit 62c7738

Please sign in to comment.