Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
chore: extract logic to a new API
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Jan 24, 2022
1 parent e557cb4 commit cbf1cb6
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 92 deletions.
31 changes: 28 additions & 3 deletions crates/rome_formatter/src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use std::collections::HashSet;
use crate::printer::Printer;
use crate::{
concat_elements, empty_element, format_elements, hard_line_break, if_group_breaks,
if_group_fits_on_single_line, line_suffix, space_token, token, FormatElement, FormatOptions,
FormatResult, Formatted, ToFormatElement,
if_group_fits_on_single_line, join_elements, line_suffix, space_token, token, FormatElement,
FormatOptions, FormatResult, Formatted, ToFormatElement,
};
use rome_rowan::api::SyntaxTriviaPieceComments;
use rome_rowan::{Language, SyntaxElement};
#[cfg(debug_assertions)]
use rslint_parser::SyntaxNodeExt;
use rslint_parser::{AstNode, AstSeparatedList, SyntaxNode, SyntaxToken};
use rslint_parser::{AstNode, AstNodeList, AstSeparatedList, SyntaxNode, SyntaxToken};

/// Handles the formatting of a CST and stores the options how the CST should be formatted (user preferences).
/// The formatter is passed to the [ToFormatElement] implementation of every node in the CST so that they
Expand Down Expand Up @@ -261,6 +261,31 @@ impl Formatter {
Ok(result.into_iter())
}

/// It formats a list of nodes that are not separated.
pub fn format_list<List, Node: Clone + AstNode + ToFormatElement>(
&self,
list: List,
) -> FormatElement
where
List: AstNodeList<Node>,
{
join_elements(
hard_line_break(),
list.iter().map(|module_item| {
let snapshot = self.snapshot();
match self.format_node(module_item.clone()) {
Ok(result) => result,
Err(_) => {
self.restore(snapshot);
self.format_verbatim(module_item.syntax())
.trim_start()
.trim_end()
}
}
}),
)
}

