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 657
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rome_js_formatter): Template formatting
This PR improves Rome's formatting of `JsTemplate`s and `TsTemplate`s to closer match Prettier's formatting. It mainly implements: * simple expressions that never break even if the template literal, as a result thereof, exceeds the line width * Aligning expressions in template literals with the last template chunk This PR does not implement Prettier's custom formatting of `Jest` specs, and it doesn't implement custom comments formatting. ## Tests I manually verified the snapshot changes. There are some remaining differences but they are rooted in the fact that some, expression formatting isn't compatible with prettier yet (mainly binary expression, call arguments)
- Loading branch information
1 parent
1431af0
commit 6480ecf
Showing
38 changed files
with
904 additions
and
1,269 deletions.
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
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
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
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,32 +1,79 @@ | ||
use crate::prelude::*; | ||
use rome_formatter::write; | ||
|
||
use rome_js_syntax::JsTemplate; | ||
use rome_js_syntax::JsTemplateFields; | ||
use rome_js_syntax::{ | ||
JsAnyExpression, JsSyntaxToken, JsTemplate, TsTemplateLiteralType, TsTypeArguments, | ||
}; | ||
use rome_rowan::{declare_node_union, SyntaxResult}; | ||
|
||
#[derive(Debug, Clone, Default)] | ||
pub struct FormatJsTemplate; | ||
|
||
impl FormatNodeRule<JsTemplate> for FormatJsTemplate { | ||
fn fmt_fields(&self, node: &JsTemplate, f: &mut JsFormatter) -> FormatResult<()> { | ||
let JsTemplateFields { | ||
tag, | ||
type_arguments, | ||
l_tick_token, | ||
elements, | ||
r_tick_token, | ||
} = node.as_fields(); | ||
|
||
write![ | ||
JsAnyTemplate::from(node.clone()).fmt(f) | ||
} | ||
} | ||
|
||
declare_node_union! { | ||
JsAnyTemplate = JsTemplate | TsTemplateLiteralType | ||
} | ||
|
||
impl Format<JsFormatContext> for JsAnyTemplate { | ||
fn fmt(&self, f: &mut Formatter<JsFormatContext>) -> FormatResult<()> { | ||
write!( | ||
f, | ||
[ | ||
tag.format(), | ||
type_arguments.format(), | ||
self.tag().format(), | ||
self.type_arguments().format(), | ||
line_suffix_boundary(), | ||
l_tick_token.format(), | ||
elements.format(), | ||
r_tick_token.format() | ||
self.l_tick_token().format(), | ||
] | ||
] | ||
)?; | ||
|
||
self.write_elements(f)?; | ||
|
||
write!(f, [self.r_tick_token().format()]) | ||
} | ||
} | ||
|
||
impl JsAnyTemplate { | ||
fn tag(&self) -> Option<JsAnyExpression> { | ||
match self { | ||
JsAnyTemplate::JsTemplate(template) => template.tag(), | ||
JsAnyTemplate::TsTemplateLiteralType(_) => None, | ||
} | ||
} | ||
|
||
fn type_arguments(&self) -> Option<TsTypeArguments> { | ||
match self { | ||
JsAnyTemplate::JsTemplate(template) => template.type_arguments(), | ||
JsAnyTemplate::TsTemplateLiteralType(_) => None, | ||
} | ||
} | ||
|
||
fn l_tick_token(&self) -> SyntaxResult<JsSyntaxToken> { | ||
match self { | ||
JsAnyTemplate::JsTemplate(template) => template.l_tick_token(), | ||
JsAnyTemplate::TsTemplateLiteralType(template) => template.l_tick_token(), | ||
} | ||
} | ||
|
||
fn write_elements(&self, f: &mut JsFormatter) -> FormatResult<()> { | ||
match self { | ||
JsAnyTemplate::JsTemplate(template) => { | ||
write!(f, [template.elements().format()]) | ||
} | ||
JsAnyTemplate::TsTemplateLiteralType(template) => { | ||
write!(f, [template.elements().format()]) | ||
} | ||
} | ||
} | ||
|
||
fn r_tick_token(&self) -> SyntaxResult<JsSyntaxToken> { | ||
match self { | ||
JsAnyTemplate::JsTemplate(template) => template.r_tick_token(), | ||
JsAnyTemplate::TsTemplateLiteralType(template) => template.r_tick_token(), | ||
} | ||
} | ||
} |
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
Oops, something went wrong.