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

Commit

Permalink
refactor(rslint_parser): Update while loops to match new AST Facade (#…
Browse files Browse the repository at this point in the history
…1774)

Changes the js grammar to match our new AST facade as defined in #1725 (and proposed in #1719)

* `Continue` Statement
  * Rename from `ContinueStmt` to `JsContinueStatement`
  * Inline the label identifier (It's not a normal "variable" reference but a label reference) 
* `Break` Statement
  * Rename from `BreakStmt` to `JsBreakStatement`
  * Inline the label identifier (It's not a normal "variable" reference but a label reference) 
* `While` Statement
  * Rename from `WhileStmt` to `JsWhileStatement`
  * Rename `cons` to `body`
  * Inline the condition and rename `condition` to `test`
* `DoWhileStatement`
  * Rename to `JsDoWhileStatement`
  * Inline condition
  * rename `cons` to `body`
  * rename `condition` to `test`
  • Loading branch information
MichaReiser authored Nov 12, 2021
1 parent 93bbf01 commit 10a557e
Show file tree
Hide file tree
Showing 20 changed files with 434 additions and 286 deletions.
18 changes: 9 additions & 9 deletions crates/rome_formatter/src/cst.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::{token, FormatElement, FormatResult, Formatter, ToFormatElement};
use rslint_parser::ast::{
ArgList, ArrayExpr, ArrayPattern, ArrowExpr, AssignPattern, CallExpr, ClassBody, ClassDecl,
ClassProp, Condition, ConstructorParameters, ContinueStmt, Declarator, DoWhileStmt, FnDecl,
ForInStmt, ForStmt, ForStmtInit, ForStmtTest, ForStmtUpdate, Getter, IdentProp,
JsBlockStatement, JsCaseClause, JsCatchClause, JsDebuggerStatement, JsDefaultClause,
ClassProp, Condition, ConstructorParameters, Declarator, FnDecl, ForInStmt, ForStmt,
ForStmtInit, ForStmtTest, ForStmtUpdate, Getter, IdentProp, JsBlockStatement, JsCaseClause,
JsCatchClause, JsContinueStatement, JsDebuggerStatement, JsDefaultClause, JsDoWhileStatement,
JsEmptyStatement, JsExpressionStatement, JsFinallyClause, JsIfStatement, JsLabeledStatement,
JsReturnStatement, JsScript, JsSwitchStatement, JsTryStatement, JsWithStatement, Literal,
LiteralProp, Name, NameRef, ObjectExpr, ParameterList, SequenceExpr, Setter, SinglePattern,
VarDecl, WhileStmt,
JsReturnStatement, JsScript, JsSwitchStatement, JsTryStatement, JsWhileStatement,
JsWithStatement, Literal, LiteralProp, Name, NameRef, ObjectExpr, ParameterList, SequenceExpr,
Setter, SinglePattern, VarDecl,
};
use rslint_parser::{AstNode, AstToken, SyntaxKind, SyntaxNode, SyntaxToken};

Expand Down Expand Up @@ -92,10 +92,10 @@ impl ToFormatElement for SyntaxNode {
SyntaxKind::OBJECT_EXPR => ObjectExpr::cast(self.clone())
.unwrap()
.to_format_element(formatter),
SyntaxKind::WHILE_STMT => WhileStmt::cast(self.clone())
SyntaxKind::JS_WHILE_STATEMENT => JsWhileStatement::cast(self.clone())
.unwrap()
.to_format_element(formatter),
SyntaxKind::DO_WHILE_STMT => DoWhileStmt::cast(self.clone())
SyntaxKind::JS_DO_WHILE_STATEMENT => JsDoWhileStatement::cast(self.clone())
.unwrap()
.to_format_element(formatter),
SyntaxKind::JS_SWITCH_STATEMENT => JsSwitchStatement::cast(self.clone())
Expand All @@ -107,7 +107,7 @@ impl ToFormatElement for SyntaxNode {
SyntaxKind::JS_CASE_CLAUSE => JsCaseClause::cast(self.clone())
.unwrap()
.to_format_element(formatter),
SyntaxKind::CONTINUE_STMT => ContinueStmt::cast(self.clone())
SyntaxKind::JS_CONTINUE_STATEMENT => JsContinueStatement::cast(self.clone())
.unwrap()
.to_format_element(formatter),
SyntaxKind::JS_LABELED_STATEMENT => JsLabeledStatement::cast(self.clone())
Expand Down
22 changes: 11 additions & 11 deletions crates/rome_formatter/src/ts/statements/break_statement.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
use crate::{
empty_element, format_elements, group_elements, space_token, token, FormatElement,
FormatResult, Formatter, ToFormatElement,
empty_element, format_elements, space_token, token, FormatElement, FormatResult, Formatter,
ToFormatElement,
};
use rslint_parser::ast::BreakStmt;
use rslint_parser::ast::JsBreakStatement;

impl ToFormatElement for BreakStmt {
impl ToFormatElement for JsBreakStatement {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
let break_element = formatter.format_token(&self.break_token()?)?;
let ident = if let Some(ident_token) = self.ident_token() {
group_elements(format_elements![
space_token(),
formatter.format_token(&ident_token)?
])
let label = if let Some(label_token) = self.label_token() {
format_elements![space_token(), formatter.format_token(&label_token)?]
} else {
empty_element()
};

Ok(format_elements![break_element, ident, token(";")])
Ok(format_elements![
formatter.format_token(&self.break_token()?)?,
label,
token(";")
])
}
}
17 changes: 10 additions & 7 deletions crates/rome_formatter/src/ts/statements/continue_statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ use crate::{
empty_element, format_elements, space_token, token, FormatElement, FormatResult, Formatter,
ToFormatElement,
};
use rslint_parser::ast::ContinueStmt;
use rslint_parser::ast::JsContinueStatement;

impl ToFormatElement for ContinueStmt {
impl ToFormatElement for JsContinueStatement {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
// NOTE: rslint parser (upstream) is currently broken https://github.com/rslint/rslint/issues/126
let ident = if let Ok(name_ref) = self.name_ref() {
format_elements![space_token(), formatter.format_node(name_ref)?]
let label = if let Some(label_token) = self.label_token() {
format_elements![space_token(), formatter.format_token(&label_token)?]
} else {
empty_element()
};
let continue_token = formatter.format_token(&self.continue_token()?)?;
Ok(format_elements![continue_token, ident, token(";")])

Ok(format_elements![
formatter.format_token(&self.continue_token()?)?,
label,
token(";")
])
}
}
25 changes: 12 additions & 13 deletions crates/rome_formatter/src/ts/statements/do_while_statement.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
use crate::{
format_elements, space_token, token, FormatElement, FormatResult, Formatter, ToFormatElement,
format_elements, group_elements, soft_indent, space_token, token, FormatElement, FormatResult,
Formatter, ToFormatElement,
};
use rslint_parser::ast::DoWhileStmt;
use rslint_parser::ast::JsDoWhileStatement;

impl ToFormatElement for DoWhileStmt {
impl ToFormatElement for JsDoWhileStatement {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
let do_token = formatter.format_token(&self.do_token()?)?;

let condition = formatter.format_node(self.condition()?)?;
let cons = formatter.format_node(self.cons()?)?;
let while_token = formatter.format_token(&self.while_token()?)?;

Ok(format_elements![
do_token,
formatter.format_token(&self.do_token()?)?,
space_token(),
cons,
formatter.format_node(self.body()?)?,
space_token(),
while_token,
formatter.format_token(&self.while_token()?)?,
space_token(),
condition,
group_elements(format_elements![
formatter.format_token(&self.l_paren_token()?)?,
soft_indent(formatter.format_node(self.test()?)?),
formatter.format_token(&self.r_paren_token()?)?
]),
token(";")
])
}
Expand Down
8 changes: 4 additions & 4 deletions crates/rome_formatter/src/ts/statements/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ impl ToFormatElement for JsAnyStatement {
expr_stmt.to_format_element(formatter)
}
JsAnyStatement::JsIfStatement(if_stmt) => if_stmt.to_format_element(formatter),
JsAnyStatement::DoWhileStmt(do_while_statement) => {
JsAnyStatement::JsDoWhileStatement(do_while_statement) => {
do_while_statement.to_format_element(formatter)
}
JsAnyStatement::WhileStmt(while_statement) => {
JsAnyStatement::JsWhileStatement(while_statement) => {
while_statement.to_format_element(formatter)
}
JsAnyStatement::ForStmt(for_stmt) => for_stmt.to_format_element(formatter),
JsAnyStatement::ForInStmt(for_in_statement) => {
for_in_statement.to_format_element(formatter)
}
JsAnyStatement::ContinueStmt(continue_statement) => {
JsAnyStatement::JsContinueStatement(continue_statement) => {
continue_statement.to_format_element(formatter)
}
JsAnyStatement::BreakStmt(break_statement) => {
JsAnyStatement::JsBreakStatement(break_statement) => {
break_statement.to_format_element(formatter)
}
JsAnyStatement::JsReturnStatement(stmt) => stmt.to_format_element(formatter),
Expand Down
21 changes: 11 additions & 10 deletions crates/rome_formatter/src/ts/statements/while_statement.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
use crate::{
format_elements, space_token, FormatElement, FormatResult, Formatter, ToFormatElement,
format_elements, group_elements, soft_indent, space_token, FormatElement, FormatResult,
Formatter, ToFormatElement,
};
use rslint_parser::ast::WhileStmt;
use rslint_parser::ast::JsWhileStatement;

impl ToFormatElement for WhileStmt {
impl ToFormatElement for JsWhileStatement {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
let while_token = formatter.format_token(&self.while_token()?)?;
let condition = formatter.format_node(self.condition()?)?;
let cons = formatter.format_node(self.cons()?)?;

Ok(format_elements![
while_token,
formatter.format_token(&self.while_token()?)?,
space_token(),
condition,
group_elements(format_elements![
formatter.format_token(&self.l_paren_token()?)?,
soft_indent(formatter.format_node(self.test()?)?),
formatter.format_token(&self.r_paren_token()?)?
]),
space_token(),
cons
formatter.format_node(self.body()?)?
])
}
}
Loading

0 comments on commit 10a557e

Please sign in to comment.