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

Commit

Permalink
refactor(rome_js_formatter): Move node formatting into its own trait
Browse files Browse the repository at this point in the history
Today, the formatting that must happen for each JS node happens inside `Formatter.format_syntax_node` and the `formatter_traits`.

This PR moves the common logic applied by all nodes and tokens into the `ToFormatElement` implementation and renames it to `Format`.

* Rename `ToFormatElement` to `Format` and `to_format_element` to `format`
* Implement `Format` for `JsSyntaxToken`. CSS, JSON etc. can implement their own generic token formatting logic
* Introduce a new `FormatNode` trait (language specific) that implements the formatting of a node and calls into `format_fields` that is implemented for each field.
* Make the `FormatterTraits` node and language independent. Rename to `FormatTraits`.

`Format`, `FormatterTraits`, `FormatResult`, and `FormatError` are now all language independent. However, some still depend on the `Formatter`.

The next step is to move many of the `Formatter`'s helper function out of the Formatter so that it becomes language independent.
  • Loading branch information
MichaReiser committed Apr 21, 2022
1 parent 6cdcad6 commit 451029e
Show file tree
Hide file tree
Showing 401 changed files with 3,400 additions and 1,964 deletions.
8 changes: 4 additions & 4 deletions crates/rome_js_formatter/src/cst.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{FormatElement, FormatResult, Formatter, ToFormatElement};
use crate::{Format, FormatElement, FormatResult, Formatter};
use rome_js_syntax::{map_syntax_node, JsSyntaxNode};

impl ToFormatElement for JsSyntaxNode {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
map_syntax_node!(self.clone(), node => node.to_format_element(formatter))
impl Format for JsSyntaxNode {
fn format(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
map_syntax_node!(self.clone(), node => node.format(formatter))
}
}
Loading

0 comments on commit 451029e

Please sign in to comment.