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

Commit

Permalink
refactor(rsliint_parser): Refine variable declaration AST Tree
Browse files Browse the repository at this point in the history
Changes the js grammar of the variable declaration to match our new AST facade as defined in #1725 (and proposed in #1719)

* Split `VarDecl` into `JsVariableDeclarationStatement` and `JsVariableDeclaration` to correctly account for variable declarations inside for loops (that aren't statements).
* Change the parser to emit a `let` token instead of an ident if let is used inside a variable declaration (let is a valid identifier which is why the lexer returns an `ident` token)
* Rename `Declarator` to `JsVariableDeclarator`
* Split out the `init` into a `JsEqualValueClause` that holds the `=` token and the initializer expression.
  • Loading branch information
MichaReiser authored Nov 12, 2021
1 parent 10a557e commit 124d5a5
Show file tree
Hide file tree
Showing 59 changed files with 2,175 additions and 2,239 deletions.
22 changes: 12 additions & 10 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, Declarator, FnDecl, ForInStmt, ForStmt,
ForStmtInit, ForStmtTest, ForStmtUpdate, Getter, IdentProp, JsBlockStatement, JsCaseClause,
JsCatchClause, JsContinueStatement, JsDebuggerStatement, JsDefaultClause, JsDoWhileStatement,
ClassProp, Condition, ConstructorParameters, FnDecl, ForInStmt, ForStmt, ForStmtInit,
ForStmtTest, ForStmtUpdate, Getter, IdentProp, JsBlockStatement, JsCaseClause, JsCatchClause,
JsContinueStatement, JsDebuggerStatement, JsDefaultClause, JsDoWhileStatement,
JsEmptyStatement, JsExpressionStatement, JsFinallyClause, JsIfStatement, JsLabeledStatement,
JsReturnStatement, JsScript, JsSwitchStatement, JsTryStatement, JsWhileStatement,
JsWithStatement, Literal, LiteralProp, Name, NameRef, ObjectExpr, ParameterList, SequenceExpr,
Setter, SinglePattern, VarDecl,
JsReturnStatement, JsScript, JsSwitchStatement, JsTryStatement, JsVariableDeclarationStatement,
JsVariableDeclarator, JsWhileStatement, JsWithStatement, Literal, LiteralProp, Name, NameRef,
ObjectExpr, ParameterList, SequenceExpr, Setter, SinglePattern,
};
use rslint_parser::{AstNode, AstToken, SyntaxKind, SyntaxNode, SyntaxToken};

