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): Restructure simple statements (#1769)
Browse files Browse the repository at this point in the history
Renames and restructures the following statements as we agreed to in our new JS grammar (#1719):

* BlockStatement: Rename node and rename `stmts` child to `statements`
* DebuggerStatement: Rename node
* EmptyStatement: Rename node
* ExpressionStatement: Rename node, `expr` child to `expression`, and add optional trailing semicolon
* ReturnStatement: Rename node, and rename 'value' child to 'argument' 
* LabeledStatement: Rename node, change type of `label` child to a token, and rename `stmt` child to `body`
* WithStatement: 
  * Rename node
  * inline "condition" (it's not a condition)
  * rename `condition` to `object`
  * rename `cons` to `body`
  * Add parser test
  • Loading branch information
MichaReiser authored Nov 11, 2021
1 parent 7dca80b commit 8bdbafe
Show file tree
Hide file tree
Showing 88 changed files with 626 additions and 476 deletions.
28 changes: 14 additions & 14 deletions crates/rome_formatter/src/cst.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::{token, FormatElement, FormatResult, Formatter, ToFormatElement};
use rslint_parser::ast::{
ArgList, ArrayExpr, ArrayPattern, ArrowExpr, AssignPattern, BlockStmt, CallExpr, CaseClause,
CatchClause, ClassBody, ClassDecl, ClassProp, Condition, ConstructorParameters, ContinueStmt,
DebuggerStmt, Declarator, DefaultClause, DoWhileStmt, EmptyStmt, ExprStmt, Finalizer, FnDecl,
ForInStmt, ForStmt, ForStmtInit, ForStmtTest, ForStmtUpdate, Getter, IdentProp, IfStmt,
JsScript, LabelledStmt, Literal, LiteralProp, Name, NameRef, ObjectExpr, ParameterList,
ReturnStmt, SequenceExpr, Setter, SinglePattern, SwitchStmt, TryStmt, VarDecl, WhileStmt,
WithStmt,
ArgList, ArrayExpr, ArrayPattern, ArrowExpr, AssignPattern, CallExpr, CaseClause, CatchClause,
ClassBody, ClassDecl, ClassProp, Condition, ConstructorParameters, ContinueStmt, Declarator,
DefaultClause, DoWhileStmt, Finalizer, FnDecl, ForInStmt, ForStmt, ForStmtInit, ForStmtTest,
ForStmtUpdate, Getter, IdentProp, IfStmt, JsBlockStatement, JsDebuggerStatement,
JsEmptyStatement, JsExpressionStatement, JsLabeledStatement, JsReturnStatement, JsScript,
JsWithStatement, Literal, LiteralProp, Name, NameRef, ObjectExpr, ParameterList, SequenceExpr,
Setter, SinglePattern, SwitchStmt, TryStmt, VarDecl, WhileStmt,
};
use rslint_parser::{AstNode, AstToken, SyntaxKind, SyntaxNode, SyntaxToken};

Expand Down Expand Up @@ -55,13 +55,13 @@ impl ToFormatElement for SyntaxNode {
SyntaxKind::SEQUENCE_EXPR => SequenceExpr::cast(self.clone())
.unwrap()
.to_format_element(formatter),
SyntaxKind::BLOCK_STMT => BlockStmt::cast(self.clone())
SyntaxKind::JS_BLOCK_STATEMENT => JsBlockStatement::cast(self.clone())
.unwrap()
.to_format_element(formatter),
SyntaxKind::EXPR_STMT => ExprStmt::cast(self.clone())
SyntaxKind::JS_EXPRESSION_STATEMENT => JsExpressionStatement::cast(self.clone())
.unwrap()
.to_format_element(formatter),
SyntaxKind::RETURN_STMT => ReturnStmt::cast(self.clone())
SyntaxKind::JS_RETURN_STATEMENT => JsReturnStatement::cast(self.clone())
.unwrap()
.to_format_element(formatter),
SyntaxKind::IF_STMT => IfStmt::cast(self.clone())
Expand All @@ -82,7 +82,7 @@ impl ToFormatElement for SyntaxNode {
SyntaxKind::FOR_STMT_UPDATE => ForStmtUpdate::cast(self.clone())
.unwrap()
.to_format_element(formatter),
SyntaxKind::EMPTY_STMT => EmptyStmt::cast(self.clone())
SyntaxKind::JS_EMPTY_STATEMENT => JsEmptyStatement::cast(self.clone())
.unwrap()
.to_format_element(formatter),
SyntaxKind::IDENT_PROP => IdentProp::cast(self.clone())
Expand All @@ -109,7 +109,7 @@ impl ToFormatElement for SyntaxNode {
SyntaxKind::CONTINUE_STMT => ContinueStmt::cast(self.clone())
.unwrap()
.to_format_element(formatter),
SyntaxKind::LABELLED_STMT => LabelledStmt::cast(self.clone())
SyntaxKind::JS_LABELED_STATEMENT => JsLabeledStatement::cast(self.clone())
.unwrap()
.to_format_element(formatter),
SyntaxKind::TRY_STMT => TryStmt::cast(self.clone())
Expand All @@ -121,10 +121,10 @@ impl ToFormatElement for SyntaxNode {
SyntaxKind::CATCH_CLAUSE => CatchClause::cast(self.clone())
.unwrap()
.to_format_element(formatter),
SyntaxKind::WITH_STMT => WithStmt::cast(self.clone())
SyntaxKind::JS_WITH_STATEMENT => JsWithStatement::cast(self.clone())
.unwrap()
.to_format_element(formatter),
SyntaxKind::DEBUGGER_STMT => DebuggerStmt::cast(self.clone())
SyntaxKind::JS_DEBUGGER_STATEMENT => JsDebuggerStatement::cast(self.clone())
.unwrap()
.to_format_element(formatter),
SyntaxKind::FOR_IN_STMT => ForInStmt::cast(self.clone())
Expand Down
2 changes: 1 addition & 1 deletion crates/rome_formatter/src/ts/class/class_declarator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl ToFormatElement for ClassBody {
impl ToFormatElement for ClassElement {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
match self {
ClassElement::EmptyStmt(empty_statement) => {
ClassElement::JsEmptyStatement(empty_statement) => {
empty_statement.to_format_element(formatter)
}
ClassElement::Method(method) => method.to_format_element(formatter),
Expand Down
2 changes: 1 addition & 1 deletion crates/rome_formatter/src/ts/expr_or_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rslint_parser::ast::ExprOrBlock;
impl ToFormatElement for ExprOrBlock {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
match self {
ExprOrBlock::BlockStmt(block) => block.to_format_element(formatter),
ExprOrBlock::JsBlockStatement(block) => block.to_format_element(formatter),
ExprOrBlock::JsAnyExpression(expr) => expr.to_format_element(formatter),
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/rome_formatter/src/ts/statements/block.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rslint_parser::ast::{BlockStmt, IfStmt};
use rslint_parser::ast::{IfStmt, JsBlockStatement};
use rslint_parser::AstNode;

use crate::ts::statements::format_statements;
Expand All @@ -7,9 +7,9 @@ use crate::{
ToFormatElement,
};

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

// Formatting of curly braces for an:
// * empty block: same line `{}`,
Expand Down
4 changes: 2 additions & 2 deletions crates/rome_formatter/src/ts/statements/debugger_statement.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{format_elements, token, FormatElement, FormatResult, Formatter, ToFormatElement};
use rslint_parser::ast::DebuggerStmt;
use rslint_parser::ast::JsDebuggerStatement;

impl ToFormatElement for DebuggerStmt {
impl ToFormatElement for JsDebuggerStatement {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
Ok(format_elements![
formatter.format_token(&self.debugger_token()?)?,
Expand Down
4 changes: 2 additions & 2 deletions crates/rome_formatter/src/ts/statements/empty_statement.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{empty_element, FormatElement, FormatResult, Formatter, ToFormatElement};
use rslint_parser::ast::EmptyStmt;
use rslint_parser::ast::JsEmptyStatement;

impl ToFormatElement for EmptyStmt {
impl ToFormatElement for JsEmptyStatement {
fn to_format_element(&self, _formatter: &Formatter) -> FormatResult<FormatElement> {
Ok(empty_element())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use rslint_parser::ast::ExprStmt;
use rslint_parser::ast::JsExpressionStatement;

use crate::{format_elements, token, FormatElement, FormatResult, Formatter, ToFormatElement};

impl ToFormatElement for ExprStmt {
impl ToFormatElement for JsExpressionStatement {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
Ok(format_elements![
formatter.format_node(self.expr()?)?,
formatter.format_node(self.expression()?)?,
token(";")
])
}
Expand Down
8 changes: 4 additions & 4 deletions crates/rome_formatter/src/ts/statements/label_statement.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::{
format_elements, space_token, FormatElement, FormatResult, Formatter, ToFormatElement,
};
use rslint_parser::ast::LabelledStmt;
use rslint_parser::ast::JsLabeledStatement;

impl ToFormatElement for LabelledStmt {
impl ToFormatElement for JsLabeledStatement {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
let label = formatter.format_node(self.label()?)?;
let statement = formatter.format_node(self.stmt()?)?;
let label = formatter.format_token(&self.label_token()?)?;
let colon = formatter.format_token(&self.colon_token()?)?;
let statement = formatter.format_node(self.body()?)?;

Ok(format_elements![label, colon, space_token(), statement])
}
Expand Down
12 changes: 7 additions & 5 deletions crates/rome_formatter/src/ts/statements/return_statement.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use crate::{
concat_elements, space_token, token, FormatElement, FormatResult, Formatter, ToFormatElement,
};
use rslint_parser::ast::ReturnStmt;
use rslint_parser::ast::JsReturnStatement;

impl ToFormatElement for ReturnStmt {
impl ToFormatElement for JsReturnStatement {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
let mut tokens = vec![token("return")];
let mut tokens = vec![formatter.format_token(&self.return_token()?)?];

tokens.push(space_token());
tokens.push(formatter.format_node(self.value()?)?);
if let Some(argument) = self.argument() {
tokens.push(space_token());
tokens.push(formatter.format_node(argument)?);
}

tokens.push(token(";"));

Expand Down
18 changes: 11 additions & 7 deletions crates/rome_formatter/src/ts/statements/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ use rslint_parser::ast::JsAnyStatement;
impl ToFormatElement for JsAnyStatement {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
match self {
JsAnyStatement::BlockStmt(block) => block.to_format_element(formatter),
JsAnyStatement::EmptyStmt(empty_statement) => {
JsAnyStatement::JsBlockStatement(block) => block.to_format_element(formatter),
JsAnyStatement::JsEmptyStatement(empty_statement) => {
empty_statement.to_format_element(formatter)
}
JsAnyStatement::ExprStmt(expr_stmt) => expr_stmt.to_format_element(formatter),
JsAnyStatement::JsExpressionStatement(expr_stmt) => {
expr_stmt.to_format_element(formatter)
}
JsAnyStatement::IfStmt(if_stmt) => if_stmt.to_format_element(formatter),
JsAnyStatement::DoWhileStmt(do_while_statement) => {
do_while_statement.to_format_element(formatter)
Expand All @@ -26,9 +28,11 @@ impl ToFormatElement for JsAnyStatement {
JsAnyStatement::BreakStmt(break_statement) => {
break_statement.to_format_element(formatter)
}
JsAnyStatement::ReturnStmt(stmt) => stmt.to_format_element(formatter),
JsAnyStatement::WithStmt(with_statement) => with_statement.to_format_element(formatter),
JsAnyStatement::LabelledStmt(label_statement) => {
JsAnyStatement::JsReturnStatement(stmt) => stmt.to_format_element(formatter),
JsAnyStatement::JsWithStatement(with_statement) => {
with_statement.to_format_element(formatter)
}
JsAnyStatement::JsLabeledStatement(label_statement) => {
label_statement.to_format_element(formatter)
}
JsAnyStatement::SwitchStmt(switch_statement) => {
Expand All @@ -38,7 +42,7 @@ impl ToFormatElement for JsAnyStatement {
throw_statement.to_format_element(formatter)
}
JsAnyStatement::TryStmt(try_statement) => try_statement.to_format_element(formatter),
JsAnyStatement::DebuggerStmt(debugger_statement) => {
JsAnyStatement::JsDebuggerStatement(debugger_statement) => {
debugger_statement.to_format_element(formatter)
}

Expand Down
21 changes: 11 additions & 10 deletions crates/rome_formatter/src/ts/statements/with_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::WithStmt;
use rslint_parser::ast::JsWithStatement;

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

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

0 comments on commit 8bdbafe

Please sign in to comment.