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): Change MemberExpression AST structure (#1799)
Browse files Browse the repository at this point in the history
Refactors the AST tree structure for MemberExpressions as part of #1725 

* Renames `BracketExpr` to `JsComputedMemberExpression`
* Renames `DotExpr` to `JsStaticMemberExpression`
* Introduces the new `JsReferenceIdentifierMember` and `JsReferencePrivateMember` which are references to member names (analog to `JsReferenceIdentifierExpression` that references an identifier and `JsBindingIdentifier` that defines an identifier)
* Merge `PrivatePropAccess` into `JsStaticMemberExpression` (enabled by introducing `JsReferenceMember`
* Introduce `SuperExpression` so that `super.test()` works 
* Add new check that verifies that calling `super()` only works inside of constructor (I leave checking if we're inside of a subclass to another PR).
* Delete `SuperCall` as this can now be modelled using `CallExpr`
* Deleted some no longer used nodes/kinds
  • Loading branch information
MichaReiser authored Nov 22, 2021
1 parent a9aa8dc commit d0b9b60
Show file tree
Hide file tree
Showing 45 changed files with 789 additions and 500 deletions.
7 changes: 2 additions & 5 deletions crates/rome_formatter/src/cst.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{FormatElement, FormatResult, Formatter, ToFormatElement};
use rslint_parser::ast::{
ArgList, ArrayPattern, AssignPattern, CallExpr, Condition, ForInStmt, ForStmt, ForStmtInit,
ForStmtTest, ForStmtUpdate, JsArrayExpression, JsArrowFunctionExpression, JsBlockStatement,
ArgList, ArrayPattern, AssignPattern, CallExpr, ForInStmt, ForStmt, ForStmtInit, ForStmtTest,
ForStmtUpdate, JsArrayExpression, JsArrowFunctionExpression, JsBlockStatement,
JsBooleanLiteral, JsCaseClause, JsCatchClause, JsClassDeclaration, JsConstructorParameterList,
JsContinueStatement, JsDebuggerStatement, JsDefaultClause, JsDoWhileStatement,
JsEmptyStatement, JsExpressionStatement, JsFinallyClause, JsFunctionDeclaration,
Expand Down Expand Up @@ -86,9 +86,6 @@ impl ToFormatElement for SyntaxNode {
SyntaxKind::JS_IF_STATEMENT => JsIfStatement::cast(self.clone())
.unwrap()
.to_format_element(formatter),
SyntaxKind::CONDITION => Condition::cast(self.clone())
.unwrap()
.to_format_element(formatter),
SyntaxKind::FOR_STMT => ForStmt::cast(self.clone())
.unwrap()
.to_format_element(formatter),
Expand Down
10 changes: 1 addition & 9 deletions crates/rome_formatter/src/ts/class/class_declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
block_indent, empty_element, format_elements, group_elements, hard_line_break, join_elements,
space_token, FormatElement, FormatResult, Formatter, ToFormatElement,
};
use rslint_parser::ast::{JsClassDeclaration, SuperCall};
use rslint_parser::ast::JsClassDeclaration;

impl ToFormatElement for JsClassDeclaration {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
Expand All @@ -29,11 +29,3 @@ impl ToFormatElement for JsClassDeclaration {
])
}
}

impl ToFormatElement for SuperCall {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
let super_token = formatter.format_token(&self.super_token()?)?;
let arguments = formatter.format_node(self.arguments()?)?;
Ok(format_elements![super_token, arguments])
}
}
15 changes: 0 additions & 15 deletions crates/rome_formatter/src/ts/condition.rs

This file was deleted.

7 changes: 3 additions & 4 deletions crates/rome_formatter/src/ts/expressions/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ impl ToFormatElement for JsAnyExpression {
object_expression.to_format_element(formatter)
}
JsAnyExpression::JsParenthesizedExpression(_) => todo!(),
JsAnyExpression::BracketExpr(_) => todo!(),
JsAnyExpression::DotExpr(_) => todo!(),
JsAnyExpression::JsComputedMemberExpression(_) => todo!(),
JsAnyExpression::JsStaticMemberExpression(_) => todo!(),
JsAnyExpression::NewExpr(_) => todo!(),
JsAnyExpression::CallExpr(call_expression) => {
call_expression.to_format_element(formatter)
Expand All @@ -33,18 +33,17 @@ impl ToFormatElement for JsAnyExpression {
JsAnyExpression::JsClassExpression(_) => todo!(),
JsAnyExpression::NewTarget(_) => todo!(),
JsAnyExpression::ImportMeta(_) => todo!(),
JsAnyExpression::SuperCall(super_call) => super_call.to_format_element(formatter),
JsAnyExpression::JsImportCallExpression(_) => todo!(),
JsAnyExpression::JsYieldExpression(_) => todo!(),
JsAnyExpression::JsAwaitExpression(_) => todo!(),
JsAnyExpression::PrivatePropAccess(_) => todo!(),
JsAnyExpression::TsNonNull(_) => todo!(),
JsAnyExpression::TsAssertion(_) => todo!(),
JsAnyExpression::TsConstAssertion(_) => todo!(),
JsAnyExpression::JsPreUpdateExpression(_) => todo!(),
JsAnyExpression::JsPostUpdateExpression(_) => todo!(),
JsAnyExpression::JsUnknownExpression(_) => todo!(),
JsAnyExpression::JsLogicalExpression(_) => todo!(),
JsAnyExpression::JsSuperExpression(expr) => expr.to_format_element(formatter),
}
}
}
1 change: 1 addition & 0 deletions crates/rome_formatter/src/ts/expressions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ mod literal;
mod object_expression;
mod reference_identifier_expression;
mod sequence_expression;
mod super_expression;
8 changes: 8 additions & 0 deletions crates/rome_formatter/src/ts/expressions/super_expression.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use crate::{FormatElement, FormatResult, Formatter, ToFormatElement};
use rslint_parser::ast::JsSuperExpression;