Expand Down Expand Up @@ -44,10 +44,12 @@ impl ToFormatElement for SyntaxNode {
SyntaxKind::SPREAD_ELEMENT => SinglePattern::cast(self.clone())
.unwrap()
.to_format_element(formatter),
SyntaxKind::VAR_DECL => VarDecl::cast(self.clone())
.unwrap()
.to_format_element(formatter),
SyntaxKind::DECLARATOR => Declarator::cast(self.clone())
SyntaxKind::JS_VARIABLE_DECLARATION_STATEMENT => {
JsVariableDeclarationStatement::cast(self.clone())
.unwrap()
.to_format_element(formatter)
}
SyntaxKind::JS_VARIABLE_DECLARATOR => JsVariableDeclarator::cast(self.clone())
.unwrap()
.to_format_element(formatter),
SyntaxKind::FN_DECL => FnDecl::cast(self.clone())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use crate::{
format_elements, space_token, FormatElement, FormatResult, Formatter, ToFormatElement,
};
use rslint_parser::ast::Declarator;
use rslint_parser::ast::JsEqualValueClause;

impl ToFormatElement for Declarator {
impl ToFormatElement for JsEqualValueClause {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
Ok(format_elements![
formatter.format_node(self.pattern()?)?,
space_token(),
formatter.format_token(&self.eq_token()?)?,
space_token(),
formatter.format_node(self.value()?)?,
formatter.format_node(self.expression()?)?
])
}
}
1 change: 1 addition & 0 deletions crates/rome_formatter/src/ts/auxiliary/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod equal_value_clause;
2 changes: 1 addition & 1 deletion crates/rome_formatter/src/ts/declarators/decl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ impl ToFormatElement for Decl {
match self {
Decl::FnDecl(fn_decl) => fn_decl.to_format_element(formatter),
Decl::ClassDecl(class_declarator) => class_declarator.to_format_element(formatter),
Decl::VarDecl(var_decl) => var_decl.to_format_element(formatter),
Decl::JsVariableDeclarationStatement(var_decl) => var_decl.to_format_element(formatter),
Decl::TsEnum(_) => todo!(),
Decl::TsTypeAliasDecl(_) => todo!(),
Decl::TsNamespaceDecl(_) => todo!(),
Expand Down
3 changes: 1 addition & 2 deletions crates/rome_formatter/src/ts/declarators/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
mod decl;
mod declarator;
mod fn_decl;
mod var_decl;
mod variable_declaration_statement;
36 changes: 0 additions & 36 deletions crates/rome_formatter/src/ts/declarators/var_decl.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use crate::{
empty_element, format_elements, join_elements, space_token, token, FormatElement, FormatResult,
Formatter, ToFormatElement,
};
use rslint_parser::ast::{
JsVariableDeclaration, JsVariableDeclarationStatement, JsVariableDeclarator,
};

impl ToFormatElement for JsVariableDeclarationStatement {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
Ok(format_elements![
formatter.format_node(self.declaration()?)?,
token(";"),
])
}
}

impl ToFormatElement for JsVariableDeclaration {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
let mut declarators = Vec::with_capacity(self.declarators().len());

for declarator in self.declarators().iter() {
declarators.push(formatter.format_node(declarator)?);
}

Ok(format_elements![
formatter.format_token(&self.kind_token()?)?,
space_token(),
join_elements(format_elements![token(","), space_token()], declarators),
])
}
}

impl ToFormatElement for JsVariableDeclarator {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
let initializer = if let Some(initializer) = self.init() {
format_elements![space_token(), formatter.format_node(initializer)?]
} else {
empty_element()
};

Ok(format_elements![
formatter.format_node(self.id()?)?,
initializer
])
}
}
1 change: 1 addition & 0 deletions crates/rome_formatter/src/ts/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod arg_list;
mod auxiliary;
mod class;
mod condition;
mod declarators;
Expand Down
2 changes: 1 addition & 1 deletion crates/rome_formatter/src/ts/statements/for_stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl ToFormatElement for ForStmtInit {
impl ToFormatElement for ForHead {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
match self {
ForHead::VarDecl(decl) => decl.to_format_element(formatter),
ForHead::JsVariableDeclaration(decl) => decl.to_format_element(formatter),
ForHead::JsAnyExpression(expr) => expr.to_format_element(formatter),
}
}
Expand Down
40 changes: 14 additions & 26 deletions crates/rslint_parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ impl<N: AstNode> IntoIterator for &AstSeparatedList<N> {
/// Specific result used when navigating nodes using AST APIs
pub type SyntaxResult<ResultType> = Result<ResultType, SyntaxError>;

#[derive(Debug)]
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum SyntaxError {
/// Error thrown when a mandatory node is not found
MissingRequiredChild(SyntaxNode),
Expand All @@ -364,14 +364,12 @@ mod support {
use crate::SyntaxList;
use crate::{SyntaxError, SyntaxResult};

// TODO: #1725 remove once API are set in stone
#[allow(dead_code)]
pub(super) fn child<N: AstNode>(parent: &SyntaxNode) -> Option<N> {
pub(super) fn node<N: AstNode>(parent: &SyntaxNode) -> Option<N> {
parent.children().find_map(N::cast)
}

pub(super) fn as_optional_node<N: AstNode>(parent: &SyntaxNode) -> Option<N> {
parent.children().find_map(N::cast)
pub(super) fn required_node<N: AstNode>(parent: &SyntaxNode) -> SyntaxResult<N> {
node(parent).ok_or_else(|| SyntaxError::MissingRequiredChild(parent.clone()))
}

pub(super) fn elements(parent: &SyntaxNode) -> SyntaxElementChildren {
Expand Down Expand Up @@ -409,29 +407,11 @@ mod support {
.find(|it| it.kind() == kind)
}

pub(super) fn as_optional_token(parent: &SyntaxNode, kind: SyntaxKind) -> Option<SyntaxToken> {
parent
.children_with_tokens()
.filter_map(|it| it.into_token())
.find(|it| it.kind() == kind)
}

pub(super) fn as_mandatory_node<N: AstNode>(parent: &SyntaxNode) -> SyntaxResult<N> {
parent
.children()
.find_map(N::cast)
.ok_or_else(|| SyntaxError::MissingRequiredChild(parent.clone()))
}

pub(super) fn as_mandatory_token(
pub(super) fn required_token(
parent: &SyntaxNode,
kind: SyntaxKind,
) -> SyntaxResult<SyntaxToken> {
parent
.children_with_tokens()
.filter_map(|it| it.into_token())
.find(|it| it.kind() == kind)
.ok_or_else(|| SyntaxError::MissingRequiredChild(parent.clone()))
token(parent, kind).ok_or_else(|| SyntaxError::MissingRequiredChild(parent.clone()))
}

pub(super) fn find_token(
Expand All @@ -447,6 +427,14 @@ mod support {
.any(|possible_kind| *possible_kind == it.kind())
})
}

pub(super) fn find_required_token(
parent: &SyntaxNode,
possible_kinds: &[SyntaxKind],
) -> SyntaxResult<SyntaxToken> {
find_token(parent, possible_kinds)
.ok_or_else(|| SyntaxError::MissingRequiredChild(parent.clone()))
}
}

#[cfg(test)]
Expand Down
8 changes: 4 additions & 4 deletions crates/rslint_parser/src/ast/expr_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use SyntaxKind::*;

impl BracketExpr {
pub fn object(&self) -> Option<JsAnyExpression> {
support::child(self.syntax())
support::node(self.syntax())
}

pub fn prop(&self) -> Option<JsAnyExpression> {
Expand All @@ -15,7 +15,7 @@ impl BracketExpr {

impl CondExpr {
pub fn test(&self) -> Option<JsAnyExpression> {
support::child(self.syntax())
support::node(self.syntax())
}

pub fn cons(&self) -> Option<JsAnyExpression> {
Expand All @@ -29,7 +29,7 @@ impl CondExpr {

impl LiteralProp {
pub fn key(&self) -> SyntaxResult<PropName> {
support::as_mandatory_node::<PropName>(self.syntax())
support::required_node::<PropName>(self.syntax())
}

pub fn value(&self) -> SyntaxResult<JsAnyExpression> {
Expand Down Expand Up @@ -144,7 +144,7 @@ impl BinExpr {
}

pub fn lhs(&self) -> Option<JsAnyExpression> {
support::child(self.syntax())
support::node(self.syntax())
}

pub fn rhs(&self) -> Option<JsAnyExpression> {
Expand Down
Loading

0 comments on commit 124d5a5

Please sign in to comment.