fn print_leading_trivia(&self, token: &SyntaxToken) -> FormatElement {
// False positive: the trivias need to be collected in a vector as they
// are iterated on in reverse order later, but SyntaxTriviaPiecesIterator
Expand Down
6 changes: 2 additions & 4 deletions crates/rome_formatter/src/ts/auxiliary/function_body.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use rslint_parser::ast::JsFunctionBody;

use crate::ts::statements::format_statements;
use crate::{
block_indent, format_elements, FormatElement, FormatResult, Formatter, ToFormatElement,
};
use rslint_parser::ast::JsFunctionBody;

impl ToFormatElement for JsFunctionBody {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
Expand All @@ -12,7 +10,7 @@ impl ToFormatElement for JsFunctionBody {
|leading, trailing| {
Ok(block_indent(format_elements![
leading,
format_statements(self.statements(), formatter),
formatter.format_list(self.statements()),
trailing,
]))
},
Expand Down
44 changes: 24 additions & 20 deletions crates/rome_formatter/src/ts/root/mod.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
use crate::{hard_line_break, join_elements, FormatElement, Formatter};
use rslint_parser::ast::JsModuleItemList;
use rslint_parser::AstNode;
use crate::ts::directives::format_directives;
use crate::{
empty_element, format_elements, hard_line_break, FormatElement, FormatResult, Formatter,
};
use rslint_parser::ast::JsDirectiveList;
use rslint_parser::AstNodeList;
use rslint_parser::SyntaxToken;

mod any_module_item;
mod module;
mod script;

pub fn format_module_item_list(list: JsModuleItemList, formatter: &Formatter) -> FormatElement {
join_elements(
hard_line_break(),
list.iter().map(|module_item| {
let snapshot = formatter.snapshot();
match formatter.format_node(module_item.clone()) {
Ok(result) => result,
Err(_) => {
formatter.restore(snapshot);
formatter
.format_verbatim(module_item.syntax())
.trim_start()
.trim_end()
}
}
}),
)
pub fn format_directives_list(directives: JsDirectiveList, formatter: &Formatter) -> FormatElement {
if directives.len() > 0 {
format_elements![format_directives(directives, formatter), hard_line_break()]
} else {
empty_element()
}
}

pub fn format_interpreter(
interpreter: Option<SyntaxToken>,
formatter: &Formatter,
) -> FormatResult<FormatElement> {
let result = if let Some(interpreter) = interpreter {
format_elements![formatter.format_token(&interpreter)?, hard_line_break()]
} else {
empty_element()
};
Ok(result)
}
20 changes: 4 additions & 16 deletions crates/rome_formatter/src/ts/root/module.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
use crate::ts::directives::format_directives;
use crate::ts::root::format_module_item_list;
use crate::ts::root::{format_directives_list, format_interpreter};
use crate::{
format_elements, hard_line_break, FormatElement, FormatResult, Formatter, ToFormatElement,
};
use rslint_parser::ast::JsModule;
use rslint_parser::AstNodeList;

impl ToFormatElement for JsModule {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
let mut elements = vec![];

if let Some(interpreter) = self.interpreter_token() {
elements.push(formatter.format_token(&interpreter)?);
elements.push(hard_line_break());
}

let directives = self.directives();
if directives.len() > 0 {
elements.push(format_directives(directives, formatter));
elements.push(hard_line_break());
}

elements.push(format_module_item_list(self.items(), formatter));

elements.push(format_interpreter(self.interpreter_token(), formatter)?);
elements.push(format_directives_list(self.directives(), formatter));
elements.push(formatter.format_list(self.items()));
elements.push(formatter.format_token(&self.eof_token()?)?);

Ok(format_elements![
Expand Down
21 changes: 4 additions & 17 deletions crates/rome_formatter/src/ts/root/script.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,15 @@
use crate::ts::directives::format_directives;
use crate::ts::statements::format_statements;
use crate::ts::root::{format_directives_list, format_interpreter};
use crate::{
format_elements, hard_line_break, FormatElement, FormatResult, Formatter, ToFormatElement,
};
use rslint_parser::ast::JsScript;
use rslint_parser::AstNodeList;

impl ToFormatElement for JsScript {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
let mut elements = vec![];

if let Some(interpreter) = self.interpreter_token() {
elements.push(formatter.format_token(&interpreter)?);
elements.push(hard_line_break());
}

let directives = self.directives();
if directives.len() > 0 {
elements.push(format_directives(directives, formatter));
elements.push(hard_line_break());
}

elements.push(format_statements(self.statements(), formatter));

elements.push(format_interpreter(self.interpreter_token(), formatter)?);
elements.push(format_directives_list(self.directives(), formatter));
elements.push(formatter.format_list(self.statements()));
elements.push(formatter.format_token(&self.eof_token()?)?);

Ok(format_elements![
Expand Down
8 changes: 3 additions & 5 deletions crates/rome_formatter/src/ts/statements/block.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use rslint_parser::ast::JsBlockStatement;
use rslint_parser::{AstNode, AstNodeList, JsSyntaxKind};

use crate::ts::statements::format_statements;
use crate::{
block_indent, format_elements, hard_line_break, FormatElement, FormatResult, Formatter,
ToFormatElement,
};
use rslint_parser::ast::JsBlockStatement;
use rslint_parser::{AstNode, AstNodeList, JsSyntaxKind};

impl ToFormatElement for JsBlockStatement {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
let stmts = format_statements(self.statements(), formatter);
let stmts = formatter.format_list(self.statements());

if is_non_collapsable_empty_block(self) {
Ok(format_elements![
Expand Down
24 changes: 0 additions & 24 deletions crates/rome_formatter/src/ts/statements/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
use crate::{hard_line_break, join_elements, FormatElement, Formatter};
use rslint_parser::ast::{AstNodeList, JsStatementList};
use rslint_parser::AstNode;

mod block;
mod break_statement;
mod continue_statement;
Expand All @@ -23,23 +19,3 @@ mod try_statement;
mod variable_declaration_statement;
mod while_statement;
mod with_statement;

/// Formats a list of statements
pub fn format_statements(stmts: JsStatementList, formatter: &Formatter) -> FormatElement {
join_elements(
hard_line_break(),
stmts.iter().map(|stmt| {
let snapshot = formatter.snapshot();
match formatter.format_node(stmt.clone()) {
Ok(result) => result,
Err(_) => {
formatter.restore(snapshot);
formatter
.format_verbatim(stmt.syntax())
.trim_start()
.trim_end()
}
}
}),
)
}
5 changes: 2 additions & 3 deletions crates/rome_formatter/src/ts/statements/switch_statement.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::ts::statements::format_statements;
use crate::{block_indent, FormatResult};
use crate::{
format_element::indent, format_elements, group_elements, hard_line_break, join_elements,
Expand Down Expand Up @@ -53,7 +52,7 @@ impl ToFormatElement for JsDefaultClause {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
let default = formatter.format_token(&self.default_token()?)?;
let colon = formatter.format_token(&self.colon_token()?)?;
let statements = format_statements(self.consequent(), formatter);
let statements = formatter.format_list(self.consequent());

Ok(format_elements![
default,
Expand All @@ -72,7 +71,7 @@ impl ToFormatElement for JsCaseClause {

let test = formatter.format_node(self.test()?)?;

let cons = format_statements(self.consequent(), formatter);
let cons = formatter.format_list(self.consequent());

Ok(format_elements![
case_word,
Expand Down

0 comments on commit cbf1cb6

Please sign in to comment.