impl ToFormatElement for JsSuperExpression {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
formatter.format_token(&self.super_token()?)
}
}
1 change: 0 additions & 1 deletion crates/rome_formatter/src/ts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ mod arg_list;
mod auxiliary;
mod bindings;
mod class;
mod condition;
mod declarators;
mod expressions;
mod ident;
Expand Down
2 changes: 1 addition & 1 deletion crates/rome_formatter/src/ts/object_members/prop_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{FormatElement, FormatResult, Formatter, ToFormatElement};
impl ToFormatElement for PropName {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
match self {
PropName::ComputedPropertyName(_) => todo!(),
PropName::JsComputedMemberName(_) => todo!(),
PropName::JsStringLiteral(literal) => literal.to_format_element(formatter),
PropName::JsNumberLiteral(literal) => literal.to_format_element(formatter),
PropName::Ident(ident) => ident.to_format_element(formatter),
Expand Down
4 changes: 4 additions & 0 deletions crates/rome_rowan/src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1538,6 +1538,8 @@ impl Iterator for PreorderWithTokens {
}
}

/// Represents a cursor to a green node slot. A slot either contains an element or is empty
/// if the child isn't present in the source.
#[derive(Debug, Clone)]
pub(crate) enum SyntaxSlot {
Node(SyntaxNode),
Expand All @@ -1554,6 +1556,7 @@ impl From<SyntaxElement> for SyntaxSlot {
}
}

/// Iterator over a node's slots
pub(crate) struct SyntaxSlots<'a> {
parent: &'a SyntaxNode,
raw: Enumerate<Slots<'a>>,
Expand Down Expand Up @@ -1606,6 +1609,7 @@ impl<'a> ExactSizeIterator for SyntaxSlots<'a> {
}
impl<'a> FusedIterator for SyntaxSlots<'a> {}

/// Iterator to visit a node's slots in pre-order.
pub(crate) struct SlotsPreorder {
start: SyntaxNode,
next: Option<WalkEvent<SyntaxSlot>>,
Expand Down
43 changes: 5 additions & 38 deletions crates/rslint_parser/src/ast/expr_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ use crate::{ast::*, numbers::*, util::*, TextRange, T};
use rome_rowan::{SyntaxText, TextSize};
use SyntaxKind::*;

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

pub fn prop(&self) -> Option<JsAnyExpression> {
support::children(self.syntax()).nth(1)
impl JsComputedMemberExpression {
pub fn member(&self) -> SyntaxResult<JsAnyExpression> {
support::children(self.syntax())
.nth(1)
.ok_or_else(|| SyntaxError::MissingRequiredChild(self.syntax().clone()))
}
}

Expand Down Expand Up @@ -451,28 +449,6 @@ impl Template {
}
}

impl JsAnyExpression {
/// Whether this is an optional chain expression.
pub fn opt_chain(&self) -> bool {
match self {
JsAnyExpression::DotExpr(dotexpr) => dotexpr.opt_chain_token(),
JsAnyExpression::CallExpr(callexpr) => callexpr.opt_chain_token(),
JsAnyExpression::BracketExpr(bracketexpr) => bracketexpr.opt_chain_token(),
_ => return false,
}
.is_some()
}
}

impl DotExpr {
pub fn opt_chain_token(&self) -> Option<SyntaxToken> {
self.syntax()
.children_with_tokens()
.filter_map(|child| child.into_token())
.find(|tok| tok.kind() == QUESTIONDOT)
}
}

impl CallExpr {
pub fn opt_chain_token(&self) -> Option<SyntaxToken> {
self.syntax()
Expand All @@ -482,15 +458,6 @@ impl CallExpr {
}
}

impl BracketExpr {
pub fn opt_chain_token(&self) -> Option<SyntaxToken> {
self.syntax()
.children_with_tokens()
.filter_map(|child| child.into_token())
.find(|tok| tok.kind() == QUESTIONDOT)
}
}

/// A simple macro for making assign, binop, or unary operators
#[macro_export]
macro_rules! op {
Expand Down
Loading

0 comments on commit d0b9b60

Please sign in to comment.