Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Refactor Class AST Tree Structure #1791

Merged
merged 12 commits into from
Nov 18, 2021
37 changes: 18 additions & 19 deletions crates/rome_formatter/src/cst.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use crate::{FormatElement, FormatResult, Formatter, ToFormatElement};
use rslint_parser::ast::{
ArgList, ArrayPattern, AssignPattern, CallExpr, ClassBody, ClassDecl, ClassProp, Condition,
ConstructorParameters, ForInStmt, ForStmt, ForStmtInit, ForStmtTest, ForStmtUpdate, Getter,
JsArrayExpression, JsArrowFunctionExpression, JsBlockStatement, JsBooleanLiteral, JsCaseClause,
JsCatchClause, JsContinueStatement, JsDebuggerStatement, JsDefaultClause, JsDoWhileStatement,
JsEmptyStatement, JsExpressionStatement, JsFinallyClause, JsFunctionDeclaration, JsIfStatement,
JsLabeledStatement, JsNullLiteral, JsNumberLiteral, JsObjectExpression, JsParameterList,
JsPropertyObjectMember, JsReferenceIdentifierExpression, JsReturnStatement, JsRoot,
JsSequenceExpression, JsShorthandPropertyObjectMember, JsStringLiteral, JsSwitchStatement,
ArgList, ArrayPattern, AssignPattern, CallExpr, Condition, ForInStmt, ForStmt, ForStmtInit,
ForStmtTest, ForStmtUpdate, JsArrayExpression, JsArrowFunctionExpression, JsBlockStatement,
JsBooleanLiteral, JsCaseClause, JsCatchClause, JsClassDeclaration, JsContinueStatement,
JsDebuggerStatement, JsDefaultClause, JsDoWhileStatement, JsEmptyStatement,
JsExpressionStatement, JsFinallyClause, JsFunctionDeclaration, JsGetterClassMember,
JsIfStatement, JsLabeledStatement, JsNullLiteral, JsNumberLiteral, JsObjectExpression,
JsParameterList, JsPropertyClassMember, JsPropertyObjectMember,
JsReferenceIdentifierExpression, JsReturnStatement, JsRoot, JsSequenceExpression,
JsSetterClassMember, JsShorthandPropertyObjectMember, JsStringLiteral, JsSwitchStatement,
JsTryStatement, JsVariableDeclarationStatement, JsVariableDeclarator, JsWhileStatement,
JsWithStatement, Name, Setter, SinglePattern,
JsWithStatement, Name, SinglePattern,
};
use rslint_parser::{AstNode, SyntaxKind, SyntaxNode};

Expand Down Expand Up @@ -162,22 +163,20 @@ impl ToFormatElement for SyntaxNode {
SyntaxKind::JS_PROPERTY_OBJECT_MEMBER => JsPropertyObjectMember::cast(self.clone())
.unwrap()
.to_format_element(formatter),
SyntaxKind::CLASS_DECL => ClassDecl::cast(self.clone())
SyntaxKind::JS_CLASS_DECLARATION => JsClassDeclaration::cast(self.clone())
.unwrap()
.to_format_element(formatter),
SyntaxKind::CLASS_BODY => ClassBody::cast(self.clone())
// TODO
// SyntaxKind::CONSTRUCTOR_PARAMETERS => ConstructorParameters::cast(self.clone())
// .unwrap()
// .to_format_element(formatter),
SyntaxKind::JS_GETTER_CLASS_MEMBER => JsGetterClassMember::cast(self.clone())
.unwrap()
.to_format_element(formatter),
SyntaxKind::CONSTRUCTOR_PARAMETERS => ConstructorParameters::cast(self.clone())
SyntaxKind::JS_SETTER_CLASS_MEMBER => JsSetterClassMember::cast(self.clone())
.unwrap()
.to_format_element(formatter),
SyntaxKind::GETTER => Getter::cast(self.clone())
.unwrap()
.to_format_element(formatter),
SyntaxKind::SETTER => Setter::cast(self.clone())
.unwrap()
.to_format_element(formatter),
SyntaxKind::CLASS_PROP => ClassProp::cast(self.clone())
SyntaxKind::JS_PROPERTY_CLASS_MEMBER => JsPropertyClassMember::cast(self.clone())
.unwrap()
.to_format_element(formatter),

Expand Down
39 changes: 39 additions & 0 deletions crates/rome_formatter/src/ts/class/class_declaration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use crate::{
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, sorry. Don't know why git didn't recognise my renames of the formatter files, maybe because I rebased the commits?

block_indent, empty_element, format_elements, group_elements, hard_line_break, join_elements,
space_token, FormatElement, FormatResult, Formatter, ToFormatElement,
};
use rslint_parser::ast::{JsClassDeclaration, SuperCall};

impl ToFormatElement for JsClassDeclaration {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
let extends = if let Some(extends_clause) = self.extends_clause() {
format_elements![space_token(), formatter.format_node(extends_clause)?]
} else {
empty_element()
};

Ok(format_elements![
formatter.format_token(&self.class_token()?)?,
space_token(),
formatter.format_node(self.id()?)?,
extends,
space_token(),
group_elements(format_elements![
formatter.format_token(&self.l_curly_token()?)?,
block_indent(join_elements(
hard_line_break(),
formatter.format_nodes(self.members())?
)),
formatter.format_token(&self.r_curly_token()?)?
])
])
}
}

impl ToFormatElement for SuperCall {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
let super_token = formatter.format_token(&self.super_token()?)?;
let arguments = formatter.format_node(self.arguments()?)?;
Ok(format_elements![super_token, arguments])
}
}
74 changes: 0 additions & 74 deletions crates/rome_formatter/src/ts/class/class_declarator.rs

