From f210404ae784ccb2a53dbdd3b54b803dac46c0ad Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Mon, 15 Nov 2021 14:54:45 +0100 Subject: [PATCH] Rename `ArrowExpr` to `JsArrowFunctionExpression` --- crates/rome_formatter/src/cst.rs | 28 +- crates/rome_formatter/src/ts/expr_or_block.rs | 11 - .../src/ts/expressions/arrow_expr.rs | 21 +- .../src/ts/expressions/expression.rs | 2 +- crates/rome_formatter/src/ts/mod.rs | 3 +- crates/rslint_parser/src/ast/expr_ext.rs | 6 +- .../rslint_parser/src/ast/generated/nodes.rs | 567 +++++++++--------- crates/rslint_parser/src/syntax/expr.rs | 24 +- .../err/async_arrow_expr_await_parameter.rast | 4 +- .../err/binding_identifier_invalid.rast | 2 +- .../inline/err/invalid_method_recover.rast | 2 +- .../paren_or_arrow_expr_invalid_params.rast | 2 +- .../inline/ok/arrow_expr_single_param.rast | 16 +- .../test_data/inline/ok/async_arrow_expr.rast | 8 +- .../test_data/inline/ok/directives.rast | 2 +- .../test_data/inline/ok/new_exprs.rast | 2 +- .../inline/ok/paren_or_arrow_expr.rast | 6 +- .../test_data/inline/ok/return_stmt.rast | 2 +- crates/rslint_syntax/src/generated.rs | 2 +- xtask/js.ungram | 26 +- xtask/src/codegen/kinds_src.rs | 2 +- 21 files changed, 374 insertions(+), 364 deletions(-) delete mode 100644 crates/rome_formatter/src/ts/expr_or_block.rs diff --git a/crates/rome_formatter/src/cst.rs b/crates/rome_formatter/src/cst.rs index d1f23cff0e85..76cb2fa965d3 100644 --- a/crates/rome_formatter/src/cst.rs +++ b/crates/rome_formatter/src/cst.rs @@ -1,15 +1,15 @@ use crate::{FormatElement, FormatResult, Formatter, ToFormatElement}; use rslint_parser::ast::{ - ArgList, ArrayPattern, ArrowExpr, AssignPattern, CallExpr, ClassBody, ClassDecl, ClassProp, - Condition, ConstructorParameters, ForInStmt, ForStmt, ForStmtInit, ForStmtTest, ForStmtUpdate, - Getter, IdentProp, JsArrayExpression, JsBlockStatement, JsBooleanLiteral, JsCaseClause, - JsCatchClause, JsContinueStatement, JsDebuggerStatement, JsDefaultClause, JsDoWhileStatement, - JsEmptyStatement, JsExpressionStatement, JsFinallyClause, JsFunctionDeclaration, JsIfStatement, - JsLabeledStatement, JsNullLiteral, JsNumberLiteral, JsParameterList, - JsReferenceIdentifierExpression, JsReturnStatement, JsRoot, JsSequenceExpression, - JsStringLiteral, JsSwitchStatement, JsTryStatement, JsVariableDeclarationStatement, - JsVariableDeclarator, JsWhileStatement, JsWithStatement, LiteralProp, Name, ObjectExpr, Setter, - SinglePattern, + ArgList, ArrayPattern, AssignPattern, CallExpr, ClassBody, ClassDecl, ClassProp, Condition, + ConstructorParameters, ForInStmt, ForStmt, ForStmtInit, ForStmtTest, ForStmtUpdate, Getter, + IdentProp, JsArrayExpression, JsArrowFunctionExpression, JsBlockStatement, JsBooleanLiteral, + JsCaseClause, JsCatchClause, JsContinueStatement, JsDebuggerStatement, JsDefaultClause, + JsDoWhileStatement, JsEmptyStatement, JsExpressionStatement, JsFinallyClause, + JsFunctionDeclaration, JsIfStatement, JsLabeledStatement, JsNullLiteral, JsNumberLiteral, + JsParameterList, JsReferenceIdentifierExpression, JsReturnStatement, JsRoot, + JsSequenceExpression, JsStringLiteral, JsSwitchStatement, JsTryStatement, + JsVariableDeclarationStatement, JsVariableDeclarator, JsWhileStatement, JsWithStatement, + LiteralProp, Name, ObjectExpr, Setter, SinglePattern, }; use rslint_parser::{AstNode, SyntaxKind, SyntaxNode}; @@ -19,9 +19,11 @@ impl ToFormatElement for SyntaxNode { SyntaxKind::JS_ARRAY_EXPRESSION => JsArrayExpression::cast(self.clone()) .unwrap() .to_format_element(formatter), - SyntaxKind::ARROW_EXPR => ArrowExpr::cast(self.clone()) - .unwrap() - .to_format_element(formatter), + SyntaxKind::JS_ARROW_FUNCTION_EXPRESSION => { + JsArrowFunctionExpression::cast(self.clone()) + .unwrap() + .to_format_element(formatter) + } SyntaxKind::ASSIGN_PATTERN => AssignPattern::cast(self.clone()) .unwrap() .to_format_element(formatter), diff --git a/crates/rome_formatter/src/ts/expr_or_block.rs b/crates/rome_formatter/src/ts/expr_or_block.rs deleted file mode 100644 index bdc4d97ff3eb..000000000000 --- a/crates/rome_formatter/src/ts/expr_or_block.rs +++ /dev/null @@ -1,11 +0,0 @@ -use crate::{FormatElement, FormatResult, Formatter, ToFormatElement}; -use rslint_parser::ast::ExprOrBlock; - -impl ToFormatElement for ExprOrBlock { - fn to_format_element(&self, formatter: &Formatter) -> FormatResult { - match self { - ExprOrBlock::JsFunctionBody(body) => body.to_format_element(formatter), - ExprOrBlock::JsAnyExpression(expr) => expr.to_format_element(formatter), - } - } -} diff --git a/crates/rome_formatter/src/ts/expressions/arrow_expr.rs b/crates/rome_formatter/src/ts/expressions/arrow_expr.rs index b9ca823b5cd3..a9bdebf81208 100644 --- a/crates/rome_formatter/src/ts/expressions/arrow_expr.rs +++ b/crates/rome_formatter/src/ts/expressions/arrow_expr.rs @@ -1,11 +1,13 @@ -use rslint_parser::ast::{ArrowExpr, ArrowExprParams}; +use rslint_parser::ast::{ + JsAnyArrowFunctionBody, JsAnyArrowFunctionParameters, JsArrowFunctionExpression, +}; use crate::{ concat_elements, format_elements, space_token, token, FormatElement, FormatResult, Formatter, ToFormatElement, }; -impl ToFormatElement for ArrowExpr { +impl ToFormatElement for JsArrowFunctionExpression { fn to_format_element(&self, formatter: &Formatter) -> FormatResult { let mut tokens: Vec = vec![]; @@ -16,14 +18,14 @@ impl ToFormatElement for ArrowExpr { )); } - if let Some(params) = self.params() { + if let Some(params) = self.parameter_list() { match params { - ArrowExprParams::Name(name) => { + JsAnyArrowFunctionParameters::JsIdentifierBinding(name) => { tokens.push(token("(")); tokens.push(formatter.format_node(name)?); tokens.push(token(")")); } - ArrowExprParams::JsParameterList(params) => { + JsAnyArrowFunctionParameters::JsParameterList(params) => { tokens.push(formatter.format_node(params)?) } } @@ -39,3 +41,12 @@ impl ToFormatElement for ArrowExpr { Ok(concat_elements(tokens)) } } + +impl ToFormatElement for JsAnyArrowFunctionBody { + fn to_format_element(&self, formatter: &Formatter) -> FormatResult { + match self { + JsAnyArrowFunctionBody::JsFunctionBody(body) => body.to_format_element(formatter), + JsAnyArrowFunctionBody::JsAnyExpression(expr) => expr.to_format_element(formatter), + } + } +} diff --git a/crates/rome_formatter/src/ts/expressions/expression.rs b/crates/rome_formatter/src/ts/expressions/expression.rs index c6e47ade2dc5..5376579071ea 100644 --- a/crates/rome_formatter/src/ts/expressions/expression.rs +++ b/crates/rome_formatter/src/ts/expressions/expression.rs @@ -4,7 +4,7 @@ use rslint_parser::ast::JsAnyExpression; impl ToFormatElement for JsAnyExpression { fn to_format_element(&self, formatter: &Formatter) -> FormatResult { match self { - JsAnyExpression::ArrowExpr(arrow) => arrow.to_format_element(formatter), + JsAnyExpression::JsArrowFunctionExpression(arrow) => arrow.to_format_element(formatter), JsAnyExpression::JsAnyLiteral(literal) => literal.to_format_element(formatter), JsAnyExpression::Template(_) => todo!(), JsAnyExpression::JsReferenceIdentifierExpression(name_ref) => { diff --git a/crates/rome_formatter/src/ts/mod.rs b/crates/rome_formatter/src/ts/mod.rs index 176c9ef5fbd1..3bbdca88191e 100644 --- a/crates/rome_formatter/src/ts/mod.rs +++ b/crates/rome_formatter/src/ts/mod.rs @@ -1,10 +1,10 @@ mod any_js_array_element; mod arg_list; mod auxiliary; +mod bindings; mod class; mod condition; mod declarators; -mod expr_or_block; mod expressions; mod getter; mod ident; @@ -17,7 +17,6 @@ mod script; mod setter; mod spread; mod statements; -mod bindings; #[cfg(test)] mod test { diff --git a/crates/rslint_parser/src/ast/expr_ext.rs b/crates/rslint_parser/src/ast/expr_ext.rs index 7ce42becca27..022d5de31ce5 100644 --- a/crates/rslint_parser/src/ast/expr_ext.rs +++ b/crates/rslint_parser/src/ast/expr_ext.rs @@ -333,9 +333,9 @@ impl JsStringLiteral { } } -impl ArrowExpr { - pub fn body(&self) -> Option { - ExprOrBlock::cast(self.syntax().children().last()?) +impl JsArrowFunctionExpression { + pub fn body(&self) -> Option { + JsAnyArrowFunctionBody::cast(self.syntax().children().last()?) } } diff --git a/crates/rslint_parser/src/ast/generated/nodes.rs b/crates/rslint_parser/src/ast/generated/nodes.rs index d60d64b230b0..b3f96ad845d6 100644 --- a/crates/rslint_parser/src/ast/generated/nodes.rs +++ b/crates/rslint_parser/src/ast/generated/nodes.rs @@ -772,6 +772,21 @@ impl JsArrayExpression { } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct JsArrowFunctionExpression { + pub(crate) syntax: SyntaxNode, +} +impl JsArrowFunctionExpression { + pub fn async_token(&self) -> Option { support::token(&self.syntax, T![async]) } + pub fn type_parameters(&self) -> Option { support::node(&self.syntax) } + pub fn parameter_list(&self) -> Option { + support::node(&self.syntax) + } + pub fn fat_arrow_token(&self) -> SyntaxResult { + support::required_token(&self.syntax, T ! [=>]) + } + pub fn return_type(&self) -> Option { support::node(&self.syntax) } +} +#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct JsAwaitExpression { pub(crate) syntax: SyntaxNode, } @@ -841,7 +856,7 @@ impl JsFunctionExpression { } pub fn star_token(&self) -> Option { support::token(&self.syntax, T ! [*]) } pub fn id(&self) -> Option { support::node(&self.syntax) } - pub fn type_params(&self) -> Option { support::node(&self.syntax) } + pub fn type_parameters(&self) -> Option { support::node(&self.syntax) } pub fn parameters(&self) -> SyntaxResult { support::required_node(&self.syntax) } @@ -970,20 +985,6 @@ impl JsYieldExpression { pub fn argument(&self) -> Option { support::node(&self.syntax) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct ArrowExpr { - pub(crate) syntax: SyntaxNode, -} -impl ArrowExpr { - pub fn async_token(&self) -> Option { support::token(&self.syntax, T![async]) } - pub fn type_params(&self) -> Option { support::node(&self.syntax) } - pub fn params(&self) -> Option { support::node(&self.syntax) } - pub fn fat_arrow_token(&self) -> SyntaxResult { - support::required_token(&self.syntax, T ! [=>]) - } - pub fn colon_token(&self) -> Option { support::token(&self.syntax, T ! [:]) } - pub fn return_type(&self) -> Option { support::node(&self.syntax) } -} -#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Template { pub(crate) syntax: SyntaxNode, } @@ -2326,6 +2327,7 @@ pub enum JsAnyStatement { pub enum JsAnyExpression { JsAnyLiteral(JsAnyLiteral), JsArrayExpression(JsArrayExpression), + JsArrowFunctionExpression(JsArrowFunctionExpression), JsAwaitExpression(JsAwaitExpression), JsBinaryExpression(JsBinaryExpression), JsConditionalExpression(JsConditionalExpression), @@ -2340,7 +2342,6 @@ pub enum JsAnyExpression { JsPreUpdateExpression(JsPreUpdateExpression), JsPostUpdateExpression(JsPostUpdateExpression), JsYieldExpression(JsYieldExpression), - ArrowExpr(ArrowExpr), Template(Template), ObjectExpr(ObjectExpr), BracketExpr(BracketExpr), @@ -2388,46 +2389,12 @@ pub enum JsAnyLiteral { JsRegexLiteral(JsRegexLiteral), } #[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub enum ArrowExprParams { - Name(Name), +pub enum JsAnyArrowFunctionParameters { JsParameterList(JsParameterList), + JsIdentifierBinding(JsIdentifierBinding), } #[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub enum TsType { - TsAny(TsAny), - TsUnknown(TsUnknown), - TsNumber(TsNumber), - TsObject(TsObject), - TsBoolean(TsBoolean), - TsBigint(TsBigint), - TsString(TsString), - TsSymbol(TsSymbol), - TsVoid(TsVoid), - TsUndefined(TsUndefined), - TsNull(TsNull), - TsNever(TsNever), - TsThis(TsThis), - TsLiteral(TsLiteral), - TsPredicate(TsPredicate), - TsTuple(TsTuple), - TsParen(TsParen), - TsTypeRef(TsTypeRef), - TsTemplate(TsTemplate), - TsMappedType(TsMappedType), - TsImport(TsImport), - TsArray(TsArray), - TsIndexedArray(TsIndexedArray), - TsTypeOperator(TsTypeOperator), - TsIntersection(TsIntersection), - TsUnion(TsUnion), - TsFnType(TsFnType), - TsConstructorType(TsConstructorType), - TsConditionalType(TsConditionalType), - TsObjectType(TsObjectType), - TsInfer(TsInfer), -} -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub enum ExprOrBlock { +pub enum JsAnyArrowFunctionBody { JsAnyExpression(JsAnyExpression), JsFunctionBody(JsFunctionBody), } @@ -2469,6 +2436,40 @@ pub enum ConstructorParamOrPat { Pattern(Pattern), } #[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub enum TsType { + TsAny(TsAny), + TsUnknown(TsUnknown), + TsNumber(TsNumber), + TsObject(TsObject), + TsBoolean(TsBoolean), + TsBigint(TsBigint), + TsString(TsString), + TsSymbol(TsSymbol), + TsVoid(TsVoid), + TsUndefined(TsUndefined), + TsNull(TsNull), + TsNever(TsNever), + TsThis(TsThis), + TsLiteral(TsLiteral), + TsPredicate(TsPredicate), + TsTuple(TsTuple), + TsParen(TsParen), + TsTypeRef(TsTypeRef), + TsTemplate(TsTemplate), + TsMappedType(TsMappedType), + TsImport(TsImport), + TsArray(TsArray), + TsIndexedArray(TsIndexedArray), + TsTypeOperator(TsTypeOperator), + TsIntersection(TsIntersection), + TsUnion(TsUnion), + TsFnType(TsFnType), + TsConstructorType(TsConstructorType), + TsConditionalType(TsConditionalType), + TsObjectType(TsObjectType), + TsInfer(TsInfer), +} +#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum JsAnyArrayElement { JsAnyExpression(JsAnyExpression), SpreadElement(SpreadElement), @@ -3159,6 +3160,17 @@ impl AstNode for JsArrayExpression { } fn syntax(&self) -> &SyntaxNode { &self.syntax } } +impl AstNode for JsArrowFunctionExpression { + fn can_cast(kind: SyntaxKind) -> bool { kind == JS_ARROW_FUNCTION_EXPRESSION } + fn cast(syntax: SyntaxNode) -> Option { + if Self::can_cast(syntax.kind()) { + Some(Self { syntax }) + } else { + None + } + } + fn syntax(&self) -> &SyntaxNode { &self.syntax } +} impl AstNode for JsAwaitExpression { fn can_cast(kind: SyntaxKind) -> bool { kind == JS_AWAIT_EXPRESSION } fn cast(syntax: SyntaxNode) -> Option { @@ -3313,17 +3325,6 @@ impl AstNode for JsYieldExpression { } fn syntax(&self) -> &SyntaxNode { &self.syntax } } -impl AstNode for ArrowExpr { - fn can_cast(kind: SyntaxKind) -> bool { kind == ARROW_EXPR } - fn cast(syntax: SyntaxNode) -> Option { - if Self::can_cast(syntax.kind()) { - Some(Self { syntax }) - } else { - None - } - } - fn syntax(&self) -> &SyntaxNode { &self.syntax } -} impl AstNode for Template { fn can_cast(kind: SyntaxKind) -> bool { kind == TEMPLATE } fn cast(syntax: SyntaxNode) -> Option { @@ -4838,6 +4839,11 @@ impl AstNode for JsAnyStatement { impl From for JsAnyExpression { fn from(node: JsArrayExpression) -> JsAnyExpression { JsAnyExpression::JsArrayExpression(node) } } +impl From for JsAnyExpression { + fn from(node: JsArrowFunctionExpression) -> JsAnyExpression { + JsAnyExpression::JsArrowFunctionExpression(node) + } +} impl From for JsAnyExpression { fn from(node: JsAwaitExpression) -> JsAnyExpression { JsAnyExpression::JsAwaitExpression(node) } } @@ -4900,9 +4906,6 @@ impl From for JsAnyExpression { impl From for JsAnyExpression { fn from(node: JsYieldExpression) -> JsAnyExpression { JsAnyExpression::JsYieldExpression(node) } } -impl From for JsAnyExpression { - fn from(node: ArrowExpr) -> JsAnyExpression { JsAnyExpression::ArrowExpr(node) } -} impl From