feat(rome_formatter): `enum` formatting
DELETE
crates/rome_formatter/src/interpreter.rs
-use crate::{
- empty_element, format_elements, formatter_traits::FormatOptionalTokenAndNode, hard_line_break,
- FormatElement, FormatResult, Formatter,
-};
-use rslint_parser::SyntaxToken;
-
-pub(crate) fn format_interpreter(
- interpreter: Option<SyntaxToken>,
- formatter: &Formatter,
-) -> FormatResult<FormatElement> {
- interpreter.format_with_or(
- formatter,
- |interpreter| format_elements![interpreter, hard_line_break()],
- empty_element,
- )
-}

MODIFY
crates/rome_formatter/src/js/auxiliary/module.rs
crates/rome_formatter/src/js/auxiliary/module.rs
+use crate::utils::format_interpreter;
use crate::{
- format_elements, formatter_traits::FormatTokenAndNode, hard_line_break,
- interpreter::format_interpreter, FormatElement, FormatResult, Formatter, ToFormatElement,
+ format_elements, formatter_traits::FormatTokenAndNode, hard_line_break, FormatElement,
+ FormatResult, Formatter, ToFormatElement,
};
-
use rslint_parser::ast::JsModule;
impl ToFormatElement for JsModule {

MODIFY
crates/rome_formatter/src/js/auxiliary/script.rs
crates/rome_formatter/src/js/auxiliary/script.rs
+use crate::utils::format_interpreter;
use crate::{
- format_elements, formatter_traits::FormatTokenAndNode, hard_line_break,
- interpreter::format_interpreter, FormatElement, FormatResult, Formatter, ToFormatElement,
+ format_elements, formatter_traits::FormatTokenAndNode, hard_line_break, FormatElement,
+ FormatResult, Formatter, ToFormatElement,
};
use rslint_parser::ast::JsScript;

MODIFY
crates/rome_formatter/src/js/auxiliary/variable_declaration.rs
crates/rome_formatter/src/js/auxiliary/variable_declaration.rs
-use crate::formatter_traits::{FormatOptionalTokenAndNode, FormatTokenAndNode};
-
-use crate::{
- format_elements, space_token, FormatElement, FormatResult, Formatter, ToFormatElement,
-};
-
+use crate::formatter_traits::FormatTokenAndNode;
+use crate::utils::format_initializer_clause;
+use crate::{format_elements, FormatElement, FormatResult, Formatter, ToFormatElement};
use rslint_parser::ast::JsVariableDeclaration;
impl ToFormatElement for JsVariableDeclaration {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
- let initializer = self
- .initializer()
- .format_with_or_empty(formatter, |initializer| {
- format_elements![space_token(), initializer]
- })?;
+ let initializer = format_initializer_clause(formatter, self.initializer())?;
Ok(format_elements![self.id().format(formatter)?, initializer])
}

MODIFY
crates/rome_formatter/src/js/bindings/formal_parameter.rs
crates/rome_formatter/src/js/bindings/formal_parameter.rs
-use crate::formatter_traits::{FormatOptionalTokenAndNode, FormatTokenAndNode};
+use crate::formatter_traits::FormatTokenAndNode;
-use crate::{
- format_elements, space_token, FormatElement, FormatResult, Formatter, ToFormatElement,
-};
+use crate::{format_elements, FormatElement, FormatResult, Formatter, ToFormatElement};
+use crate::utils::format_initializer_clause;
use rslint_parser::ast::JsFormalParameter;
impl ToFormatElement for JsFormalParameter {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
- let initializer = self
- .initializer()
- .format_with_or_empty(formatter, |initializer| {
- format_elements![space_token(), initializer]
- })?;
+ let initializer = format_initializer_clause(formatter, self.initializer())?;
Ok(format_elements![
self.binding().format(formatter)?,

MODIFY
crates/rome_formatter/src/lib.rs
crates/rome_formatter/src/lib.rs
@@ -52,11 +52,11 @@ mod format_element;
mod format_elements;
mod formatter;
pub mod formatter_traits;
-pub(crate) mod interpreter;
mod intersperse;
mod js;
mod printer;
mod ts;
+mod utils;
pub use formatter::Formatter;
use rome_rowan::TextRange;

MODIFY
crates/rome_formatter/src/ts/auxiliary/enum_member.rs
crates/rome_formatter/src/ts/auxiliary/enum_member.rs
-use crate::{FormatElement, FormatResult, Formatter, ToFormatElement};
-use rslint_parser::{ast::TsEnumMember, AstNode};
+use crate::formatter_traits::FormatTokenAndNode;
+use crate::utils::format_initializer_clause;
+use crate::{format_elements, FormatElement, FormatResult, Formatter, ToFormatElement};
+use rslint_parser::ast::TsEnumMember;
+
impl ToFormatElement for TsEnumMember {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
- Ok(formatter.format_verbatim(self.syntax()))
+ let name = self.name().format(formatter)?;
+ let initializer = format_initializer_clause(formatter, self.initializer())?;
+
+ Ok(format_elements![name, initializer])
}
}

MODIFY
crates/rome_formatter/src/ts/statements/enum_statement.rs
crates/rome_formatter/src/ts/statements/enum_statement.rs
-use crate::{FormatElement, FormatResult, Formatter, ToFormatElement};
-use rslint_parser::{ast::TsEnumStatement, AstNode};
+use crate::formatter_traits::{FormatOptionalTokenAndNode, FormatTokenAndNode};
+use crate::{
+ format_elements, group_elements, join_elements, soft_block_indent, soft_line_break_or_space,
+ space_token, token, FormatElement, FormatResult, Formatter, ToFormatElement,
+};
+use rslint_parser::ast::TsEnumStatement;
+
impl ToFormatElement for TsEnumStatement {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
- Ok(formatter.format_verbatim(self.syntax()))
+ let const_token = self
+ .const_token()
+ .format_with_or_empty(formatter, |const_token| {
+ format_elements![const_token, space_token()]
+ })?;
+ let enum_token = self.enum_token().format_with(formatter, |enum_token| {
+ format_elements![enum_token, space_token()]
+ })?;
+ let id = self
+ .id()
+ .format_with(formatter, |id| format_elements![id, space_token()])?;
+
+ let members = formatter.format_separated(self.members(), || token(","))?;
+ let list = group_elements(formatter.format_delimited(
+ &self.l_curly_token()?,
+ |open_token_trailing, close_token_leading| {
+ Ok(format_elements![
+ soft_line_break_or_space(),
+ soft_block_indent(format_elements![
+ open_token_trailing,
+ join_elements(soft_line_break_or_space(), members),
+ close_token_leading,
+ ]),
+ soft_line_break_or_space(),
+ ])
+ },
+ &self.r_curly_token()?,
+ )?);
+
+ Ok(format_elements![const_token, enum_token, id, list])
}
}

ADD
crates/rome_formatter/src/utils.rs
+use crate::formatter_traits::FormatOptionalTokenAndNode;
+use crate::{
+ empty_element, format_elements, hard_line_break, space_token, FormatElement, FormatResult,
+ Formatter,
+};
+use rslint_parser::ast::JsInitializerClause;
+use rslint_parser::SyntaxToken;
+
+/// Utility function to format the node [rslint_parser::ast::JsInitializerClause]
+pub(crate) fn format_initializer_clause(
+ formatter: &Formatter,
+ initializer: Option<JsInitializerClause>,
+) -> FormatResult<FormatElement> {
+ initializer.format_with_or_empty(formatter, |initializer| {
+ format_elements![space_token(), initializer]
+ })
+}
+
+pub(crate) fn format_interpreter(
+ interpreter: Option<SyntaxToken>,
+ formatter: &Formatter,
+) -> FormatResult<FormatElement> {
+ interpreter.format_with_or(
+ formatter,
+ |interpreter| format_elements![interpreter, hard_line_break()],
+ empty_element,
+ )
+}

ADD
crates/rome_formatter/tests/specs/ts/statement/enum_statement.ts
+enum A {}
+enum B {a = "something",
+b = "something", c = "something",
+d = "something",
+e = "something", f = "something",
+}
+
+const enum C {
+ A, B, C,
+ D
+ ,
+ F
+}
\ No newline at end of file

ADD
crates/rome_formatter/tests/specs/ts/statement/enum_statement.ts.snap
+---
+source: crates/rome_formatter/tests/spec_test.rs
+assertion_line: 163
+expression: enum_statement.ts
+
+---
+# Input
+enum A {}
+enum B {a = "something",
+b = "something", c = "something",
+d = "something",
+e = "something", f = "something",
+}
+
+const enum C {
+ A, B, C,
+ D
+ ,
+ F
+}
+=============================
+# Outputs
+## Output 1
+-----
+Indent style: Tab
+Line width: 80
+-----
+enum A { }
+enum B {
+ a = "something",
+ b = "something",
+ c = "something",
+ d = "something",
+ e = "something",
+ f = "something",
+}
+
+const enum C { A, B, C, D, F }
+

feat(rome_formatter): `enum` formatting (#2075)