This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 660
Refactor Class AST Tree Structure #1791
Merged
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5f78048
Extract class parsing into `syntax/class.rs`
MichaReiser af3cfc3
Split `class_decl` into `class_declaration` and `class_expression`
MichaReiser bc97bd5
Add tests
MichaReiser dba1dc6
Parsing
MichaReiser ce8d902
Rename SemicolonMember to Empty Member
MichaReiser 54d85cd
Test for `declare` members, add test cases
MichaReiser 0be9108
Test private members
MichaReiser 6facea9
Add typescript members for parity with parser
MichaReiser 3fb0239
Inline literals/identifier into StaticMemberName
MichaReiser b47ff1e
Update nodes
MichaReiser b268e4a
Rename StaticMemberName to LiteralMemberName
MichaReiser 25b1fe2
Fix shorthand property parsing
MichaReiser File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use crate::{ | ||
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]) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!(), | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
58 changes: 58 additions & 0 deletions
58
crates/rome_formatter/src/ts/class/constructor_class_member.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()?)? | ||
]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()?)? | ||
]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?