Skip to content

Commit

Permalink
feat(rome_formatter): tag_expression (rome#2262)
Browse files Browse the repository at this point in the history
the simplest tag_expression (that is not self closing) to format is the smallest possible tree that
needs no formatting. for example `<a></a>`.
  • Loading branch information
darrow-olykos committed Mar 22, 2022
1 parent 76e74c9 commit 8fd9d42
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 18 deletions.
10 changes: 7 additions & 3 deletions crates/rome_formatter/src/jsx/auxiliary/name.rs
@@ -1,7 +1,11 @@
use crate::{FormatElement, FormatResult, Formatter, ToFormatElement};
use rome_js_syntax::{AstNode, JsxName};
use crate::{
formatter_traits::FormatTokenAndNode, FormatElement, FormatResult, Formatter, ToFormatElement,
};
use rome_js_syntax::{AstNode, JsxName, JsxNameFields};
impl ToFormatElement for JsxName {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
Ok(formatter.format_verbatim(self.syntax()))
let JsxNameFields { value_token } = self.as_fields();

value_token.format(formatter)
}
}
2 changes: 1 addition & 1 deletion crates/rome_formatter/src/jsx/auxiliary/text.rs
@@ -1,4 +1,4 @@
use crate::{FormatElement, FormatResult, Formatter, ToFormatElement};
use crate::{FormatElement, FormatResult, Formatter, ToFormatElement, token};
use rome_js_syntax::{AstNode, JsxText};
impl ToFormatElement for JsxText {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
Expand Down
7 changes: 4 additions & 3 deletions crates/rome_formatter/src/jsx/expressions/tag_expression.rs
@@ -1,7 +1,8 @@
use crate::{FormatElement, FormatResult, Formatter, ToFormatElement};
use rome_js_syntax::{AstNode, JsxTagExpression};
use crate::{format_elements, token, FormatElement, FormatResult, Formatter, ToFormatElement};
use rome_js_syntax::{AstNode, JsxTagExpression, JsxTagExpressionFields};
impl ToFormatElement for JsxTagExpression {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
Ok(formatter.format_verbatim(self.syntax()))
let JsxTagExpressionFields { tag } = self.as_fields();
Ok(tag?.to_format_element(formatter)?)
}
}
21 changes: 18 additions & 3 deletions crates/rome_formatter/src/jsx/tag/closing_element.rs
@@ -1,7 +1,22 @@
use crate::{FormatElement, FormatResult, Formatter, ToFormatElement};
use rome_js_syntax::{AstNode, JsxClosingElement};
use crate::{
format_elements, formatter_traits::FormatTokenAndNode, FormatElement, FormatResult, Formatter,
ToFormatElement,
};
use rome_js_syntax::{AstNode, JsxClosingElement, JsxClosingElementFields};
impl ToFormatElement for JsxClosingElement {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
Ok(formatter.format_verbatim(self.syntax()))
let JsxClosingElementFields {
l_angle_token,
slash_token,
name,
r_angle_token,
} = self.as_fields();

Ok(format_elements![
l_angle_token.format(formatter)?,
slash_token.format(formatter)?,
name?.to_format_element(formatter)?,
r_angle_token.format(formatter)?
])
}
}
16 changes: 13 additions & 3 deletions crates/rome_formatter/src/jsx/tag/element.rs
@@ -1,7 +1,17 @@
use crate::{FormatElement, FormatResult, Formatter, ToFormatElement};
use rome_js_syntax::{AstNode, JsxElement};
use crate::{format_elements, token, FormatElement, FormatResult, Formatter, ToFormatElement};
use rome_js_syntax::{AstNode, JsxElement, JsxElementFields};
impl ToFormatElement for JsxElement {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
Ok(formatter.format_verbatim(self.syntax()))
let JsxElementFields {
opening_element,
children,
closing_element,
} = self.as_fields();

Ok(format_elements![
opening_element?.to_format_element(formatter)?,
formatter.format_list(children),
closing_element?.to_format_element(formatter)?
])
}
}
21 changes: 18 additions & 3 deletions crates/rome_formatter/src/jsx/tag/opening_element.rs
@@ -1,7 +1,22 @@
use crate::{FormatElement, FormatResult, Formatter, ToFormatElement};
use rome_js_syntax::{AstNode, JsxOpeningElement};
use crate::{
format_elements, formatter_traits::FormatTokenAndNode, FormatElement, FormatResult, Formatter,
ToFormatElement,
};
use rome_js_syntax::{AstNode, JsxOpeningElement, JsxOpeningElementFields};
impl ToFormatElement for JsxOpeningElement {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
Ok(formatter.format_verbatim(self.syntax()))
let JsxOpeningElementFields {
l_angle_token,
name,
attributes,
r_angle_token,
} = self.as_fields();

Ok(format_elements![
l_angle_token.format(formatter)?,
name?.to_format_element(formatter)?,
formatter.format_list(attributes),
r_angle_token.format(formatter)?
])
}
}
6 changes: 4 additions & 2 deletions crates/rome_formatter/src/lib.rs
Expand Up @@ -596,9 +596,11 @@ mod test {
// use this test check if your snippet prints as you wish, without using a snapshot
fn quick_test() {
let src = r#"
3 + 3 === 7
<Foo>
{abc}
</Foo>
"#;
let syntax = SourceType::ts();
let syntax = SourceType::jsx();
let tree = parse(src, 0, syntax.clone());
let result = format(FormatOptions::default(), &tree.syntax()).unwrap();
check_reformat(CheckReformatParams {
Expand Down
@@ -0,0 +1 @@
<a></a>
@@ -0,0 +1,17 @@
---
source: crates/rome_formatter/tests/spec_test.rs
assertion_line: 197
expression: tag_expression.jsx

---
# Input
<a></a>
=============================
# Outputs
## Output 1
-----
Indent style: Tab
Line width: 80
-----
<a></a>;

1 comment on commit 8fd9d42

@darrow-olykos
Copy link
Author

@darrow-olykos darrow-olykos commented on 8fd9d42 Mar 22, 2022

Choose a reason for hiding this comment

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

  • use .format or .to_format_element in appropriate places

.to_format_element calls the ToFormatElement implementation of that Node or Token. .format ensures that .to_format_element doesn't get called if formatting of that node is disabled with a suppression comment. It also is more convenient if you call it on an Option where you can't use the try operator because the to_format_element method must return a FormatResult

Please sign in to comment.