This file was deleted.

22 changes: 22 additions & 0 deletions crates/rome_formatter/src/ts/class/class_member.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use crate::{FormatElement, FormatResult, Formatter, ToFormatElement};
use rslint_parser::ast::JsAnyClassMember;

impl ToFormatElement for JsAnyClassMember {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
match self {
JsAnyClassMember::JsSemicolonClassMember(empty_statement) => {
empty_statement.to_format_element(formatter)
}
JsAnyClassMember::JsMethodClassMember(method) => method.to_format_element(formatter),
JsAnyClassMember::JsPropertyClassMember(class_prop) => {
class_prop.to_format_element(formatter)
}
JsAnyClassMember::JsConstructorClassMember(constructor) => {
constructor.to_format_element(formatter)
}
JsAnyClassMember::JsGetterClassMember(getter) => getter.to_format_element(formatter),
JsAnyClassMember::JsSetterClassMember(setter) => setter.to_format_element(formatter),
JsAnyClassMember::JsUnknownMember(_) => todo!(),
}
}
}
12 changes: 12 additions & 0 deletions crates/rome_formatter/src/ts/class/class_member_name.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use crate::{FormatElement, FormatResult, Formatter, ToFormatElement};
use rslint_parser::ast::JsAnyClassMemberName;

impl ToFormatElement for JsAnyClassMemberName {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
match self {
JsAnyClassMemberName::JsStaticMemberName(name) => name.to_format_element(formatter),
JsAnyClassMemberName::JsComputedMemberName(_) => todo!(),
JsAnyClassMemberName::JsPrivateClassMemberName(_) => todo!(),
}
}
}
45 changes: 0 additions & 45 deletions crates/rome_formatter/src/ts/class/constructor.rs

This file was deleted.

58 changes: 58 additions & 0 deletions crates/rome_formatter/src/ts/class/constructor_class_member.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use crate::{
format_elements, group_elements, join_elements, soft_line_break_or_space, space_token, token,
FormatElement, FormatResult, Formatter, ToFormatElement,
};
use rslint_parser::ast::{
JsAnyConstructorMemberName, JsAnyConstructorParameter, JsConstructorClassMember,
JsConstructorParameterList,
};

impl ToFormatElement for JsConstructorClassMember {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
Ok(format_elements![
formatter.format_node(self.name()?)?,
formatter.format_node(self.parameter_list()?)?,
space_token(),
formatter.format_node(self.body()?)?
])
}
}

impl ToFormatElement for JsAnyConstructorMemberName {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
match self {
JsAnyConstructorMemberName::JsStringLiteral(literal) => {
literal.to_format_element(formatter)
}
JsAnyConstructorMemberName::JsStaticMemberName(name) => {
name.to_format_element(formatter)
}
}
}
}

impl ToFormatElement for JsConstructorParameterList {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
let l_bracket = formatter.format_token(&self.l_paren_token()?)?;
let params = formatter.format_nodes(self.parameters())?;
let r_bracket = formatter.format_token(&self.r_paren_token()?)?;

Ok(format_elements![group_elements(format_elements![
l_bracket,
join_elements(
format_elements![token(","), soft_line_break_or_space()],
params
),
r_bracket
])])
}
}

impl ToFormatElement for JsAnyConstructorParameter {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
match self {
JsAnyConstructorParameter::TsConstructorParam(_) => todo!(),
JsAnyConstructorParameter::Pattern(pattern) => pattern.to_format_element(formatter),
}
}
}
14 changes: 14 additions & 0 deletions crates/rome_formatter/src/ts/class/extends_clause.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use crate::{
format_elements, space_token, FormatElement, FormatResult, Formatter, ToFormatElement,
};
use rslint_parser::ast::JsExtendsClause;

impl ToFormatElement for JsExtendsClause {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
Ok(format_elements![
formatter.format_token(&self.extends_token()?)?,
space_token(),
formatter.format_node(self.super_class()?)?
])
}
}
18 changes: 18 additions & 0 deletions crates/rome_formatter/src/ts/class/getter_class_member.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use crate::{
format_elements, space_token, FormatElement, FormatResult, Formatter, ToFormatElement,
};
use rslint_parser::ast::JsGetterClassMember;

impl ToFormatElement for JsGetterClassMember {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
Ok(format_elements![
formatter.format_token(&self.get_token()?)?,
space_token(),
formatter.format_node(self.name()?)?,
formatter.format_token(&self.l_paren_token()?)?,
formatter.format_token(&self.r_paren_token()?)?,
space_token(),
formatter.format_node(self.body()?)?
])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ use crate::{
empty_element, format_elements, space_token, FormatElement, FormatResult, Formatter,
ToFormatElement,
};
use rslint_parser::ast::Method;
use rslint_parser::ast::JsMethodClassMember;

impl ToFormatElement for Method {
impl ToFormatElement for JsMethodClassMember {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
let private_token = if let Some(static_token) = self.static_token() {
format_elements![formatter.format_token(&static_token)?, space_token()]
} else {
empty_element()
};
let name = formatter.format_node(self.name()?)?;
let params = formatter.format_node(self.parameters()?)?;
let params = formatter.format_node(self.parameter_list()?)?;
let body = formatter.format_node(self.body()?)?;
Ok(format_elements![
private_token,
Expand Down
13 changes: 10 additions & 3 deletions crates/rome_formatter/src/ts/class/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
mod class_declarator;
mod constructor;
mod prop;
mod class_declaration;
mod class_member;
mod class_member_name;
mod constructor_class_member;
mod extends_clause;
mod getter_class_member;
mod method_class_member;
mod property_class_member;
mod semicolon_class_member;
mod setter_class_member;
Loading