This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 657
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(rome_js_formatter): print empty statements differently
- Loading branch information
Showing
8 changed files
with
67 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 14 additions & 5 deletions
19
crates/rome_js_formatter/src/js/statements/empty_statement.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,22 @@ | ||
use crate::{empty_element, FormatElement, FormatNode, Formatter}; | ||
use crate::{empty_element, Format, FormatElement, FormatNode, Formatter}; | ||
use rome_formatter::FormatResult; | ||
|
||
use rome_js_syntax::JsEmptyStatement; | ||
use rome_js_syntax::JsEmptyStatementFields; | ||
use rome_js_syntax::{JsEmptyStatement, JsSyntaxKind}; | ||
use rome_rowan::AstNode; | ||
|
||
impl FormatNode for JsEmptyStatement { | ||
fn format_fields(&self, formatter: &Formatter) -> FormatResult<FormatElement> { | ||
let JsEmptyStatementFields { semicolon_token } = self.as_fields(); | ||
|
||
Ok(formatter.format_replaced(&semicolon_token?, empty_element())) | ||
let parent_kind = self.syntax().parent().map(|p| p.kind()); | ||
if matches!( | ||
parent_kind, | ||
Some(JsSyntaxKind::JS_DO_WHILE_STATEMENT) | ||
| Some(JsSyntaxKind::JS_IF_STATEMENT) | ||
| Some(JsSyntaxKind::JS_ELSE_CLAUSE) | ||
) { | ||
semicolon_token.format(formatter) | ||
} else { | ||
Ok(formatter.format_replaced(&semicolon_token?, empty_element())) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,7 @@ do { // trailing | |
var foo = 4 | ||
} | ||
|
||
while (something) | ||
while (something) | ||
|
||
|
||
do; while(true); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
crates/rome_js_formatter/tests/specs/js/module/statement/if_else.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
if(a); | ||
if(a); else ; | ||
|
||
if (Math.random() > 0.5) { | ||
console.log(1) | ||
} else if (Math.random() > 0.5) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters