forked from rome/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
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 (rome#3063)
- Loading branch information
1 parent
21d5f54
commit e7d05bc
Showing
38 changed files
with
956 additions
and
1,272 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.