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 656
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(rsliint_parser): Refine variable declaration AST Tree
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
1 parent
10a557e
commit 124d5a5
Showing
59 changed files
with
2,175 additions
and
2,239 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
8 changes: 3 additions & 5 deletions
8
...ormatter/src/ts/declarators/declarator.rs → ...er/src/ts/auxiliary/equal_value_clause.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,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()?)? | ||
]) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
mod equal_value_clause; |
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
mod decl; | ||
mod declarator; | ||
mod fn_decl; | ||
mod var_decl; | ||
mod variable_declaration_statement; |
This file was deleted.
Oops, something went wrong.
47 changes: 47 additions & 0 deletions
47
crates/rome_formatter/src/ts/declarators/variable_declaration_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 |
---|---|---|
@@ -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 | ||
]) | ||
} | ||
} |
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,4 +1,5 @@ | ||
mod arg_list; | ||
mod auxiliary; | ||
mod class; | ||
mod condition; | ||
mod declarators; | ||
|
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
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
Oops, something went wrong.