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 656
feat(rome_formatter): format export
syntax
#2008
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5768f00
feat(rome_formatter): initial formatting of module
ematipico 4468ea0
chore: reduce bloat
ematipico ce7b5cb
feat: format import syntax
ematipico 6daf29e
feat: named import formatting
ematipico 59dfcbb
feat: export syntax
ematipico 2f0b088
feat: default expression and function
ematipico 01dfc39
chore: export from syntax
ematipico bed0370
rebase
ematipico c7657a1
rebase and fix indent
ematipico 17671a9
revert change
ematipico ff5afa2
code review
ematipico 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,24 @@ | ||
use crate::{FormatElement, FormatResult, Formatter, ToFormatElement}; | ||
use rslint_parser::ast::JsAnyExportClause; | ||
|
||
impl ToFormatElement for JsAnyExportClause { | ||
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> { | ||
match self { | ||
JsAnyExportClause::JsExportClassClause(node) => node.to_format_element(formatter), | ||
JsAnyExportClause::JsExportDefaultClassClause(node) => { | ||
node.to_format_element(formatter) | ||
} | ||
JsAnyExportClause::JsExportDefaultExpressionClause(node) => { | ||
node.to_format_element(formatter) | ||
} | ||
JsAnyExportClause::JsExportDefaultFunctionClause(node) => { | ||
node.to_format_element(formatter) | ||
} | ||
JsAnyExportClause::JsExportFromClause(node) => node.to_format_element(formatter), | ||
JsAnyExportClause::JsExportFunctionClause(node) => node.to_format_element(formatter), | ||
JsAnyExportClause::JsExportNamedClause(node) => node.to_format_element(formatter), | ||
JsAnyExportClause::JsExportNamedFromClause(node) => node.to_format_element(formatter), | ||
JsAnyExportClause::JsExportVariableClause(node) => node.to_format_element(formatter), | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
crates/rome_formatter/src/ts/export/any_export_name_specifier.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,15 @@ | ||
use crate::{FormatElement, FormatResult, Formatter, ToFormatElement}; | ||
use rslint_parser::ast::JsAnyExportNamedSpecifier; | ||
|
||
impl ToFormatElement for JsAnyExportNamedSpecifier { | ||
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> { | ||
match self { | ||
JsAnyExportNamedSpecifier::JsExportNamedShorthandSpecifier(node) => { | ||
node.to_format_element(formatter) | ||
} | ||
JsAnyExportNamedSpecifier::JsExportNamedSpecifier(node) => { | ||
node.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,13 @@ | ||
use crate::{ | ||
format_elements, space_token, FormatElement, FormatResult, Formatter, ToFormatElement, | ||
}; | ||
use rslint_parser::ast::JsExportAsClause; | ||
|
||
impl ToFormatElement for JsExportAsClause { | ||
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> { | ||
let as_token = formatter.format_token(&self.as_token()?)?; | ||
let exported_name = formatter.format_node(&self.exported_name()?)?; | ||
|
||
Ok(format_elements![as_token, space_token(), exported_name]) | ||
} | ||
} |
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,8 @@ | ||
use crate::{FormatElement, FormatResult, Formatter, ToFormatElement}; | ||
use rslint_parser::ast::{JsAnyClass, JsExportClassClause}; | ||
|
||
impl ToFormatElement for JsExportClassClause { | ||
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> { | ||
JsAnyClass::from(self.clone()).to_format_element(formatter) | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
crates/rome_formatter/src/ts/export/default_class_clause.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,13 @@ | ||
use crate::{ | ||
format_elements, space_token, FormatElement, FormatResult, Formatter, ToFormatElement, | ||
}; | ||
use rslint_parser::ast::{JsAnyClass, JsExportDefaultClassClause}; | ||
|
||
impl ToFormatElement for JsExportDefaultClassClause { | ||
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> { | ||
let default_token = formatter.format_token(&self.default_token()?)?; | ||
let class = JsAnyClass::from(self.clone()).to_format_element(formatter)?; | ||
|
||
Ok(format_elements![default_token, space_token(), class]) | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
crates/rome_formatter/src/ts/export/default_expression_clause.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,22 @@ | ||
use crate::{ | ||
format_elements, space_token, token, FormatElement, FormatResult, Formatter, ToFormatElement, | ||
}; | ||
use rslint_parser::ast::JsExportDefaultExpressionClause; | ||
|
||
impl ToFormatElement for JsExportDefaultExpressionClause { | ||
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> { | ||
let default_token = formatter.format_token(&self.default_token()?)?; | ||
let class = formatter.format_node(&self.expression()?)?; | ||
let semicolon = if let Some(semicolon) = &self.semicolon_token() { | ||
formatter.format_token(semicolon)? | ||
} else { | ||
token(";") | ||
}; | ||
Ok(format_elements![ | ||
default_token, | ||
space_token(), | ||
class, | ||
semicolon | ||
]) | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
crates/rome_formatter/src/ts/export/default_function_clause.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,13 @@ | ||
use crate::{ | ||
format_elements, space_token, FormatElement, FormatResult, Formatter, ToFormatElement, | ||
}; | ||
use rslint_parser::ast::{JsAnyFunction, JsExportDefaultFunctionClause}; | ||
|
||
impl ToFormatElement for JsExportDefaultFunctionClause { | ||
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> { | ||
let default_token = formatter.format_token(&self.default_token()?)?; | ||
let class = JsAnyFunction::from(self.clone()).to_format_element(formatter)?; | ||
|
||
Ok(format_elements![default_token, space_token(), 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,38 @@ | ||
use crate::{ | ||
empty_element, format_elements, space_token, token, FormatElement, FormatResult, Formatter, | ||
ToFormatElement, | ||
}; | ||
use rslint_parser::ast::JsExportFromClause; | ||
|
||
impl ToFormatElement for JsExportFromClause { | ||
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> { | ||
let star = formatter.format_token(&self.star_token()?)?; | ||
let export_as = if let Some(export_as) = self.export_as() { | ||
format_elements![formatter.format_node(&export_as)?, space_token()] | ||
} else { | ||
empty_element() | ||
}; | ||
let from = formatter.format_token(&self.from_token()?)?; | ||
let source = formatter.format_node(&self.source()?)?; | ||
let assertion = if let Some(assertion) = self.assertion() { | ||
formatter.format_node(&assertion)? | ||
} else { | ||
empty_element() | ||
}; | ||
let semicolon = if let Some(semicolon) = self.semicolon_token() { | ||
formatter.format_token(&semicolon)? | ||
} else { | ||
token(";") | ||
}; | ||
Ok(format_elements![ | ||
star, | ||
space_token(), | ||
export_as, | ||
from, | ||
space_token(), | ||
source, | ||
assertion, | ||
semicolon | ||
]) | ||
} | ||
} |
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,8 @@ | ||
use crate::{FormatElement, FormatResult, Formatter, ToFormatElement}; | ||
use rslint_parser::ast::{JsAnyFunction, JsExportFunctionClause}; | ||
|
||
impl ToFormatElement for JsExportFunctionClause { | ||
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> { | ||
JsAnyFunction::from(self.clone()).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,28 @@ | ||
mod any_export_clause; | ||
mod any_export_name_specifier; | ||
mod as_clause; | ||
mod class_clause; | ||
mod default_class_clause; | ||
mod default_expression_clause; | ||
mod default_function_clause; | ||
mod from_clause; | ||
mod function_clause; | ||
mod named_clause; | ||
mod named_from_clause; | ||
mod named_from_specifier; | ||
mod named_shorthand_specifier; | ||
mod named_specifier; | ||
mod variable_clause; | ||
|
||
use crate::{ | ||
format_elements, space_token, FormatElement, FormatResult, Formatter, ToFormatElement, | ||
}; | ||
use rslint_parser::ast::JsExport; | ||
|
||
impl ToFormatElement for JsExport { | ||
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> { | ||
let export_token = formatter.format_token(&self.export_token()?)?; | ||
let export_clause = formatter.format_node(&self.export_clause()?)?; | ||
Ok(format_elements![export_token, space_token(), export_clause]) | ||
} | ||
} |
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,44 @@ | ||
use crate::{ | ||
empty_element, format_elements, group_elements, if_group_fits_on_single_line, join_elements, | ||
soft_block_indent, soft_line_break_or_space, space_token, token, FormatElement, FormatResult, | ||
Formatter, ToFormatElement, | ||
}; | ||
use rslint_parser::ast::JsExportNamedClause; | ||
use rslint_parser::AstSeparatedList; | ||
|
||
impl ToFormatElement for JsExportNamedClause { | ||
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> { | ||
let specifiers = self.specifiers(); | ||
let space = if specifiers.is_empty() { | ||
empty_element() | ||
} else { | ||
if_group_fits_on_single_line(space_token()) | ||
}; | ||
let list = group_elements(formatter.format_delimited( | ||
&self.l_curly_token()?, | ||
|leading, trailing| { | ||
Ok(format_elements!( | ||
space.clone(), | ||
soft_block_indent(format_elements![ | ||
leading, | ||
join_elements( | ||
soft_line_break_or_space(), | ||
formatter.format_separated(specifiers, || token(","))? | ||
), | ||
trailing, | ||
]), | ||
space, | ||
)) | ||
}, | ||
&self.r_curly_token()?, | ||
)?); | ||
|
||
let semicolon = if let Some(semicolon) = self.semicolon_token() { | ||
formatter.format_token(&semicolon)? | ||
} else { | ||
token(";") | ||
}; | ||
|
||
Ok(format_elements![list, semicolon]) | ||
} | ||
} |
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,60 @@ | ||
use crate::{ | ||
empty_element, format_elements, group_elements, if_group_fits_on_single_line, join_elements, | ||
soft_block_indent, soft_line_break_or_space, space_token, token, FormatElement, FormatResult, | ||
Formatter, ToFormatElement, | ||
}; | ||
use rslint_parser::ast::JsExportNamedFromClause; | ||
use rslint_parser::AstSeparatedList; | ||
|
||
impl ToFormatElement for JsExportNamedFromClause { | ||
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> { | ||
let specifiers = self.specifiers(); | ||
let space = if specifiers.is_empty() { | ||
empty_element() | ||
} else { | ||
if_group_fits_on_single_line(space_token()) | ||
}; | ||
let list = group_elements(formatter.format_delimited( | ||
&self.l_curly_token()?, | ||
|leading, trailing| { | ||
Ok(format_elements!( | ||
space.clone(), | ||
soft_block_indent(format_elements![ | ||
leading, | ||
join_elements( | ||
soft_line_break_or_space(), | ||
formatter.format_separated(specifiers, || token(","))? | ||
), | ||
trailing, | ||
]), | ||
space, | ||
)) | ||
}, | ||
&self.r_curly_token()?, | ||
)?); | ||
|
||
let from = formatter.format_token(&self.from_token()?)?; | ||
let source = formatter.format_node(&self.source()?)?; | ||
let assertion = if let Some(assertion) = self.assertion() { | ||
formatter.format_node(&assertion)? | ||
} else { | ||
empty_element() | ||
}; | ||
let semicolon = if let Some(semicolon) = self.semicolon_token() { | ||
formatter.format_token(&semicolon)? | ||
} else { | ||
token(";") | ||
}; | ||
|
||
Ok(format_elements![ | ||
list, | ||
space_token(), | ||
from, | ||
space_token(), | ||
source, | ||
space_token(), | ||
assertion, | ||
semicolon | ||
]) | ||
} | ||
} |
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.
Nit: Should there be a helper for this common use case? If there isn't one already (CC: @leops )
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.
It could have been written as
formatter.format_token(&self.semicolon_token()).unwrap_or_else(|| token(";"))
but that's being deprecated in #2012 in favor ofself.semicolon_token().format_or(formatter, || token(";"))