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

fix(rome_js_formatter): 🐛 preserve new-lines after directives #2500

Merged
merged 4 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions crates/rome_formatter/src/format_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -819,30 +819,30 @@ where
join_elements_with(elements, hard_line_break)
}

/// Get the number of line breaks between two consecutive SyntaxNodes in the tree
pub fn get_lines_before<L: Language>(next_node: &SyntaxNode<L>) -> usize {
// Count the newlines in the leading trivia of the next node
if let Some(leading_trivia) = next_node.first_leading_trivia() {
leading_trivia
.pieces()
.take_while(|piece| {
// Stop at the first comment piece, the comment printer
// will handle newlines between the comment and the node
!piece.is_comments()
})
.filter(|piece| piece.is_newline())
.count()
} else {
0
}
}

#[inline]
pub fn join_elements_with<I, L>(elements: I, separator: fn() -> FormatElement) -> FormatElement
where
I: IntoIterator<Item = (SyntaxNode<L>, FormatElement)>,
L: Language,
{
/// Get the number of line breaks between two consecutive SyntaxNodes in the tree
fn get_lines_before<L: Language>(next_node: &SyntaxNode<L>) -> usize {
// Count the newlines in the leading trivia of the next node
if let Some(leading_trivia) = next_node.first_leading_trivia() {
leading_trivia
.pieces()
.take_while(|piece| {
// Stop at the first comment piece, the comment printer
// will handle newlines between the comment and the node
!piece.is_comments()
})
.filter(|piece| piece.is_newline())
.count()
} else {
0
}
}

concat_elements(IntersperseFn::new(
elements.into_iter(),
|_, next_node, next_elem| {
Expand Down
28 changes: 25 additions & 3 deletions crates/rome_js_formatter/src/js/lists/directive_list.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
use crate::{empty_element, format_elements, hard_line_break, Format, FormatElement, Formatter};
use rome_formatter::FormatResult;
use rome_formatter::{empty_line, format_element::get_lines_before, FormatResult};
use rome_js_syntax::JsDirectiveList;
use rome_rowan::AstNodeList;
use rome_rowan::{AstNode, AstNodeList};

impl Format for JsDirectiveList {
fn format(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
if !self.is_empty() {
let syntax_node = self.syntax();
let next_sibling = syntax_node.next_sibling();
// if next_sibling's first leading_trivia has more than one new_line, we should add an extra empty line at the end of
// JsDirectiveList, for example:
//```js
// "use strict"; <- first leading new_line
// <- second leading new_line
// function foo() {

// }
//```
// so we should keep an extra empty line after JsDirectiveList
let need_extra_empty_line = if let Some(next_sibling) = next_sibling {
get_lines_before(&next_sibling) > 1
} else {
false
};
Ok(format_elements![
formatter.format_list(self.clone()),
hard_line_break()
hard_line_break(),
if need_extra_empty_line {
empty_line()
} else {
empty_element()
}
])
} else {
Ok(empty_element())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
source: crates/rome_js_formatter/tests/spec_test.rs
assertion_line: 242
expression: newlines.js

---
# Input
"directive";
Expand Down Expand Up @@ -97,6 +99,7 @@ Quote style: Double Quotes
"directive";

"directive";

statement();
// comment
statement();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: crates/rome_js_formatter/tests/prettier_tests.rs
assertion_line: 144
assertion_line: 175
expression: newline.js

---
Expand All @@ -21,6 +21,7 @@ a();
/* @flow */

"use strict";

import a from "a";

a();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
source: crates/rome_js_formatter/tests/prettier_tests.rs
assertion_line: 175
expression: test.js

---
# Input
```js
Expand Down Expand Up @@ -35,6 +37,7 @@ function f4() {
# Output
```js
"use strict";

function f1() {
"use strict";
}
Expand All @@ -46,11 +49,13 @@ function f2() {

function f3() {
"ngInject";

Object.assign(this, { $log, $uibModal });
}

function f4() {
"ngInject";

Object.assign(this, { $log, $uibModal });
}

Expand Down