fix(rome_formatter): various minor corrections to the formatter
MODIFY
crates/rome_formatter/src/format_element.rs
crates/rome_formatter/src/format_element.rs
@@ -349,9 +349,11 @@ where
}
concat_elements(IntersperseFn::new(
- elements.into_iter().filter(|(_, e)| !e.is_empty()),
- |prev_node, next_node| {
- if get_lines_between_nodes(prev_node.syntax(), next_node.syntax()) > 1 {
+ elements.into_iter(),
+ |prev_node, next_node, next_elem| {
+ if next_elem.is_empty() {
+ empty_element()
+ } else if get_lines_between_nodes(prev_node.syntax(), next_node.syntax()) > 1 {
empty_line()
} else {
separator()

MODIFY
crates/rome_formatter/src/intersperse.rs
crates/rome_formatter/src/intersperse.rs
@@ -97,7 +97,7 @@ where
impl<I, F, N> IntersperseFn<I, F, N>
where
I: Iterator<Item = (N, FormatElement)>,
- F: FnMut(&N, &N) -> FormatElement,
+ F: FnMut(&N, &N, &FormatElement) -> FormatElement,
{
pub fn new(iter: I, separator_factory: F) -> Self {
Self {
@@ -111,15 +111,15 @@ where
impl<I, F, N> Iterator for IntersperseFn<I, F, N>
where
I: Iterator<Item = (N, FormatElement)>,
- F: FnMut(&N, &N) -> FormatElement,
+ F: FnMut(&N, &N, &FormatElement) -> FormatElement,
{
type Item = FormatElement;
#[inline]
fn next(&mut self) -> Option<FormatElement> {
if let Some(prev_node) = self.prev_item.take() {
- if let Some((next_node, _)) = self.iter.peek() {
- return Some((self.separator_factory)(&prev_node, next_node));
+ if let Some((next_node, next_elem)) = self.iter.peek() {
+ return Some((self.separator_factory)(&prev_node, next_node, next_elem));
}
}
@@ -147,6 +147,6 @@ where
impl<I, F, N> ExactSizeIterator for IntersperseFn<I, F, N>
where
I: Iterator<Item = (N, FormatElement)>,
- F: FnMut(&N, &N) -> FormatElement,
+ F: FnMut(&N, &N, &FormatElement) -> FormatElement,
{
}

MODIFY
crates/rome_formatter/src/ts/auxiliary/function_body.rs
crates/rome_formatter/src/ts/auxiliary/function_body.rs
use crate::{
- block_indent, format_elements, FormatElement, FormatResult, Formatter, ToFormatElement,
+ block_indent, format_elements, formatter_traits::FormatTokenAndNode, FormatElement,
+ FormatResult, Formatter, ToFormatElement,
};
use rslint_parser::ast::JsFunctionBody;
@@ -10,6 +11,7 @@ impl ToFormatElement for JsFunctionBody {
|open_token_trailing, close_token_leading| {
Ok(block_indent(format_elements![
open_token_trailing,
+ self.directives().format(formatter)?,
formatter.format_list(self.statements()),
close_token_leading,
]))

MODIFY
crates/rome_formatter/src/ts/class/getter_class_member.rs
crates/rome_formatter/src/ts/class/getter_class_member.rs
-use crate::formatter_traits::FormatTokenAndNode;
+use crate::formatter_traits::{FormatOptionalTokenAndNode, FormatTokenAndNode};
use crate::{
format_elements, space_token, FormatElement, FormatResult, Formatter, ToFormatElement,
};
@@ -7,6 +7,8 @@ use rslint_parser::ast::JsGetterClassMember;
impl ToFormatElement for JsGetterClassMember {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
Ok(format_elements![
+ self.static_token()
+ .format_with_or_empty(formatter, |token| format_elements![token, space_token()])?,
self.get_token().format(formatter)?,
space_token(),
self.name().format(formatter)?,

MODIFY
crates/rome_formatter/src/ts/class/method_class_member.rs
crates/rome_formatter/src/ts/class/method_class_member.rs
@@ -6,6 +6,9 @@ use rslint_parser::ast::JsMethodClassMember;
impl ToFormatElement for JsMethodClassMember {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
+ let async_token = self
+ .async_token()
+ .format_with_or_empty(formatter, |token| format_elements![token, space_token()])?;
let static_token = self
.static_token()
.format_with_or_empty(formatter, |token| format_elements![token, space_token()])?;
@@ -15,6 +18,7 @@ impl ToFormatElement for JsMethodClassMember {
let body = self.body().format(formatter)?;
Ok(format_elements![
static_token,
+ async_token,
star_token,
name,
params,

MODIFY
crates/rome_formatter/src/ts/statements/for_of_statement.rs
crates/rome_formatter/src/ts/statements/for_of_statement.rs
use rslint_parser::ast::JsForOfStatement;
-use crate::formatter_traits::FormatTokenAndNode;
+use crate::formatter_traits::{FormatOptionalTokenAndNode, FormatTokenAndNode};
+
use crate::{
format_elements, group_elements, soft_block_indent, soft_line_break_or_space, space_token,
FormatElement, FormatResult, Formatter, ToFormatElement,
@@ -9,6 +10,9 @@ use crate::{
impl ToFormatElement for JsForOfStatement {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
let for_token = self.for_token().format(formatter)?;
+ let await_token = self
+ .await_token()
+ .format_with_or_empty(formatter, |token| format_elements![token, space_token()])?;
let initializer = self.initializer().format(formatter)?;
let of_token = self.of_token().format(formatter)?;
let expression = self.expression().format(formatter)?;
@@ -17,6 +21,7 @@ impl ToFormatElement for JsForOfStatement {
Ok(format_elements![
for_token,
space_token(),
+ await_token,
formatter.format_delimited(
&self.l_paren_token()?,
|open_token_trailing, close_token_leading| Ok(group_elements(soft_block_indent(

MODIFY
crates/rome_formatter/tests/specs/js/module/class/class.js
crates/rome_formatter/tests/specs/js/module/class/class.js
@@ -6,6 +6,10 @@ class Foo extends Boar {
super();
}
+ static get sg() {
+
+ }
+
get g() {
}
@@ -14,18 +18,28 @@ class Foo extends Boar {
}
- * generator (){}
-
- lorem() {
+ method() {
return "ipsum";
}
- static foo;
+ async asyncMethod() {}
+
+ * generatorMethod (){}
- static bar() {
+ static staticMethod() {
return "bar"
}
+ async * asyncGeneratorMethod (){}
+
+ static async staticAsyncMethod (){}
+
+ static * staticGeneratorMethod (){}
+
+ static async *staticAsyncGeneratorMethod() {}
+
+ static foo;
+
new_prop = 5
#a = b

MODIFY
crates/rome_formatter/tests/specs/js/module/class/class.js.snap
crates/rome_formatter/tests/specs/js/module/class/class.js.snap
@@ -13,6 +13,10 @@ class Foo extends Boar {
super();
}
+ static get sg() {
+
+ }
+
get g() {
}
@@ -21,18 +25,28 @@ class Foo extends Boar {
}
- * generator (){}
-
- lorem() {
+ method() {
return "ipsum";
}
- static foo;
+ async asyncMethod() {}
+
+ * generatorMethod (){}
- static bar() {
+ static staticMethod() {
return "bar"
}
+ async * asyncGeneratorMethod (){}
+
+ static async staticAsyncMethod (){}
+
+ static * staticGeneratorMethod (){}
+
+ static async *staticAsyncGeneratorMethod() {}
+
+ static foo;
+
new_prop = 5
#a = b
@@ -64,22 +78,34 @@ class Foo extends Boar {
super();
}
+ static get sg() {}
+
get g() {}
set gg(a) {}
- *generator() {}
-
- lorem() {
+ method() {
return "ipsum";
}
- static foo;
+ async asyncMethod() {}
+
+ *generatorMethod() {}
- static bar() {
+ static staticMethod() {
return "bar";
}
+ async *asyncGeneratorMethod() {}
+
+ static async staticAsyncMethod() {}
+
+ static *staticGeneratorMethod() {}
+
+ static async *staticAsyncGeneratorMethod() {}
+
+ static foo;
+
new_prop = 5;
#a = b;

MODIFY
crates/rome_formatter/tests/specs/js/module/function/function.js
crates/rome_formatter/tests/specs/js/module/function/function.js
@@ -26,3 +26,7 @@ function foo() {
return ref;
}
+
+function directives() {
+ "use strict";
+}

MODIFY
crates/rome_formatter/tests/specs/js/module/function/function.js.snap
crates/rome_formatter/tests/specs/js/module/function/function.js.snap
---
source: crates/rome_formatter/tests/spec_test.rs
-assertion_line: 43
+assertion_line: 57
expression: function.js
---
@@ -34,6 +34,10 @@ function foo() {
return ref;
}
+function directives() {
+ "use strict";
+}
+
---
# Output
function foo() {}
@@ -65,3 +69,7 @@ function foo() {
return ref;
}
+function directives() {
+ "use strict";
+}
+

MODIFY
crates/rome_formatter/tests/specs/js/module/newlines.js
crates/rome_formatter/tests/specs/js/module/newlines.js
@@ -18,6 +18,10 @@ statement();
statement();
+;
+
+
+
switch(a) {
case 1:
break;

MODIFY
crates/rome_formatter/tests/specs/js/module/newlines.js.snap
crates/rome_formatter/tests/specs/js/module/newlines.js.snap
@@ -25,6 +25,10 @@ statement();
statement();
+;
+
+
+
switch(a) {
case 1:
break;

MODIFY
crates/rome_formatter/tests/specs/js/module/statement/for_of.js
crates/rome_formatter/tests/specs/js/module/statement/for_of.js
@@ -4,3 +4,5 @@ for (let a of b) {}
for (const aVeryLongVariableNameToEnforceLineBreaksaVeryLongVariableNameToEnforceLineBreaks of aVeryLongVariableNameToEnforceLineBreaksaVeryLongVariableNameToEnforceLineBreaks) {
}
+
+for await ( const a of b ) {}

MODIFY
crates/rome_formatter/tests/specs/js/module/statement/for_of.js.snap
crates/rome_formatter/tests/specs/js/module/statement/for_of.js.snap
---
source: crates/rome_formatter/tests/spec_test.rs
-assertion_line: 43
+assertion_line: 57
expression: for_of.js
---
@@ -12,6 +12,8 @@ for (let a of b) {}
for (const aVeryLongVariableNameToEnforceLineBreaksaVeryLongVariableNameToEnforceLineBreaks of aVeryLongVariableNameToEnforceLineBreaksaVeryLongVariableNameToEnforceLineBreaks) {
}
+for await ( const a of b ) {}
+
---
# Output
for (a of b) {}
@@ -24,3 +26,5 @@ for (
aVeryLongVariableNameToEnforceLineBreaksaVeryLongVariableNameToEnforceLineBreaks
) {}
+for await (const a of b) {}
+

fix(rome_formatter): various minor corrections to the formatter (#2052)