From f0eebb099c62dd91208139f13afdaa756d766416 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Mon, 15 Nov 2021 13:56:28 +0100 Subject: [PATCH 1/5] Function Declaration facade refinements * Rename `FnDecl` to `JsFunctionDeclaration` * Rename `ParameterList` to `JsParameterList` * Introduce `TsReturnType` to group the `: Type` * Restructure parsing implementation of function decl/expression * Introduce `JsRestParameter` --- crates/rome_formatter/src/cst.rs | 21 +- .../src/ts/bindings/identifier_binding.rs | 8 + crates/rome_formatter/src/ts/bindings/mod.rs | 1 + .../src/ts/declarators/fn_decl.rs | 8 +- .../src/ts/expressions/arrow_expr.rs | 2 +- crates/rome_formatter/src/ts/mod.rs | 1 + .../rome_formatter/src/ts/parameter_list.rs | 4 +- .../src/ts/statements/statement.rs | 2 +- .../rslint_parser/src/ast/generated/nodes.rs | 256 ++++++++++++++---- crates/rslint_parser/src/syntax.rs | 1 + crates/rslint_parser/src/syntax/decl.rs | 89 +----- crates/rslint_parser/src/syntax/expr.rs | 31 +-- crates/rslint_parser/src/syntax/function.rs | 121 +++++++++ crates/rslint_parser/src/syntax/program.rs | 24 +- crates/rslint_parser/src/syntax/stmt.rs | 17 +- crates/rslint_parser/src/syntax/typescript.rs | 11 +- .../err/binding_identifier_invalid.rast | 8 +- .../test_data/inline/err/directives_err.rast | 24 +- .../err/formal_params_no_binding_element.rast | 6 +- .../inline/err/function_decl_err.rast | 40 ++- .../inline/err/invalid_method_recover.rast | 2 +- .../paren_or_arrow_expr_invalid_params.rast | 2 +- .../test_data/inline/ok/async_arrow_expr.rast | 6 +- .../inline/ok/async_function_expr.rast | 6 +- .../test_data/inline/ok/async_method.rast | 4 +- .../test_data/inline/ok/await_expression.rast | 16 +- .../inline/ok/class_empty_element.rast | 2 +- .../test_data/inline/ok/class_expr.rast | 2 +- .../test_data/inline/ok/directives.rast | 256 +++++++++--------- .../test_data/inline/ok/function_decl.rast | 40 +-- .../test_data/inline/ok/function_expr.rast | 6 +- .../test_data/inline/ok/method_getter.rast | 2 +- .../test_data/inline/ok/method_setter.rast | 2 +- .../test_data/inline/ok/new_exprs.rast | 2 +- .../inline/ok/object_expr_async_method.rast | 4 +- .../ok/object_expr_generator_method.rast | 2 +- .../inline/ok/object_expr_getter_setter.rast | 2 +- .../object_expr_getter_setter_computed.rast | 2 +- .../inline/ok/object_expr_method.rast | 2 +- .../inline/ok/paren_or_arrow_expr.rast | 8 +- .../test_data/inline/ok/return_stmt.rast | 2 +- .../test_data/inline/ok/semicolons.rast | 6 +- .../test_data/inline/ok/static_method.rast | 8 +- .../test_data/inline/ok/with_statement.rast | 6 +- .../test_data/inline/ok/yield_expr.rast | 6 +- crates/rslint_syntax/src/generated.rs | 6 +- xtask/js.ungram | 64 +++-- xtask/src/codegen/kinds_src.rs | 6 +- xtask/src/coverage/test262 | 2 +- 49 files changed, 683 insertions(+), 466 deletions(-) create mode 100644 crates/rome_formatter/src/ts/bindings/identifier_binding.rs create mode 100644 crates/rome_formatter/src/ts/bindings/mod.rs create mode 100644 crates/rslint_parser/src/syntax/function.rs diff --git a/crates/rome_formatter/src/cst.rs b/crates/rome_formatter/src/cst.rs index 45e1cb3b798..d1f23cff0e8 100644 --- a/crates/rome_formatter/src/cst.rs +++ b/crates/rome_formatter/src/cst.rs @@ -1,14 +1,15 @@ use crate::{FormatElement, FormatResult, Formatter, ToFormatElement}; use rslint_parser::ast::{ ArgList, ArrayPattern, ArrowExpr, AssignPattern, CallExpr, ClassBody, ClassDecl, ClassProp, - Condition, ConstructorParameters, FnDecl, ForInStmt, ForStmt, ForStmtInit, ForStmtTest, - ForStmtUpdate, Getter, IdentProp, JsArrayExpression, JsBlockStatement, JsBooleanLiteral, - JsCaseClause, JsCatchClause, JsContinueStatement, JsDebuggerStatement, JsDefaultClause, - JsDoWhileStatement, JsEmptyStatement, JsExpressionStatement, JsFinallyClause, JsIfStatement, - JsLabeledStatement, JsNullLiteral, JsNumberLiteral, JsReferenceIdentifierExpression, - JsReturnStatement, JsRoot, JsSequenceExpression, JsStringLiteral, JsSwitchStatement, - JsTryStatement, JsVariableDeclarationStatement, JsVariableDeclarator, JsWhileStatement, - JsWithStatement, LiteralProp, Name, ObjectExpr, ParameterList, Setter, SinglePattern, + 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, }; use rslint_parser::{AstNode, SyntaxKind, SyntaxNode}; @@ -44,7 +45,7 @@ impl ToFormatElement for SyntaxNode { .unwrap() .to_format_element(formatter) } - SyntaxKind::PARAMETER_LIST => ParameterList::cast(self.clone()) + SyntaxKind::JS_PARAMETER_LIST => JsParameterList::cast(self.clone()) .unwrap() .to_format_element(formatter), SyntaxKind::JS_ROOT => JsRoot::cast(self.clone()) @@ -64,7 +65,7 @@ impl ToFormatElement for SyntaxNode { SyntaxKind::JS_VARIABLE_DECLARATOR => JsVariableDeclarator::cast(self.clone()) .unwrap() .to_format_element(formatter), - SyntaxKind::FN_DECL => FnDecl::cast(self.clone()) + SyntaxKind::JS_FUNCTION_DECLARATION => JsFunctionDeclaration::cast(self.clone()) .unwrap() .to_format_element(formatter), SyntaxKind::JS_SEQUENCE_EXPRESSION => JsSequenceExpression::cast(self.clone()) diff --git a/crates/rome_formatter/src/ts/bindings/identifier_binding.rs b/crates/rome_formatter/src/ts/bindings/identifier_binding.rs new file mode 100644 index 00000000000..5f2568f48ee --- /dev/null +++ b/crates/rome_formatter/src/ts/bindings/identifier_binding.rs @@ -0,0 +1,8 @@ +use crate::{FormatElement, FormatResult, Formatter, ToFormatElement}; +use rslint_parser::ast::JsIdentifierBinding; + +impl ToFormatElement for JsIdentifierBinding { + fn to_format_element(&self, formatter: &Formatter) -> FormatResult { + formatter.format_token(&self.name_token()?) + } +} diff --git a/crates/rome_formatter/src/ts/bindings/mod.rs b/crates/rome_formatter/src/ts/bindings/mod.rs new file mode 100644 index 00000000000..b4d583c2131 --- /dev/null +++ b/crates/rome_formatter/src/ts/bindings/mod.rs @@ -0,0 +1 @@ +mod identifier_binding; diff --git a/crates/rome_formatter/src/ts/declarators/fn_decl.rs b/crates/rome_formatter/src/ts/declarators/fn_decl.rs index bd47509d35e..cbbb8f0fa2c 100644 --- a/crates/rome_formatter/src/ts/declarators/fn_decl.rs +++ b/crates/rome_formatter/src/ts/declarators/fn_decl.rs @@ -1,9 +1,9 @@ use crate::{ concat_elements, space_token, FormatElement, FormatResult, Formatter, ToFormatElement, }; -use rslint_parser::ast::FnDecl; +use rslint_parser::ast::JsFunctionDeclaration; -impl ToFormatElement for FnDecl { +impl ToFormatElement for JsFunctionDeclaration { fn to_format_element(&self, formatter: &Formatter) -> FormatResult { let mut tokens = vec![]; @@ -19,8 +19,8 @@ impl ToFormatElement for FnDecl { } tokens.push(space_token()); - tokens.push(formatter.format_node(self.name()?)?); - tokens.push(formatter.format_node(self.parameters()?)?); + tokens.push(formatter.format_node(self.id()?)?); + tokens.push(formatter.format_node(self.parameter_list()?)?); tokens.push(space_token()); tokens.push(formatter.format_node(self.body()?)?); diff --git a/crates/rome_formatter/src/ts/expressions/arrow_expr.rs b/crates/rome_formatter/src/ts/expressions/arrow_expr.rs index 872702ac5a6..b9ca823b5cd 100644 --- a/crates/rome_formatter/src/ts/expressions/arrow_expr.rs +++ b/crates/rome_formatter/src/ts/expressions/arrow_expr.rs @@ -23,7 +23,7 @@ impl ToFormatElement for ArrowExpr { tokens.push(formatter.format_node(name)?); tokens.push(token(")")); } - ArrowExprParams::ParameterList(params) => { + ArrowExprParams::JsParameterList(params) => { tokens.push(formatter.format_node(params)?) } } diff --git a/crates/rome_formatter/src/ts/mod.rs b/crates/rome_formatter/src/ts/mod.rs index f80526554b7..176c9ef5fbd 100644 --- a/crates/rome_formatter/src/ts/mod.rs +++ b/crates/rome_formatter/src/ts/mod.rs @@ -17,6 +17,7 @@ mod script; mod setter; mod spread; mod statements; +mod bindings; #[cfg(test)] mod test { diff --git a/crates/rome_formatter/src/ts/parameter_list.rs b/crates/rome_formatter/src/ts/parameter_list.rs index 9373e9fa273..ddef2834b38 100644 --- a/crates/rome_formatter/src/ts/parameter_list.rs +++ b/crates/rome_formatter/src/ts/parameter_list.rs @@ -2,9 +2,9 @@ use crate::{ format_elements, group_elements, join_elements, soft_indent, soft_line_break_or_space, token, FormatElement, FormatResult, Formatter, ToFormatElement, }; -use rslint_parser::ast::ParameterList; +use rslint_parser::ast::JsParameterList; -impl ToFormatElement for ParameterList { +impl ToFormatElement for JsParameterList { fn to_format_element(&self, formatter: &Formatter) -> FormatResult { let param_tokens = formatter.format_nodes(self.parameters())?; diff --git a/crates/rome_formatter/src/ts/statements/statement.rs b/crates/rome_formatter/src/ts/statements/statement.rs index a55d305b28e..0dc13fd096e 100644 --- a/crates/rome_formatter/src/ts/statements/statement.rs +++ b/crates/rome_formatter/src/ts/statements/statement.rs @@ -49,7 +49,7 @@ impl ToFormatElement for JsAnyStatement { } JsAnyStatement::ForOfStmt(_) => todo!(), - JsAnyStatement::FnDecl(decl) => decl.to_format_element(formatter), + JsAnyStatement::JsFunctionDeclaration(decl) => decl.to_format_element(formatter), JsAnyStatement::ClassDecl(decl) => decl.to_format_element(formatter), JsAnyStatement::JsVariableDeclarationStatement(decl) => { decl.to_format_element(formatter) diff --git a/crates/rslint_parser/src/ast/generated/nodes.rs b/crates/rslint_parser/src/ast/generated/nodes.rs index a37f20ba9d9..e3220d56616 100644 --- a/crates/rslint_parser/src/ast/generated/nodes.rs +++ b/crates/rslint_parser/src/ast/generated/nodes.rs @@ -375,20 +375,21 @@ impl JsDebuggerStatement { pub fn semicolon_token(&self) -> Option { support::token(&self.syntax, T ! [;]) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct FnDecl { +pub struct JsFunctionDeclaration { pub(crate) syntax: SyntaxNode, } -impl FnDecl { +impl JsFunctionDeclaration { pub fn async_token(&self) -> Option { support::token(&self.syntax, T![async]) } pub fn function_token(&self) -> SyntaxResult { support::required_token(&self.syntax, T![function]) } pub fn star_token(&self) -> Option { support::token(&self.syntax, T ! [*]) } - pub fn name(&self) -> SyntaxResult { support::required_node(&self.syntax) } + pub fn id(&self) -> SyntaxResult { support::required_node(&self.syntax) } pub fn type_parameters(&self) -> Option { support::node(&self.syntax) } - pub fn parameters(&self) -> SyntaxResult { support::required_node(&self.syntax) } - pub fn colon_token(&self) -> Option { support::token(&self.syntax, T ! [:]) } - pub fn return_type(&self) -> Option { support::node(&self.syntax) } + pub fn parameter_list(&self) -> SyntaxResult { + support::required_node(&self.syntax) + } + pub fn return_type(&self) -> Option { support::node(&self.syntax) } pub fn body(&self) -> SyntaxResult { support::required_node(&self.syntax) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -1251,10 +1252,10 @@ impl JsFunctionBody { } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct ParameterList { +pub struct JsParameterList { pub(crate) syntax: SyntaxNode, } -impl ParameterList { +impl JsParameterList { pub fn l_paren_token(&self) -> SyntaxResult { support::required_token(&self.syntax, T!['(']) } @@ -1296,7 +1297,9 @@ impl Method { pub fn star_token(&self) -> Option { support::token(&self.syntax, T ! [*]) } pub fn name(&self) -> SyntaxResult { support::required_node(&self.syntax) } pub fn type_params(&self) -> Option { support::node(&self.syntax) } - pub fn parameters(&self) -> SyntaxResult { support::required_node(&self.syntax) } + pub fn parameters(&self) -> SyntaxResult { + support::required_node(&self.syntax) + } pub fn colon_token(&self) -> Option { support::token(&self.syntax, T ! [:]) } pub fn return_type(&self) -> Option { support::node(&self.syntax) } pub fn body(&self) -> SyntaxResult { support::required_node(&self.syntax) } @@ -1338,7 +1341,9 @@ impl Constructor { pub fn accessibility(&self) -> Option { support::node(&self.syntax) } pub fn name(&self) -> SyntaxResult { support::required_node(&self.syntax) } pub fn type_params(&self) -> Option { support::node(&self.syntax) } - pub fn parameters(&self) -> SyntaxResult { support::required_node(&self.syntax) } + pub fn parameters(&self) -> SyntaxResult { + support::required_node(&self.syntax) + } pub fn body(&self) -> SyntaxResult { support::required_node(&self.syntax) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -1370,7 +1375,9 @@ impl Getter { support::required_token(&self.syntax, T![get]) } pub fn key(&self) -> SyntaxResult { support::required_node(&self.syntax) } - pub fn parameters(&self) -> SyntaxResult { support::required_node(&self.syntax) } + pub fn parameters(&self) -> SyntaxResult { + support::required_node(&self.syntax) + } pub fn body(&self) -> SyntaxResult { support::required_node(&self.syntax) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -1382,7 +1389,9 @@ impl Setter { support::required_token(&self.syntax, T![set]) } pub fn key(&self) -> SyntaxResult { support::required_node(&self.syntax) } - pub fn parameters(&self) -> SyntaxResult { support::required_node(&self.syntax) } + pub fn parameters(&self) -> SyntaxResult { + support::required_node(&self.syntax) + } pub fn body(&self) -> SyntaxResult { support::required_node(&self.syntax) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -1655,6 +1664,25 @@ impl Condition { } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct JsIdentifierBinding { + pub(crate) syntax: SyntaxNode, +} +impl JsIdentifierBinding { + pub fn name_token(&self) -> SyntaxResult { + support::required_token(&self.syntax, T![ident]) + } +} +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct TsReturnType { + pub(crate) syntax: SyntaxNode, +} +impl TsReturnType { + pub fn colon_token(&self) -> SyntaxResult { + support::required_token(&self.syntax, T ! [:]) + } + pub fn ty(&self) -> SyntaxResult { support::required_node(&self.syntax) } +} +#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct JsVariableDeclarator { pub(crate) syntax: SyntaxNode, } @@ -1717,6 +1745,16 @@ impl Specifier { pub fn name(&self) -> SyntaxResult { support::required_node(&self.syntax) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct JsRestParameter { + pub(crate) syntax: SyntaxNode, +} +impl JsRestParameter { + pub fn dotdotdot_token(&self) -> SyntaxResult { + support::required_token(&self.syntax, T ! [...]) + } + pub fn binding(&self) -> SyntaxResult { support::required_node(&self.syntax) } +} +#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct TsExternalModuleRef { pub(crate) syntax: SyntaxNode, } @@ -1991,7 +2029,7 @@ pub struct TsFnType { pub(crate) syntax: SyntaxNode, } impl TsFnType { - pub fn params(&self) -> SyntaxResult { support::required_node(&self.syntax) } + pub fn params(&self) -> SyntaxResult { support::required_node(&self.syntax) } pub fn fat_arrow_token(&self) -> SyntaxResult { support::required_token(&self.syntax, T ! [=>]) } @@ -2005,7 +2043,7 @@ impl TsConstructorType { pub fn new_token(&self) -> SyntaxResult { support::required_token(&self.syntax, T![new]) } - pub fn params(&self) -> SyntaxResult { support::required_node(&self.syntax) } + pub fn params(&self) -> SyntaxResult { support::required_node(&self.syntax) } pub fn colon_token(&self) -> SyntaxResult { support::required_token(&self.syntax, T ! [:]) } @@ -2171,7 +2209,9 @@ pub struct TsCallSignatureDecl { } impl TsCallSignatureDecl { pub fn type_params(&self) -> SyntaxResult { support::required_node(&self.syntax) } - pub fn parameters(&self) -> SyntaxResult { support::required_node(&self.syntax) } + pub fn parameters(&self) -> SyntaxResult { + support::required_node(&self.syntax) + } pub fn colon_token(&self) -> SyntaxResult { support::required_token(&self.syntax, T ! [:]) } @@ -2186,7 +2226,9 @@ impl TsConstructSignatureDecl { support::required_token(&self.syntax, T![new]) } pub fn type_params(&self) -> SyntaxResult { support::required_node(&self.syntax) } - pub fn parameters(&self) -> SyntaxResult { support::required_node(&self.syntax) } + pub fn parameters(&self) -> SyntaxResult { + support::required_node(&self.syntax) + } pub fn colon_token(&self) -> Option { support::token(&self.syntax, T ! [:]) } pub fn return_type(&self) -> SyntaxResult { support::required_node(&self.syntax) } } @@ -2217,7 +2259,9 @@ impl TsMethodSignature { } pub fn key(&self) -> SyntaxResult { support::required_node(&self.syntax) } pub fn type_params(&self) -> SyntaxResult { support::required_node(&self.syntax) } - pub fn parameters(&self) -> SyntaxResult { support::required_node(&self.syntax) } + pub fn parameters(&self) -> SyntaxResult { + support::required_node(&self.syntax) + } pub fn question_mark_token(&self) -> Option { support::token(&self.syntax, T ! [?]) } @@ -2258,7 +2302,7 @@ pub enum JsAnyStatement { JsTryStatement(JsTryStatement), JsTryFinallyStatement(JsTryFinallyStatement), JsDebuggerStatement(JsDebuggerStatement), - FnDecl(FnDecl), + JsFunctionDeclaration(JsFunctionDeclaration), ClassDecl(ClassDecl), JsVariableDeclarationStatement(JsVariableDeclarationStatement), TsEnum(TsEnum), @@ -2379,7 +2423,7 @@ pub enum TsType { #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum ArrowExprParams { Name(Name), - ParameterList(ParameterList), + JsParameterList(JsParameterList), } #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum ExprOrBlock { @@ -2451,13 +2495,14 @@ pub enum ImportClause { } #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum DefaultDecl { - FnDecl(FnDecl), + JsFunctionDeclaration(JsFunctionDeclaration), ClassDecl(ClassDecl), } #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum JsAnyExportDeclaration { - FnDecl(FnDecl), + JsFunctionDeclaration(JsFunctionDeclaration), ClassDecl(ClassDecl), + JsVariableDeclarationStatement(JsVariableDeclarationStatement), TsEnum(TsEnum), TsTypeAliasDecl(TsTypeAliasDecl), TsNamespaceDecl(TsNamespaceDecl), @@ -2465,6 +2510,11 @@ pub enum JsAnyExportDeclaration { TsInterfaceDecl(TsInterfaceDecl), } #[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub enum JsAnyParameter { + Pattern(Pattern), + JsRestParameter(JsRestParameter), +} +#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum TsModuleRef { TsExternalModuleRef(TsExternalModuleRef), TsEntityName(TsEntityName), @@ -2800,8 +2850,8 @@ impl AstNode for JsDebuggerStatement { } fn syntax(&self) -> &SyntaxNode { &self.syntax } } -impl AstNode for FnDecl { - fn can_cast(kind: SyntaxKind) -> bool { kind == FN_DECL } +impl AstNode for JsFunctionDeclaration { + fn can_cast(kind: SyntaxKind) -> bool { kind == JS_FUNCTION_DECLARATION } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { Some(Self { syntax }) @@ -3493,8 +3543,8 @@ impl AstNode for JsFunctionBody { } fn syntax(&self) -> &SyntaxNode { &self.syntax } } -impl AstNode for ParameterList { - fn can_cast(kind: SyntaxKind) -> bool { kind == PARAMETER_LIST } +impl AstNode for JsParameterList { + fn can_cast(kind: SyntaxKind) -> bool { kind == JS_PARAMETER_LIST } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { Some(Self { syntax }) @@ -3878,6 +3928,28 @@ impl AstNode for Condition { } fn syntax(&self) -> &SyntaxNode { &self.syntax } } +impl AstNode for JsIdentifierBinding { + fn can_cast(kind: SyntaxKind) -> bool { kind == JS_IDENTIFIER_BINDING } + 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 TsReturnType { + fn can_cast(kind: SyntaxKind) -> bool { kind == TS_RETURN_TYPE } + 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 JsVariableDeclarator { fn can_cast(kind: SyntaxKind) -> bool { kind == JS_VARIABLE_DECLARATOR } fn cast(syntax: SyntaxNode) -> Option { @@ -3944,6 +4016,17 @@ impl AstNode for Specifier { } fn syntax(&self) -> &SyntaxNode { &self.syntax } } +impl AstNode for JsRestParameter { + fn can_cast(kind: SyntaxKind) -> bool { kind == JS_REST_PARAMETER } + 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 TsExternalModuleRef { fn can_cast(kind: SyntaxKind) -> bool { kind == TS_EXTERNAL_MODULE_REF } fn cast(syntax: SyntaxNode) -> Option { @@ -4537,8 +4620,10 @@ impl From for JsAnyStatement { JsAnyStatement::JsDebuggerStatement(node) } } -impl From for JsAnyStatement { - fn from(node: FnDecl) -> JsAnyStatement { JsAnyStatement::FnDecl(node) } +impl From for JsAnyStatement { + fn from(node: JsFunctionDeclaration) -> JsAnyStatement { + JsAnyStatement::JsFunctionDeclaration(node) + } } impl From for JsAnyStatement { fn from(node: ClassDecl) -> JsAnyStatement { JsAnyStatement::ClassDecl(node) } @@ -4617,7 +4702,7 @@ impl AstNode for JsAnyStatement { | JS_TRY_STATEMENT | JS_TRY_FINALLY_STATEMENT | JS_DEBUGGER_STATEMENT - | FN_DECL + | JS_FUNCTION_DECLARATION | CLASS_DECL | JS_VARIABLE_DECLARATION_STATEMENT | TS_ENUM @@ -4671,7 +4756,9 @@ impl AstNode for JsAnyStatement { JS_DEBUGGER_STATEMENT => { JsAnyStatement::JsDebuggerStatement(JsDebuggerStatement { syntax }) } - FN_DECL => JsAnyStatement::FnDecl(FnDecl { syntax }), + JS_FUNCTION_DECLARATION => { + JsAnyStatement::JsFunctionDeclaration(JsFunctionDeclaration { syntax }) + } CLASS_DECL => JsAnyStatement::ClassDecl(ClassDecl { syntax }), JS_VARIABLE_DECLARATION_STATEMENT => { JsAnyStatement::JsVariableDeclarationStatement(JsVariableDeclarationStatement { @@ -4726,7 +4813,7 @@ impl AstNode for JsAnyStatement { JsAnyStatement::JsTryStatement(it) => &it.syntax, JsAnyStatement::JsTryFinallyStatement(it) => &it.syntax, JsAnyStatement::JsDebuggerStatement(it) => &it.syntax, - JsAnyStatement::FnDecl(it) => &it.syntax, + JsAnyStatement::JsFunctionDeclaration(it) => &it.syntax, JsAnyStatement::ClassDecl(it) => &it.syntax, JsAnyStatement::JsVariableDeclarationStatement(it) => &it.syntax, JsAnyStatement::TsEnum(it) => &it.syntax, @@ -5349,20 +5436,20 @@ impl AstNode for TsType { impl From for ArrowExprParams { fn from(node: Name) -> ArrowExprParams { ArrowExprParams::Name(node) } } -impl From for ArrowExprParams { - fn from(node: ParameterList) -> ArrowExprParams { ArrowExprParams::ParameterList(node) } +impl From for ArrowExprParams { + fn from(node: JsParameterList) -> ArrowExprParams { ArrowExprParams::JsParameterList(node) } } impl AstNode for ArrowExprParams { fn can_cast(kind: SyntaxKind) -> bool { match kind { - NAME | PARAMETER_LIST => true, + NAME | JS_PARAMETER_LIST => true, _ => false, } } fn cast(syntax: SyntaxNode) -> Option { let res = match syntax.kind() { NAME => ArrowExprParams::Name(Name { syntax }), - PARAMETER_LIST => ArrowExprParams::ParameterList(ParameterList { syntax }), + JS_PARAMETER_LIST => ArrowExprParams::JsParameterList(JsParameterList { syntax }), _ => return None, }; Some(res) @@ -5370,7 +5457,7 @@ impl AstNode for ArrowExprParams { fn syntax(&self) -> &SyntaxNode { match self { ArrowExprParams::Name(it) => &it.syntax, - ArrowExprParams::ParameterList(it) => &it.syntax, + ArrowExprParams::JsParameterList(it) => &it.syntax, } } } @@ -5763,8 +5850,8 @@ impl AstNode for ImportClause { } } } -impl From for DefaultDecl { - fn from(node: FnDecl) -> DefaultDecl { DefaultDecl::FnDecl(node) } +impl From for DefaultDecl { + fn from(node: JsFunctionDeclaration) -> DefaultDecl { DefaultDecl::JsFunctionDeclaration(node) } } impl From for DefaultDecl { fn from(node: ClassDecl) -> DefaultDecl { DefaultDecl::ClassDecl(node) } @@ -5772,13 +5859,15 @@ impl From for DefaultDecl { impl AstNode for DefaultDecl { fn can_cast(kind: SyntaxKind) -> bool { match kind { - FN_DECL | CLASS_DECL => true, + JS_FUNCTION_DECLARATION | CLASS_DECL => true, _ => false, } } fn cast(syntax: SyntaxNode) -> Option { let res = match syntax.kind() { - FN_DECL => DefaultDecl::FnDecl(FnDecl { syntax }), + JS_FUNCTION_DECLARATION => { + DefaultDecl::JsFunctionDeclaration(JsFunctionDeclaration { syntax }) + } CLASS_DECL => DefaultDecl::ClassDecl(ClassDecl { syntax }), _ => return None, }; @@ -5786,17 +5875,24 @@ impl AstNode for DefaultDecl { } fn syntax(&self) -> &SyntaxNode { match self { - DefaultDecl::FnDecl(it) => &it.syntax, + DefaultDecl::JsFunctionDeclaration(it) => &it.syntax, DefaultDecl::ClassDecl(it) => &it.syntax, } } } -impl From for JsAnyExportDeclaration { - fn from(node: FnDecl) -> JsAnyExportDeclaration { JsAnyExportDeclaration::FnDecl(node) } +impl From for JsAnyExportDeclaration { + fn from(node: JsFunctionDeclaration) -> JsAnyExportDeclaration { + JsAnyExportDeclaration::JsFunctionDeclaration(node) + } } impl From for JsAnyExportDeclaration { fn from(node: ClassDecl) -> JsAnyExportDeclaration { JsAnyExportDeclaration::ClassDecl(node) } } +impl From for JsAnyExportDeclaration { + fn from(node: JsVariableDeclarationStatement) -> JsAnyExportDeclaration { + JsAnyExportDeclaration::JsVariableDeclarationStatement(node) + } +} impl From for JsAnyExportDeclaration { fn from(node: TsEnum) -> JsAnyExportDeclaration { JsAnyExportDeclaration::TsEnum(node) } } @@ -5823,15 +5919,28 @@ impl From for JsAnyExportDeclaration { impl AstNode for JsAnyExportDeclaration { fn can_cast(kind: SyntaxKind) -> bool { match kind { - FN_DECL | CLASS_DECL | TS_ENUM | TS_TYPE_ALIAS_DECL | TS_NAMESPACE_DECL - | TS_MODULE_DECL | TS_INTERFACE_DECL => true, + JS_FUNCTION_DECLARATION + | CLASS_DECL + | JS_VARIABLE_DECLARATION_STATEMENT + | TS_ENUM + | TS_TYPE_ALIAS_DECL + | TS_NAMESPACE_DECL + | TS_MODULE_DECL + | TS_INTERFACE_DECL => true, _ => false, } } fn cast(syntax: SyntaxNode) -> Option { let res = match syntax.kind() { - FN_DECL => JsAnyExportDeclaration::FnDecl(FnDecl { syntax }), + JS_FUNCTION_DECLARATION => { + JsAnyExportDeclaration::JsFunctionDeclaration(JsFunctionDeclaration { syntax }) + } CLASS_DECL => JsAnyExportDeclaration::ClassDecl(ClassDecl { syntax }), + JS_VARIABLE_DECLARATION_STATEMENT => { + JsAnyExportDeclaration::JsVariableDeclarationStatement( + JsVariableDeclarationStatement { syntax }, + ) + } TS_ENUM => JsAnyExportDeclaration::TsEnum(TsEnum { syntax }), TS_TYPE_ALIAS_DECL => { JsAnyExportDeclaration::TsTypeAliasDecl(TsTypeAliasDecl { syntax }) @@ -5849,8 +5958,9 @@ impl AstNode for JsAnyExportDeclaration { } fn syntax(&self) -> &SyntaxNode { match self { - JsAnyExportDeclaration::FnDecl(it) => &it.syntax, + JsAnyExportDeclaration::JsFunctionDeclaration(it) => &it.syntax, JsAnyExportDeclaration::ClassDecl(it) => &it.syntax, + JsAnyExportDeclaration::JsVariableDeclarationStatement(it) => &it.syntax, JsAnyExportDeclaration::TsEnum(it) => &it.syntax, JsAnyExportDeclaration::TsTypeAliasDecl(it) => &it.syntax, JsAnyExportDeclaration::TsNamespaceDecl(it) => &it.syntax, @@ -5859,6 +5969,36 @@ impl AstNode for JsAnyExportDeclaration { } } } +impl From for JsAnyParameter { + fn from(node: JsRestParameter) -> JsAnyParameter { JsAnyParameter::JsRestParameter(node) } +} +impl AstNode for JsAnyParameter { + fn can_cast(kind: SyntaxKind) -> bool { + match kind { + JS_REST_PARAMETER => true, + k if Pattern::can_cast(k) => true, + _ => false, + } + } + fn cast(syntax: SyntaxNode) -> Option { + let res = match syntax.kind() { + JS_REST_PARAMETER => JsAnyParameter::JsRestParameter(JsRestParameter { syntax }), + _ => { + if let Some(pattern) = Pattern::cast(syntax) { + return Some(JsAnyParameter::Pattern(pattern)); + } + return None; + } + }; + Some(res) + } + fn syntax(&self) -> &SyntaxNode { + match self { + JsAnyParameter::JsRestParameter(it) => &it.syntax, + JsAnyParameter::Pattern(it) => it.syntax(), + } + } +} impl From for TsModuleRef { fn from(node: TsExternalModuleRef) -> TsModuleRef { TsModuleRef::TsExternalModuleRef(node) } } @@ -6125,6 +6265,11 @@ impl std::fmt::Display for JsAnyExportDeclaration { std::fmt::Display::fmt(self.syntax(), f) } } +impl std::fmt::Display for JsAnyParameter { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + std::fmt::Display::fmt(self.syntax(), f) + } +} impl std::fmt::Display for TsModuleRef { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { std::fmt::Display::fmt(self.syntax(), f) @@ -6290,7 +6435,7 @@ impl std::fmt::Display for JsDebuggerStatement { std::fmt::Display::fmt(self.syntax(), f) } } -impl std::fmt::Display for FnDecl { +impl std::fmt::Display for JsFunctionDeclaration { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { std::fmt::Display::fmt(self.syntax(), f) } @@ -6605,7 +6750,7 @@ impl std::fmt::Display for JsFunctionBody { std::fmt::Display::fmt(self.syntax(), f) } } -impl std::fmt::Display for ParameterList { +impl std::fmt::Display for JsParameterList { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { std::fmt::Display::fmt(self.syntax(), f) } @@ -6780,6 +6925,16 @@ impl std::fmt::Display for Condition { std::fmt::Display::fmt(self.syntax(), f) } } +impl std::fmt::Display for JsIdentifierBinding { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + std::fmt::Display::fmt(self.syntax(), f) + } +} +impl std::fmt::Display for TsReturnType { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + std::fmt::Display::fmt(self.syntax(), f) + } +} impl std::fmt::Display for JsVariableDeclarator { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { std::fmt::Display::fmt(self.syntax(), f) @@ -6810,6 +6965,11 @@ impl std::fmt::Display for Specifier { std::fmt::Display::fmt(self.syntax(), f) } } +impl std::fmt::Display for JsRestParameter { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + std::fmt::Display::fmt(self.syntax(), f) + } +} impl std::fmt::Display for TsExternalModuleRef { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { std::fmt::Display::fmt(self.syntax(), f) diff --git a/crates/rslint_parser/src/syntax.rs b/crates/rslint_parser/src/syntax.rs index 7a63a7eb582..bdfd76f53cc 100644 --- a/crates/rslint_parser/src/syntax.rs +++ b/crates/rslint_parser/src/syntax.rs @@ -9,6 +9,7 @@ pub mod decl; pub mod expr; +mod function; pub mod pat; pub mod program; pub mod stmt; diff --git a/crates/rslint_parser/src/syntax/decl.rs b/crates/rslint_parser/src/syntax/decl.rs index f048e7cd455..5e10c010162 100644 --- a/crates/rslint_parser/src/syntax/decl.rs +++ b/crates/rslint_parser/src/syntax/decl.rs @@ -1,11 +1,11 @@ //! Class and function declarations. use super::expr::{assign_expr, identifier_name, object_prop_name, STARTS_EXPR}; -use super::pat::{binding_identifier, opt_binding_identifier, pattern}; +use super::pat::{binding_identifier, pattern}; use super::typescript::*; +use crate::syntax::function::{args_body, fn_body}; use crate::syntax::stmt::function_body; use crate::{SyntaxKind::*, *}; -use std::collections::HashMap; pub const BASE_METHOD_RECOVERY_SET: TokenSet = token_set![ T!['['], @@ -81,85 +81,6 @@ fn class_prop_name(p: &mut Parser) -> Option<(CompletedMarker, Option)> { } } -fn args_body(p: &mut Parser) { - if p.at(T![<]) { - if let Some(ref mut ty) = ts_type_params(p) { - ty.err_if_not_ts(p, "type parameters can only be used in TypeScript files"); - } - } - formal_parameters(p); - if p.at(T![:]) { - if let Some(ref mut ty) = ts_type_or_type_predicate_ann(p, T![:]) { - ty.err_if_not_ts(p, "return types can only be used in TypeScript files"); - } - } - fn_body(p); -} - -fn fn_body(p: &mut Parser) { - // omitting the body is allowed in ts - if p.typescript() && !p.at(T!['{']) && is_semi(p, 0) { - p.eat(T![;]); - } else { - let mut complete = function_body(p, None); - if let Some(ref mut block) = complete { - if p.state.in_declare { - let err = p - .err_builder( - "function implementations cannot be given in ambient (declare) contexts", - ) - .primary(block.range(p), ""); - - p.error(err); - block.change_kind(p, ERROR); - } - } - } -} - -/// A function declaration, this could be async and or a generator. This takes a marker -/// because you need to first advance over async or start a marker and feed it in. -// test function_decl -// function foo() {} -// function *foo() {} -// function foo(await) {} -// async function *foo() {} -// async function foo() {} -// function *foo() { -// yield foo; -// } -pub fn function_decl(p: &mut Parser, m: Marker, fn_expr: bool) -> CompletedMarker { - // test_err function_decl_err - // function() {} - // function {} - // function *() {} - // async function() {} - // async function *() {} - // function *foo() {} - // yield foo; - p.expect(T![function]); - let in_generator = p.eat(T![*]); - let guard = &mut *p.with_state(ParserState { - labels: HashMap::new(), - in_function: true, - in_generator, - ..p.state.clone() - }); - - let complete = opt_binding_identifier(guard); - if complete.is_none() && !fn_expr { - let err = guard - .err_builder( - "expected a name for the function in a function declaration, but found none", - ) - .primary(guard.cur_tok().range, ""); - - guard.error(err); - } - args_body(guard); - m.complete(guard, FN_DECL) -} - #[allow(clippy::unnecessary_unwrap)] fn formal_param_pat(p: &mut Parser) -> Option { let m = p.start(); @@ -295,7 +216,7 @@ fn constructor_param_pat(p: &mut Parser) -> Option { } } -pub fn formal_parameters(p: &mut Parser) -> CompletedMarker { +pub fn parameter_list(p: &mut Parser) -> CompletedMarker { parameters_common(p, false) } @@ -367,7 +288,7 @@ fn parameters_common(p: &mut Parser, constructor_params: bool) -> CompletedMarke p.error(err); m.complete(p, ERROR); } - let complete = m.complete(p, REST_PATTERN); + let complete = m.complete(p, JS_REST_PARAMETER); // FIXME: this should be handled better, we should keep trying to parse params but issue an error for each one // which would allow for better recovery from `foo, ...bar, foo` @@ -430,7 +351,7 @@ fn parameters_common(p: &mut Parser, constructor_params: bool) -> CompletedMarke parameters_list.complete(p, LIST); p.state.allow_object_expr = true; p.expect(T![')']); - m.complete(p, PARAMETER_LIST) + m.complete(p, JS_PARAMETER_LIST) } pub fn arrow_body(p: &mut Parser) -> Option { diff --git a/crates/rslint_parser/src/syntax/expr.rs b/crates/rslint_parser/src/syntax/expr.rs index 5a2ee5cd82f..f5b6c5545b4 100644 --- a/crates/rslint_parser/src/syntax/expr.rs +++ b/crates/rslint_parser/src/syntax/expr.rs @@ -5,12 +5,11 @@ use syntax::decl::is_semi; -use super::decl::{ - arrow_body, class_decl, formal_parameters, function_decl, maybe_private_name, method, -}; +use super::decl::{arrow_body, class_decl, parameter_list, maybe_private_name, method}; use super::pat::pattern; use super::typescript::*; use super::util::*; +use crate::syntax::function::function_expression; use crate::{SyntaxKind::*, *}; pub const EXPR_RECOVERY_SET: TokenSet = token_set![VAR_KW, R_PAREN, L_PAREN, L_BRACK, R_BRACK]; @@ -724,7 +723,7 @@ pub fn paren_or_arrow_expr(p: &mut Parser, can_be_arrow: bool) -> CompletedMarke let expr = assign_expr(&mut *temp); if expr.is_some() && temp.at(T![:]) { temp.rewind(checkpoint); - params_marker = Some(formal_parameters(&mut *temp)); + params_marker = Some(parameter_list(&mut *temp)); break; } @@ -758,7 +757,7 @@ pub fn paren_or_arrow_expr(p: &mut Parser, can_be_arrow: bool) -> CompletedMarke ..p.state.clone() }); p.rewind(checkpoint); - formal_parameters(p); + parameter_list(p); if p.at(T![:]) { if let Some(mut ret) = ts_type_or_type_predicate_ann(p, T![:]) { ret.err_if_not_ts( @@ -797,7 +796,7 @@ pub fn paren_or_arrow_expr(p: &mut Parser, can_be_arrow: bool) -> CompletedMarke if params_marker.is_none() { // Rewind the parser so we can reparse as formal parameters p.rewind(checkpoint); - formal_parameters(p); + parameter_list(p); } if p.at(T![:]) { @@ -915,18 +914,7 @@ pub fn primary_expr(p: &mut Parser) -> Option { // let a = async function() {}; // let b = async function foo() {}; if p.nth_at(1, T![function]) { - let m = p.start(); - p.bump_remap(T![async]); - let mut complete = function_decl( - &mut *p.with_state(ParserState { - in_async: true, - ..p.state.clone() - }), - m, - true, - ); - complete.change_kind(p, FN_EXPR); - complete + function_expression(p) } else { // `async a => {}` and `async (a) => {}` if p.state.potential_arrow_start @@ -939,7 +927,7 @@ pub fn primary_expr(p: &mut Parser) -> Option { let m = p.start(); p.bump_remap(T![async]); if p.at(T!['(']) { - formal_parameters(p); + parameter_list(p); } else { let m = p.start(); // test_err async_arrow_expr_await_parameter @@ -971,10 +959,7 @@ pub fn primary_expr(p: &mut Parser) -> Option { // test function_expr // let a = function() {} // let b = function foo() {} - let m = p.start(); - let mut complete = function_decl(p, m, true); - complete.change_kind(p, FN_EXPR); - complete + function_expression(p) } T![ident] | T![yield] | T![await] => { // test identifier_reference diff --git a/crates/rslint_parser/src/syntax/function.rs b/crates/rslint_parser/src/syntax/function.rs new file mode 100644 index 00000000000..c58bc3c8a81 --- /dev/null +++ b/crates/rslint_parser/src/syntax/function.rs @@ -0,0 +1,121 @@ +use crate::syntax::decl::{is_semi, parameter_list}; +use crate::syntax::pat::{binding_identifier, opt_binding_identifier}; +use crate::syntax::stmt::function_body; +use crate::syntax::typescript::{ts_type_or_type_predicate_ann, ts_type_params}; +use crate::{CompletedMarker, Parser, ParserState}; +use rslint_syntax::SyntaxKind::{ + ERROR, FN_EXPR, JS_FUNCTION_DECLARATION, JS_IDENTIFIER_BINDING, TS_RETURN_TYPE, +}; +use rslint_syntax::{SyntaxKind, T}; +use std::collections::HashMap; + +/// A function declaration, this could be async and or a generator. This takes a marker +/// because you need to first advance over async or start a marker and feed it in. +// test function_decl +// function foo() {} +// function *foo() {} +// function foo(await) {} +// async function *foo() {} +// async function foo() {} +// function *foo() { +// yield foo; +// } +pub(super) fn function_declaration(p: &mut Parser) -> CompletedMarker { + function(p, JS_FUNCTION_DECLARATION) +} + +pub(super) fn function_expression(p: &mut Parser) -> CompletedMarker { + function(p, FN_EXPR) +} + +fn function(p: &mut Parser, kind: SyntaxKind) -> CompletedMarker { + let m = p.start(); + + if kind == JS_FUNCTION_DECLARATION { + // TS function declaration + p.eat(T![declare]); + } + + let in_async = p.at(T![ident]) && p.cur_src() == "async"; + if in_async { + p.bump_remap(T![async]); + } + + p.expect(T![function]); + + let in_generator = p.eat(T![*]); + let guard = &mut *p.with_state(ParserState { + labels: HashMap::new(), + in_function: true, + in_async, + in_generator, + ..p.state.clone() + }); + + let id = opt_binding_identifier(guard); + + if let Some(mut identifier_marker) = id { + identifier_marker.change_kind(guard, JS_IDENTIFIER_BINDING); + } else if kind == JS_FUNCTION_DECLARATION { + let err = guard + .err_builder( + "expected a name for the function in a function declaration, but found none", + ) + .primary(guard.cur_tok().range, ""); + + guard.error(err); + } + + parameter_types(guard); + parameter_list(guard); + return_type(guard); + fn_body(guard); + + m.complete(guard, kind) +} + +pub(super) fn fn_body(p: &mut Parser) { + // omitting the body is allowed in ts + if p.typescript() && !p.at(T!['{']) && is_semi(p, 0) { + p.eat(T![;]); + } else { + let mut complete = function_body(p, None); + if let Some(ref mut block) = complete { + if p.state.in_declare { + let err = p + .err_builder( + "function implementations cannot be given in ambient (declare) contexts", + ) + .primary(block.range(p), ""); + + p.error(err); + block.change_kind(p, ERROR); + } + } + } +} + +pub(super) fn args_body(p: &mut Parser) { + parameter_types(p); + parameter_list(p); + return_type(p); + fn_body(p); +} + +fn parameter_types(p: &mut Parser) { + if p.at(T![<]) { + if let Some(ref mut ty) = ts_type_params(p) { + ty.err_if_not_ts(p, "type parameters can only be used in TypeScript files"); + } + } +} + +fn return_type(p: &mut Parser) { + if p.at(T![:]) { + let return_type = p.start(); + if let Some(ref mut ty) = ts_type_or_type_predicate_ann(p, T![:]) { + ty.err_if_not_ts(p, "return types can only be used in TypeScript files"); + } + return_type.complete(p, TS_RETURN_TYPE); + } +} diff --git a/crates/rslint_parser/src/syntax/program.rs b/crates/rslint_parser/src/syntax/program.rs index 069afa088dc..34845c7eb71 100644 --- a/crates/rslint_parser/src/syntax/program.rs +++ b/crates/rslint_parser/src/syntax/program.rs @@ -2,11 +2,12 @@ use syntax::stmt::FOLLOWS_LET; -use super::decl::{class_decl, function_decl}; +use super::decl::class_decl; use super::expr::{assign_expr, expr, identifier_name, literal, object_expr, primary_expr}; use super::pat::binding_identifier; use super::stmt::{semi, statements, variable_declaration_statement}; use super::typescript::*; +use crate::syntax::function::function_declaration; use crate::syntax::stmt::directives; use crate::{SyntaxKind::*, *}; @@ -410,14 +411,8 @@ pub fn export_decl(p: &mut Parser) -> CompletedMarker { if p.cur_src() == "async" && p.nth_at(1, T![function]) && !p.has_linebreak_before_n(1) { p.state.decorators_were_valid = true; - let mut guard = p.with_state(ParserState { - in_async: true, - ..p.state.clone() - }); - let inner = guard.start(); - guard.bump_any(); - function_decl(&mut *guard, inner, false); - return m.complete(&mut *guard, EXPORT_DEFAULT_DECL); + function_declaration(p); + return m.complete(p, EXPORT_DEFAULT_DECL); } if p.cur_src() == "from" || (p.at(T![,]) && p.nth_at(1, T!['{'])) { @@ -438,17 +433,10 @@ pub fn export_decl(p: &mut Parser) -> CompletedMarker { && !p.has_linebreak_before_n(1) { p.state.decorators_were_valid = true; - let mut guard = p.with_state(ParserState { - in_async: true, - ..p.state.clone() - }); - let inner = guard.start(); - guard.bump_any(); - function_decl(&mut *guard, inner, false); + function_declaration(p); } else if !only_ty && p.at(T![function]) { p.state.decorators_were_valid = true; - let m = p.start(); - function_decl(p, m, false); + function_declaration(p); } else if !only_ty && p.at(T![const]) && p.nth_src(1) == "enum" { ts_enum(p).err_if_not_ts(p, "enums can only be used in TypeScript files"); } else if !only_ty diff --git a/crates/rslint_parser/src/syntax/stmt.rs b/crates/rslint_parser/src/syntax/stmt.rs index 394246f1ecb..1ee10a1d9d7 100644 --- a/crates/rslint_parser/src/syntax/stmt.rs +++ b/crates/rslint_parser/src/syntax/stmt.rs @@ -2,12 +2,13 @@ //! //! See the [ECMAScript spec](https://www.ecma-international.org/ecma-262/5.1/#sec-12). -use super::decl::{class_decl, decorators, function_decl}; +use super::decl::{class_decl, decorators}; use super::expr::{assign_expr, expr, EXPR_RECOVERY_SET, STARTS_EXPR}; use super::pat::*; use super::program::{export_decl, import_decl}; use super::typescript::*; use super::util::{check_for_stmt_declaration, check_label_use, check_lhs}; +use crate::syntax::function::{function_declaration, function_expression}; use crate::{SyntaxKind::*, *}; pub const STMT_RECOVERY_SET: TokenSet = token_set![ @@ -98,9 +99,8 @@ pub fn stmt( T![debugger] => debugger_stmt(p), T![function] => { p.state.decorators_were_valid = true; - let m = decorator.map(|x| x.precede(p)).unwrap_or_else(|| p.start()); // TODO: Should we change this to fn_expr if there is no name? - function_decl(p, m, true) + function_declaration(p) } T![class] => { p.state.decorators_were_valid = true; @@ -119,16 +119,7 @@ pub fn stmt( && !p.has_linebreak_before_n(1) => { p.state.decorators_were_valid = true; - let m = decorator.map(|x| x.precede(p)).unwrap_or_else(|| p.start()); - p.bump_any(); - function_decl( - &mut *p.with_state(ParserState { - in_async: true, - ..p.state.clone() - }), - m, - true, - ) + function_expression(p) } T![ident] if p.cur_src() == "let" && FOLLOWS_LET.contains(p.nth(1)) => { diff --git a/crates/rslint_parser/src/syntax/typescript.rs b/crates/rslint_parser/src/syntax/typescript.rs index fec858136de..ba901c478e4 100644 --- a/crates/rslint_parser/src/syntax/typescript.rs +++ b/crates/rslint_parser/src/syntax/typescript.rs @@ -3,6 +3,7 @@ use super::decl::*; use super::expr::{assign_expr, identifier_name, lhs_expr, literal}; use super::stmt::{semi, statements, variable_declaration_statement}; +use crate::syntax::function::function_declaration; use crate::{SyntaxKind::*, *}; pub const BASE_TS_RECOVERY_SET: TokenSet = token_set![ @@ -135,9 +136,7 @@ pub(crate) fn ts_declare(p: &mut Parser) -> Option { Some(match p.nth(1) { T![function] => { p.state.decorators_were_valid = true; - let m = p.start(); - p.bump_remap(T![declare]); - function_decl(p, m, false) + function_declaration(p) } T![class] => { p.state.decorators_were_valid = true; @@ -487,7 +486,7 @@ fn ts_property_or_method_sig(p: &mut Parser, m: Marker, readonly: bool) -> Optio if p.at(T![<]) { no_recover!(p, ts_type_params(p)); } - formal_parameters(p); + parameter_list(p); if p.at(T![:]) { ts_type_or_type_predicate_ann(p, T![:]); } @@ -536,7 +535,7 @@ pub fn ts_signature_member(p: &mut Parser, construct_sig: bool) -> Option Option])); Some(m.complete( p, diff --git a/crates/rslint_parser/test_data/inline/err/binding_identifier_invalid.rast b/crates/rslint_parser/test_data/inline/err/binding_identifier_invalid.rast index 4b72cba208d..44fe3c7ea38 100644 --- a/crates/rslint_parser/test_data/inline/err/binding_identifier_invalid.rast +++ b/crates/rslint_parser/test_data/inline/err/binding_identifier_invalid.rast @@ -5,7 +5,7 @@ JS_ROOT@0..83 ARROW_EXPR@0..30 ASYNC_KW@0..5 "async" WHITESPACE@5..6 " " - PARAMETER_LIST@6..8 + JS_PARAMETER_LIST@6..8 L_PAREN@6..7 "(" LIST@7..7 R_PAREN@7..8 ")" @@ -36,13 +36,13 @@ JS_ROOT@0..83 WHITESPACE@28..29 " " R_CURLY@29..30 "}" WHITESPACE@30..31 "\n" - FN_DECL@31..68 + JS_FUNCTION_DECLARATION@31..68 FUNCTION_KW@31..39 "function" WHITESPACE@39..40 " " STAR@40..41 "*" - NAME@41..44 + JS_IDENTIFIER_BINDING@41..44 IDENT@41..44 "foo" - PARAMETER_LIST@44..46 + JS_PARAMETER_LIST@44..46 L_PAREN@44..45 "(" LIST@45..45 R_PAREN@45..46 ")" diff --git a/crates/rslint_parser/test_data/inline/err/directives_err.rast b/crates/rslint_parser/test_data/inline/err/directives_err.rast index 58d97e7040c..f50d1db6c21 100644 --- a/crates/rslint_parser/test_data/inline/err/directives_err.rast +++ b/crates/rslint_parser/test_data/inline/err/directives_err.rast @@ -3,12 +3,12 @@ JS_ROOT@0..161 WHITESPACE@9..11 "\n\n" LIST@11..11 LIST@11..160 - FN_DECL@11..160 + JS_FUNCTION_DECLARATION@11..160 FUNCTION_KW@11..19 "function" WHITESPACE@19..20 " " - NAME@20..24 + JS_IDENTIFIER_BINDING@20..24 IDENT@20..24 "test" - PARAMETER_LIST@24..26 + JS_PARAMETER_LIST@24..26 L_PAREN@24..25 "(" LIST@25..25 R_PAREN@25..26 ")" @@ -22,12 +22,12 @@ JS_ROOT@0..161 SEMICOLON@42..43 ";" WHITESPACE@43..45 "\n\t" LIST@45..158 - FN_DECL@45..84 + JS_FUNCTION_DECLARATION@45..84 FUNCTION_KW@45..53 "function" WHITESPACE@53..54 " " - NAME@54..61 + JS_IDENTIFIER_BINDING@54..61 IDENT@54..61 "inner_a" - PARAMETER_LIST@61..63 + JS_PARAMETER_LIST@61..63 L_PAREN@61..62 "(" LIST@62..62 R_PAREN@62..63 ")" @@ -43,12 +43,12 @@ JS_ROOT@0..161 LIST@83..83 R_CURLY@83..84 "}" WHITESPACE@84..87 "\n\n\t" - FN_DECL@87..158 + JS_FUNCTION_DECLARATION@87..158 FUNCTION_KW@87..95 "function" WHITESPACE@95..96 " " - NAME@96..103 + JS_IDENTIFIER_BINDING@96..103 IDENT@96..103 "inner_b" - PARAMETER_LIST@103..105 + JS_PARAMETER_LIST@103..105 L_PAREN@103..104 "(" LIST@104..104 R_PAREN@104..105 ")" @@ -58,12 +58,12 @@ JS_ROOT@0..161 WHITESPACE@107..110 "\n\t\t" LIST@110..110 LIST@110..155 - FN_DECL@110..155 + JS_FUNCTION_DECLARATION@110..155 FUNCTION_KW@110..118 "function" WHITESPACE@118..119 " " - NAME@119..130 + JS_IDENTIFIER_BINDING@119..130 IDENT@119..130 "inner_inner" - PARAMETER_LIST@130..132 + JS_PARAMETER_LIST@130..132 L_PAREN@130..131 "(" LIST@131..131 R_PAREN@131..132 ")" diff --git a/crates/rslint_parser/test_data/inline/err/formal_params_no_binding_element.rast b/crates/rslint_parser/test_data/inline/err/formal_params_no_binding_element.rast index 8c7995e5fe4..0f9d231eb3b 100644 --- a/crates/rslint_parser/test_data/inline/err/formal_params_no_binding_element.rast +++ b/crates/rslint_parser/test_data/inline/err/formal_params_no_binding_element.rast @@ -1,12 +1,12 @@ JS_ROOT@0..22 LIST@0..0 LIST@0..21 - FN_DECL@0..21 + JS_FUNCTION_DECLARATION@0..21 FUNCTION_KW@0..8 "function" WHITESPACE@8..9 " " - NAME@9..12 + JS_IDENTIFIER_BINDING@9..12 IDENT@9..12 "foo" - PARAMETER_LIST@12..18 + JS_PARAMETER_LIST@12..18 L_PAREN@12..13 "(" LIST@13..17 ERROR@13..17 diff --git a/crates/rslint_parser/test_data/inline/err/function_decl_err.rast b/crates/rslint_parser/test_data/inline/err/function_decl_err.rast index cb8ec90510b..d899cb0cfd9 100644 --- a/crates/rslint_parser/test_data/inline/err/function_decl_err.rast +++ b/crates/rslint_parser/test_data/inline/err/function_decl_err.rast @@ -1,9 +1,9 @@ JS_ROOT@0..114 LIST@0..0 LIST@0..113 - FN_DECL@0..13 + JS_FUNCTION_DECLARATION@0..13 FUNCTION_KW@0..8 "function" - PARAMETER_LIST@8..10 + JS_PARAMETER_LIST@8..10 L_PAREN@8..9 "(" LIST@9..9 R_PAREN@9..10 ")" @@ -14,10 +14,10 @@ JS_ROOT@0..114 LIST@12..12 R_CURLY@12..13 "}" WHITESPACE@13..14 "\n" - FN_DECL@14..41 + JS_FUNCTION_DECLARATION@14..41 FUNCTION_KW@14..22 "function" WHITESPACE@22..23 " " - PARAMETER_LIST@23..38 + JS_PARAMETER_LIST@23..38 LIST@23..37 ERROR@23..24 L_CURLY@23..24 "{" @@ -39,11 +39,11 @@ JS_ROOT@0..114 LIST@40..40 R_CURLY@40..41 "}" WHITESPACE@41..42 "\n" - FN_DECL@42..61 - IDENT@42..47 "async" + FN_EXPR@42..61 + ASYNC_KW@42..47 "async" WHITESPACE@47..48 " " FUNCTION_KW@48..56 "function" - PARAMETER_LIST@56..58 + JS_PARAMETER_LIST@56..58 L_PAREN@56..57 "(" LIST@57..57 R_PAREN@57..58 ")" @@ -54,13 +54,13 @@ JS_ROOT@0..114 LIST@60..60 R_CURLY@60..61 "}" WHITESPACE@61..62 "\n" - FN_DECL@62..83 - IDENT@62..67 "async" + FN_EXPR@62..83 + ASYNC_KW@62..67 "async" WHITESPACE@67..68 " " FUNCTION_KW@68..76 "function" WHITESPACE@76..77 " " STAR@77..78 "*" - PARAMETER_LIST@78..80 + JS_PARAMETER_LIST@78..80 L_PAREN@78..79 "(" LIST@79..79 R_PAREN@79..80 ")" @@ -71,13 +71,13 @@ JS_ROOT@0..114 LIST@82..82 R_CURLY@82..83 "}" WHITESPACE@83..84 "\n" - FN_DECL@84..102 + JS_FUNCTION_DECLARATION@84..102 FUNCTION_KW@84..92 "function" WHITESPACE@92..93 " " STAR@93..94 "*" - NAME@94..97 + JS_IDENTIFIER_BINDING@94..97 IDENT@94..97 "foo" - PARAMETER_LIST@97..99 + JS_PARAMETER_LIST@97..99 L_PAREN@97..98 "(" LIST@98..98 R_PAREN@98..99 ")" @@ -98,6 +98,20 @@ JS_ROOT@0..114 SEMICOLON@112..113 ";" WHITESPACE@113..114 "\n" -- +error[SyntaxError]: expected a name for the function in a function declaration, but found none + ┌─ function_decl_err.js:1:9 + │ +1 │ function() {} + │ ^ + +-- +error[SyntaxError]: expected a name for the function in a function declaration, but found none + ┌─ function_decl_err.js:2:10 + │ +2 │ function {} + │ ^ + +-- error[SyntaxError]: expected `'('` but instead found `{` ┌─ function_decl_err.js:2:10 │ diff --git a/crates/rslint_parser/test_data/inline/err/invalid_method_recover.rast b/crates/rslint_parser/test_data/inline/err/invalid_method_recover.rast index 4ae4ebd0e59..f5114b204dd 100644 --- a/crates/rslint_parser/test_data/inline/err/invalid_method_recover.rast +++ b/crates/rslint_parser/test_data/inline/err/invalid_method_recover.rast @@ -24,7 +24,7 @@ JS_ROOT@0..48 EQ@18..19 "=" WHITESPACE@19..20 " " ARROW_EXPR@20..43 - PARAMETER_LIST@20..22 + JS_PARAMETER_LIST@20..22 L_PAREN@20..21 "(" LIST@21..21 R_PAREN@21..22 ")" diff --git a/crates/rslint_parser/test_data/inline/err/paren_or_arrow_expr_invalid_params.rast b/crates/rslint_parser/test_data/inline/err/paren_or_arrow_expr_invalid_params.rast index 5aa59064664..bca91df5b58 100644 --- a/crates/rslint_parser/test_data/inline/err/paren_or_arrow_expr_invalid_params.rast +++ b/crates/rslint_parser/test_data/inline/err/paren_or_arrow_expr_invalid_params.rast @@ -3,7 +3,7 @@ JS_ROOT@0..28 LIST@0..27 JS_EXPRESSION_STATEMENT@0..6 ARROW_EXPR@0..6 - PARAMETER_LIST@0..4 + JS_PARAMETER_LIST@0..4 L_PAREN@0..1 "(" LIST@1..4 ERROR@1..2 diff --git a/crates/rslint_parser/test_data/inline/ok/async_arrow_expr.rast b/crates/rslint_parser/test_data/inline/ok/async_arrow_expr.rast index d6be2069ed3..09d480ebdca 100644 --- a/crates/rslint_parser/test_data/inline/ok/async_arrow_expr.rast +++ b/crates/rslint_parser/test_data/inline/ok/async_arrow_expr.rast @@ -44,7 +44,7 @@ JS_ROOT@0..82 ARROW_EXPR@32..49 ASYNC_KW@32..37 "async" WHITESPACE@37..38 " " - PARAMETER_LIST@38..43 + JS_PARAMETER_LIST@38..43 L_PAREN@38..39 "(" LIST@39..42 SINGLE_PATTERN@39..42 @@ -64,7 +64,7 @@ JS_ROOT@0..82 ARROW_EXPR@50..81 ASYNC_KW@50..55 "async" WHITESPACE@55..56 " " - PARAMETER_LIST@56..74 + JS_PARAMETER_LIST@56..74 L_PAREN@56..57 "(" LIST@57..73 SINGLE_PATTERN@57..60 @@ -77,7 +77,7 @@ JS_ROOT@0..82 IDENT@62..65 "bar" COMMA@65..66 "," WHITESPACE@66..67 " " - REST_PATTERN@67..73 + JS_REST_PARAMETER@67..73 DOT2@67..70 "..." SINGLE_PATTERN@70..73 NAME@70..73 diff --git a/crates/rslint_parser/test_data/inline/ok/async_function_expr.rast b/crates/rslint_parser/test_data/inline/ok/async_function_expr.rast index b7c5bbcca23..7d3c6104a5f 100644 --- a/crates/rslint_parser/test_data/inline/ok/async_function_expr.rast +++ b/crates/rslint_parser/test_data/inline/ok/async_function_expr.rast @@ -18,7 +18,7 @@ JS_ROOT@0..62 ASYNC_KW@8..13 "async" WHITESPACE@13..14 " " FUNCTION_KW@14..22 "function" - PARAMETER_LIST@22..24 + JS_PARAMETER_LIST@22..24 L_PAREN@22..23 "(" LIST@23..23 R_PAREN@23..24 ")" @@ -48,9 +48,9 @@ JS_ROOT@0..62 WHITESPACE@42..43 " " FUNCTION_KW@43..51 "function" WHITESPACE@51..52 " " - NAME@52..55 + JS_IDENTIFIER_BINDING@52..55 IDENT@52..55 "foo" - PARAMETER_LIST@55..57 + JS_PARAMETER_LIST@55..57 L_PAREN@55..56 "(" LIST@56..56 R_PAREN@56..57 ")" diff --git a/crates/rslint_parser/test_data/inline/ok/async_method.rast b/crates/rslint_parser/test_data/inline/ok/async_method.rast index 21424bded3e..2fc3b0f272d 100644 --- a/crates/rslint_parser/test_data/inline/ok/async_method.rast +++ b/crates/rslint_parser/test_data/inline/ok/async_method.rast @@ -16,7 +16,7 @@ JS_ROOT@0..47 WHITESPACE@18..19 " " NAME@19..22 IDENT@19..22 "foo" - PARAMETER_LIST@22..24 + JS_PARAMETER_LIST@22..24 L_PAREN@22..23 "(" LIST@23..23 R_PAREN@23..24 ")" @@ -33,7 +33,7 @@ JS_ROOT@0..47 STAR@35..36 "*" NAME@36..39 IDENT@36..39 "foo" - PARAMETER_LIST@39..41 + JS_PARAMETER_LIST@39..41 L_PAREN@39..40 "(" LIST@40..40 R_PAREN@40..41 ")" diff --git a/crates/rslint_parser/test_data/inline/ok/await_expression.rast b/crates/rslint_parser/test_data/inline/ok/await_expression.rast index 8a6ae0ad47a..5a0b2d679c7 100644 --- a/crates/rslint_parser/test_data/inline/ok/await_expression.rast +++ b/crates/rslint_parser/test_data/inline/ok/await_expression.rast @@ -1,14 +1,14 @@ JS_ROOT@0..116 LIST@0..0 LIST@0..115 - FN_DECL@0..76 - IDENT@0..5 "async" + FN_EXPR@0..76 + ASYNC_KW@0..5 "async" WHITESPACE@5..6 " " FUNCTION_KW@6..14 "function" WHITESPACE@14..15 " " - NAME@15..19 + JS_IDENTIFIER_BINDING@15..19 IDENT@15..19 "test" - PARAMETER_LIST@19..21 + JS_PARAMETER_LIST@19..21 L_PAREN@19..20 "(" LIST@20..20 R_PAREN@20..21 ")" @@ -63,14 +63,14 @@ JS_ROOT@0..116 WHITESPACE@74..75 "\n" R_CURLY@75..76 "}" WHITESPACE@76..78 "\n\n" - FN_DECL@78..115 - IDENT@78..83 "async" + FN_EXPR@78..115 + ASYNC_KW@78..83 "async" WHITESPACE@83..84 " " FUNCTION_KW@84..92 "function" WHITESPACE@92..93 " " - NAME@93..98 + JS_IDENTIFIER_BINDING@93..98 IDENT@93..98 "inner" - PARAMETER_LIST@98..100 + JS_PARAMETER_LIST@98..100 L_PAREN@98..99 "(" LIST@99..99 R_PAREN@99..100 ")" diff --git a/crates/rslint_parser/test_data/inline/ok/class_empty_element.rast b/crates/rslint_parser/test_data/inline/ok/class_empty_element.rast index 447a16a207b..644cd9d7f01 100644 --- a/crates/rslint_parser/test_data/inline/ok/class_empty_element.rast +++ b/crates/rslint_parser/test_data/inline/ok/class_empty_element.rast @@ -37,7 +37,7 @@ JS_ROOT@0..41 WHITESPACE@26..27 " " NAME@27..30 IDENT@27..30 "foo" - PARAMETER_LIST@30..32 + JS_PARAMETER_LIST@30..32 L_PAREN@30..31 "(" LIST@31..31 R_PAREN@31..32 ")" diff --git a/crates/rslint_parser/test_data/inline/ok/class_expr.rast b/crates/rslint_parser/test_data/inline/ok/class_expr.rast index 539931e188c..97e511f459a 100644 --- a/crates/rslint_parser/test_data/inline/ok/class_expr.rast +++ b/crates/rslint_parser/test_data/inline/ok/class_expr.rast @@ -49,7 +49,7 @@ JS_ROOT@0..72 CONSTRUCTOR@39..55 NAME@39..50 IDENT@39..50 "constructor" - PARAMETER_LIST@50..52 + JS_PARAMETER_LIST@50..52 L_PAREN@50..51 "(" LIST@51..51 R_PAREN@51..52 ")" diff --git a/crates/rslint_parser/test_data/inline/ok/directives.rast b/crates/rslint_parser/test_data/inline/ok/directives.rast index 0b4ce7abdb0..c52cfc14486 100644 --- a/crates/rslint_parser/test_data/inline/ok/directives.rast +++ b/crates/rslint_parser/test_data/inline/ok/directives.rast @@ -1,11 +1,11 @@ -JS_ROOT@0..360 +JS_ROOT@0..321 COMMENT@0..9 "// SCRIPT" WHITESPACE@9..11 "\n\n" LIST@11..20 JS_DIRECTIVE@11..20 JS_STRING_LITERAL_TOKEN@11..20 "\"use new\"" WHITESPACE@20..22 "\n\n" - LIST@22..359 + LIST@22..320 JS_VARIABLE_DECLARATION_STATEMENT@22..33 JS_VARIABLE_DECLARATION@22..32 LET_KW@22..25 "let" @@ -28,24 +28,24 @@ JS_ROOT@0..360 JS_STRING_LITERAL_TOKEN@35..47 "\"use strict\"" SEMICOLON@47..48 ";" WHITESPACE@48..49 " " - FN_DECL@49..152 + JS_FUNCTION_DECLARATION@49..133 COMMENT@49..67 "// not a directive" WHITESPACE@67..69 "\n\n" FUNCTION_KW@69..77 "function" WHITESPACE@77..78 " " - NAME@78..82 + JS_IDENTIFIER_BINDING@78..82 IDENT@78..82 "test" - PARAMETER_LIST@82..84 + JS_PARAMETER_LIST@82..84 L_PAREN@82..83 "(" LIST@83..83 R_PAREN@83..84 ")" WHITESPACE@84..85 " " - JS_FUNCTION_BODY@85..152 + JS_FUNCTION_BODY@85..133 L_CURLY@85..86 "{" WHITESPACE@86..88 "\n\t" LIST@88..101 JS_DIRECTIVE@88..101 - JS_STRING_LITERAL_TOKEN@88..100 "'use strict'" + JS_STRING_LITERAL_TOKEN@88..100 "\"use strict\"" SEMICOLON@100..101 ";" WHITESPACE@101..104 "\n\n\t" LIST@104..131 @@ -68,127 +68,123 @@ JS_ROOT@0..360 WHITESPACE@115..118 "\n\n\t" JS_EXPRESSION_STATEMENT@118..131 JS_STRING_LITERAL@118..130 - JS_STRING_LITERAL_TOKEN@118..130 "'use strict'" + JS_STRING_LITERAL_TOKEN@118..130 "\"use strict\"" SEMICOLON@130..131 ";" - WHITESPACE@131..132 " " - COMMENT@132..150 "// not a directive" - WHITESPACE@150..151 "\n" - R_CURLY@151..152 "}" - WHITESPACE@152..154 "\n\n" - JS_EXPRESSION_STATEMENT@154..236 - JS_PARENTHESIZED_EXPRESSION@154..235 - L_PAREN@154..155 "(" - FN_EXPR@155..234 - FUNCTION_KW@155..163 "function" - WHITESPACE@163..164 " " - PARAMETER_LIST@164..166 - L_PAREN@164..165 "(" - LIST@165..165 - R_PAREN@165..166 ")" - WHITESPACE@166..167 " " - JS_FUNCTION_BODY@167..234 - L_CURLY@167..168 "{" - WHITESPACE@168..170 "\n\t" - LIST@170..183 - JS_DIRECTIVE@170..183 - JS_STRING_LITERAL_TOKEN@170..182 "\"use strict\"" - SEMICOLON@182..183 ";" - WHITESPACE@183..186 "\n\n\t" - LIST@186..213 - JS_VARIABLE_DECLARATION_STATEMENT@186..197 - JS_VARIABLE_DECLARATION@186..196 - LET_KW@186..189 "let" - WHITESPACE@189..190 " " - LIST@190..196 - JS_VARIABLE_DECLARATOR@190..196 - SINGLE_PATTERN@190..191 - NAME@190..191 - IDENT@190..191 "a" - WHITESPACE@191..192 " " - JS_EQUAL_VALUE_CLAUSE@192..196 - EQ@192..193 "=" - WHITESPACE@193..194 " " - JS_NUMBER_LITERAL@194..196 - JS_NUMBER_LITERAL_TOKEN@194..196 "10" - SEMICOLON@196..197 ";" - WHITESPACE@197..200 "\n\n\t" - JS_EXPRESSION_STATEMENT@200..213 - JS_STRING_LITERAL@200..212 - JS_STRING_LITERAL_TOKEN@200..212 "\"use strict\"" - SEMICOLON@212..213 ";" - WHITESPACE@213..214 " " - COMMENT@214..232 "// not a directive" - WHITESPACE@232..233 "\n" - R_CURLY@233..234 "}" - R_PAREN@234..235 ")" - SEMICOLON@235..236 ";" - WHITESPACE@236..238 "\n\n" - JS_VARIABLE_DECLARATION_STATEMENT@238..320 - JS_VARIABLE_DECLARATION@238..320 - LET_KW@238..241 "let" - WHITESPACE@241..242 " " - LIST@242..320 - JS_VARIABLE_DECLARATOR@242..320 - SINGLE_PATTERN@242..243 - NAME@242..243 - IDENT@242..243 "b" - WHITESPACE@243..244 " " - JS_EQUAL_VALUE_CLAUSE@244..320 - EQ@244..245 "=" - WHITESPACE@245..246 " " - ARROW_EXPR@246..320 - PARAMETER_LIST@246..248 - L_PAREN@246..247 "(" - LIST@247..247 - R_PAREN@247..248 ")" - WHITESPACE@248..249 " " - FAT_ARROW@249..251 "=>" - WHITESPACE@251..252 " " - JS_FUNCTION_BODY@252..320 - L_CURLY@252..253 "{" - WHITESPACE@253..255 "\n\t" - LIST@255..268 - JS_DIRECTIVE@255..268 - JS_STRING_LITERAL_TOKEN@255..267 "\"use strict\"" - SEMICOLON@267..268 ";" - WHITESPACE@268..271 "\n\n\t" - LIST@271..298 - JS_VARIABLE_DECLARATION_STATEMENT@271..282 - JS_VARIABLE_DECLARATION@271..281 - LET_KW@271..274 "let" - WHITESPACE@274..275 " " - LIST@275..281 - JS_VARIABLE_DECLARATOR@275..281 - SINGLE_PATTERN@275..276 - NAME@275..276 - IDENT@275..276 "a" - WHITESPACE@276..277 " " - JS_EQUAL_VALUE_CLAUSE@277..281 - EQ@277..278 "=" - WHITESPACE@278..279 " " - JS_NUMBER_LITERAL@279..281 - JS_NUMBER_LITERAL_TOKEN@279..281 "10" - SEMICOLON@281..282 ";" - WHITESPACE@282..285 "\n\n\t" - JS_EXPRESSION_STATEMENT@285..298 - JS_STRING_LITERAL@285..297 - JS_STRING_LITERAL_TOKEN@285..297 "\"use strict\"" - SEMICOLON@297..298 ";" - WHITESPACE@298..300 " " - COMMENT@300..318 "// not a directive" - WHITESPACE@318..319 "\n" - R_CURLY@319..320 "}" - WHITESPACE@320..322 "\n\n" - JS_BLOCK_STATEMENT@322..359 - L_CURLY@322..323 "{" - WHITESPACE@323..325 "\n\t" - LIST@325..338 - JS_EXPRESSION_STATEMENT@325..338 - JS_STRING_LITERAL@325..337 - JS_STRING_LITERAL_TOKEN@325..337 "\"use strict\"" - SEMICOLON@337..338 ";" - WHITESPACE@338..339 " " - COMMENT@339..357 "// not a directive" - WHITESPACE@357..358 "\n" - R_CURLY@358..359 "}" - WHITESPACE@359..360 "\n" + WHITESPACE@131..132 "\n" + R_CURLY@132..133 "}" + WHITESPACE@133..135 "\n\n" + JS_EXPRESSION_STATEMENT@135..217 + JS_PARENTHESIZED_EXPRESSION@135..216 + L_PAREN@135..136 "(" + FN_EXPR@136..215 + FUNCTION_KW@136..144 "function" + WHITESPACE@144..145 " " + JS_PARAMETER_LIST@145..147 + L_PAREN@145..146 "(" + LIST@146..146 + R_PAREN@146..147 ")" + WHITESPACE@147..148 " " + JS_FUNCTION_BODY@148..215 + L_CURLY@148..149 "{" + WHITESPACE@149..151 "\n\t" + LIST@151..164 + JS_DIRECTIVE@151..164 + JS_STRING_LITERAL_TOKEN@151..163 "\"use strict\"" + SEMICOLON@163..164 ";" + WHITESPACE@164..167 "\n\n\t" + LIST@167..194 + JS_VARIABLE_DECLARATION_STATEMENT@167..178 + JS_VARIABLE_DECLARATION@167..177 + LET_KW@167..170 "let" + WHITESPACE@170..171 " " + LIST@171..177 + JS_VARIABLE_DECLARATOR@171..177 + SINGLE_PATTERN@171..172 + NAME@171..172 + IDENT@171..172 "a" + WHITESPACE@172..173 " " + JS_EQUAL_VALUE_CLAUSE@173..177 + EQ@173..174 "=" + WHITESPACE@174..175 " " + JS_NUMBER_LITERAL@175..177 + JS_NUMBER_LITERAL_TOKEN@175..177 "10" + SEMICOLON@177..178 ";" + WHITESPACE@178..181 "\n\n\t" + JS_EXPRESSION_STATEMENT@181..194 + JS_STRING_LITERAL@181..193 + JS_STRING_LITERAL_TOKEN@181..193 "\"use strict\"" + SEMICOLON@193..194 ";" + WHITESPACE@194..195 " " + COMMENT@195..213 "// not a directive" + WHITESPACE@213..214 "\n" + R_CURLY@214..215 "}" + R_PAREN@215..216 ")" + SEMICOLON@216..217 ";" + WHITESPACE@217..219 "\n\n" + JS_VARIABLE_DECLARATION_STATEMENT@219..281 + JS_VARIABLE_DECLARATION@219..281 + LET_KW@219..222 "let" + WHITESPACE@222..223 " " + LIST@223..281 + JS_VARIABLE_DECLARATOR@223..281 + SINGLE_PATTERN@223..224 + NAME@223..224 + IDENT@223..224 "b" + WHITESPACE@224..225 " " + JS_EQUAL_VALUE_CLAUSE@225..281 + EQ@225..226 "=" + WHITESPACE@226..227 " " + ARROW_EXPR@227..281 + JS_PARAMETER_LIST@227..229 + L_PAREN@227..228 "(" + LIST@228..228 + R_PAREN@228..229 ")" + WHITESPACE@229..230 " " + FAT_ARROW@230..232 "=>" + WHITESPACE@232..233 " " + JS_FUNCTION_BODY@233..281 + L_CURLY@233..234 "{" + WHITESPACE@234..236 "\n\t" + LIST@236..249 + JS_DIRECTIVE@236..249 + JS_STRING_LITERAL_TOKEN@236..248 "\"use strict\"" + SEMICOLON@248..249 ";" + WHITESPACE@249..252 "\n\n\t" + LIST@252..279 + JS_VARIABLE_DECLARATION_STATEMENT@252..263 + JS_VARIABLE_DECLARATION@252..262 + LET_KW@252..255 "let" + WHITESPACE@255..256 " " + LIST@256..262 + JS_VARIABLE_DECLARATOR@256..262 + SINGLE_PATTERN@256..257 + NAME@256..257 + IDENT@256..257 "a" + WHITESPACE@257..258 " " + JS_EQUAL_VALUE_CLAUSE@258..262 + EQ@258..259 "=" + WHITESPACE@259..260 " " + JS_NUMBER_LITERAL@260..262 + JS_NUMBER_LITERAL_TOKEN@260..262 "10" + SEMICOLON@262..263 ";" + WHITESPACE@263..266 "\n\n\t" + JS_EXPRESSION_STATEMENT@266..279 + JS_STRING_LITERAL@266..278 + JS_STRING_LITERAL_TOKEN@266..278 "\"use strict\"" + SEMICOLON@278..279 ";" + WHITESPACE@279..280 "\n" + R_CURLY@280..281 "}" + WHITESPACE@281..283 "\n\n" + JS_BLOCK_STATEMENT@283..320 + L_CURLY@283..284 "{" + WHITESPACE@284..286 "\n\t" + LIST@286..299 + JS_EXPRESSION_STATEMENT@286..299 + JS_STRING_LITERAL@286..298 + JS_STRING_LITERAL_TOKEN@286..298 "\"use strict\"" + SEMICOLON@298..299 ";" + WHITESPACE@299..300 " " + COMMENT@300..318 "// not a directive" + WHITESPACE@318..319 "\n" + R_CURLY@319..320 "}" + WHITESPACE@320..321 "\n" diff --git a/crates/rslint_parser/test_data/inline/ok/function_decl.rast b/crates/rslint_parser/test_data/inline/ok/function_decl.rast index 319fe52c75a..e01f439bb6a 100644 --- a/crates/rslint_parser/test_data/inline/ok/function_decl.rast +++ b/crates/rslint_parser/test_data/inline/ok/function_decl.rast @@ -1,12 +1,12 @@ JS_ROOT@0..142 LIST@0..0 LIST@0..141 - FN_DECL@0..17 + JS_FUNCTION_DECLARATION@0..17 FUNCTION_KW@0..8 "function" WHITESPACE@8..9 " " - NAME@9..12 + JS_IDENTIFIER_BINDING@9..12 IDENT@9..12 "foo" - PARAMETER_LIST@12..14 + JS_PARAMETER_LIST@12..14 L_PAREN@12..13 "(" LIST@13..13 R_PAREN@13..14 ")" @@ -17,13 +17,13 @@ JS_ROOT@0..142 LIST@16..16 R_CURLY@16..17 "}" WHITESPACE@17..18 "\n" - FN_DECL@18..36 + JS_FUNCTION_DECLARATION@18..36 FUNCTION_KW@18..26 "function" WHITESPACE@26..27 " " STAR@27..28 "*" - NAME@28..31 + JS_IDENTIFIER_BINDING@28..31 IDENT@28..31 "foo" - PARAMETER_LIST@31..33 + JS_PARAMETER_LIST@31..33 L_PAREN@31..32 "(" LIST@32..32 R_PAREN@32..33 ")" @@ -34,12 +34,12 @@ JS_ROOT@0..142 LIST@35..35 R_CURLY@35..36 "}" WHITESPACE@36..37 "\n" - FN_DECL@37..59 + JS_FUNCTION_DECLARATION@37..59 FUNCTION_KW@37..45 "function" WHITESPACE@45..46 " " - NAME@46..49 + JS_IDENTIFIER_BINDING@46..49 IDENT@46..49 "foo" - PARAMETER_LIST@49..56 + JS_PARAMETER_LIST@49..56 L_PAREN@49..50 "(" LIST@50..55 SINGLE_PATTERN@50..55 @@ -53,15 +53,15 @@ JS_ROOT@0..142 LIST@58..58 R_CURLY@58..59 "}" WHITESPACE@59..60 "\n" - FN_DECL@60..84 - IDENT@60..65 "async" + FN_EXPR@60..84 + ASYNC_KW@60..65 "async" WHITESPACE@65..66 " " FUNCTION_KW@66..74 "function" WHITESPACE@74..75 " " STAR@75..76 "*" - NAME@76..79 + JS_IDENTIFIER_BINDING@76..79 IDENT@76..79 "foo" - PARAMETER_LIST@79..81 + JS_PARAMETER_LIST@79..81 L_PAREN@79..80 "(" LIST@80..80 R_PAREN@80..81 ")" @@ -72,14 +72,14 @@ JS_ROOT@0..142 LIST@83..83 R_CURLY@83..84 "}" WHITESPACE@84..85 "\n" - FN_DECL@85..108 - IDENT@85..90 "async" + FN_EXPR@85..108 + ASYNC_KW@85..90 "async" WHITESPACE@90..91 " " FUNCTION_KW@91..99 "function" WHITESPACE@99..100 " " - NAME@100..103 + JS_IDENTIFIER_BINDING@100..103 IDENT@100..103 "foo" - PARAMETER_LIST@103..105 + JS_PARAMETER_LIST@103..105 L_PAREN@103..104 "(" LIST@104..104 R_PAREN@104..105 ")" @@ -90,13 +90,13 @@ JS_ROOT@0..142 LIST@107..107 R_CURLY@107..108 "}" WHITESPACE@108..109 "\n" - FN_DECL@109..141 + JS_FUNCTION_DECLARATION@109..141 FUNCTION_KW@109..117 "function" WHITESPACE@117..118 " " STAR@118..119 "*" - NAME@119..122 + JS_IDENTIFIER_BINDING@119..122 IDENT@119..122 "foo" - PARAMETER_LIST@122..124 + JS_PARAMETER_LIST@122..124 L_PAREN@122..123 "(" LIST@123..123 R_PAREN@123..124 ")" diff --git a/crates/rslint_parser/test_data/inline/ok/function_expr.rast b/crates/rslint_parser/test_data/inline/ok/function_expr.rast index b65b48b46da..6e375a567f7 100644 --- a/crates/rslint_parser/test_data/inline/ok/function_expr.rast +++ b/crates/rslint_parser/test_data/inline/ok/function_expr.rast @@ -16,7 +16,7 @@ JS_ROOT@0..48 WHITESPACE@7..8 " " FN_EXPR@8..21 FUNCTION_KW@8..16 "function" - PARAMETER_LIST@16..18 + JS_PARAMETER_LIST@16..18 L_PAREN@16..17 "(" LIST@17..17 R_PAREN@17..18 ")" @@ -43,9 +43,9 @@ JS_ROOT@0..48 FN_EXPR@30..47 FUNCTION_KW@30..38 "function" WHITESPACE@38..39 " " - NAME@39..42 + JS_IDENTIFIER_BINDING@39..42 IDENT@39..42 "foo" - PARAMETER_LIST@42..44 + JS_PARAMETER_LIST@42..44 L_PAREN@42..43 "(" LIST@43..43 R_PAREN@43..44 ")" diff --git a/crates/rslint_parser/test_data/inline/ok/method_getter.rast b/crates/rslint_parser/test_data/inline/ok/method_getter.rast index 0dead45a79d..fc1b08a1402 100644 --- a/crates/rslint_parser/test_data/inline/ok/method_getter.rast +++ b/crates/rslint_parser/test_data/inline/ok/method_getter.rast @@ -16,7 +16,7 @@ JS_ROOT@0..28 WHITESPACE@16..17 " " NAME@17..20 IDENT@17..20 "bar" - PARAMETER_LIST@20..22 + JS_PARAMETER_LIST@20..22 L_PAREN@20..21 "(" LIST@21..21 R_PAREN@21..22 ")" diff --git a/crates/rslint_parser/test_data/inline/ok/method_setter.rast b/crates/rslint_parser/test_data/inline/ok/method_setter.rast index 02cb392ac18..31d68dfda07 100644 --- a/crates/rslint_parser/test_data/inline/ok/method_setter.rast +++ b/crates/rslint_parser/test_data/inline/ok/method_setter.rast @@ -16,7 +16,7 @@ JS_ROOT@0..28 WHITESPACE@16..17 " " NAME@17..20 IDENT@17..20 "bar" - PARAMETER_LIST@20..22 + JS_PARAMETER_LIST@20..22 L_PAREN@20..21 "(" LIST@21..21 R_PAREN@21..22 ")" diff --git a/crates/rslint_parser/test_data/inline/ok/new_exprs.rast b/crates/rslint_parser/test_data/inline/ok/new_exprs.rast index ef375abb05f..7141a4d06ca 100644 --- a/crates/rslint_parser/test_data/inline/ok/new_exprs.rast +++ b/crates/rslint_parser/test_data/inline/ok/new_exprs.rast @@ -87,7 +87,7 @@ JS_ROOT@0..113 WHITESPACE@88..89 " " JS_BINARY_EXPRESSION@89..111 ARROW_EXPR@89..100 - PARAMETER_LIST@89..94 + JS_PARAMETER_LIST@89..94 L_PAREN@89..90 "(" LIST@90..93 SINGLE_PATTERN@90..93 diff --git a/crates/rslint_parser/test_data/inline/ok/object_expr_async_method.rast b/crates/rslint_parser/test_data/inline/ok/object_expr_async_method.rast index 4b6c894d02f..66e030e5143 100644 --- a/crates/rslint_parser/test_data/inline/ok/object_expr_async_method.rast +++ b/crates/rslint_parser/test_data/inline/ok/object_expr_async_method.rast @@ -23,7 +23,7 @@ JS_ROOT@0..48 WHITESPACE@17..18 " " NAME@18..21 IDENT@18..21 "foo" - PARAMETER_LIST@21..23 + JS_PARAMETER_LIST@21..23 L_PAREN@21..22 "(" LIST@22..22 R_PAREN@22..23 ")" @@ -41,7 +41,7 @@ JS_ROOT@0..48 STAR@36..37 "*" NAME@37..40 IDENT@37..40 "foo" - PARAMETER_LIST@40..42 + JS_PARAMETER_LIST@40..42 L_PAREN@40..41 "(" LIST@41..41 R_PAREN@41..42 ")" diff --git a/crates/rslint_parser/test_data/inline/ok/object_expr_generator_method.rast b/crates/rslint_parser/test_data/inline/ok/object_expr_generator_method.rast index 212e039cffb..6feac392456 100644 --- a/crates/rslint_parser/test_data/inline/ok/object_expr_generator_method.rast +++ b/crates/rslint_parser/test_data/inline/ok/object_expr_generator_method.rast @@ -22,7 +22,7 @@ JS_ROOT@0..22 STAR@10..11 "*" NAME@11..14 IDENT@11..14 "foo" - PARAMETER_LIST@14..16 + JS_PARAMETER_LIST@14..16 L_PAREN@14..15 "(" LIST@15..15 R_PAREN@15..16 ")" diff --git a/crates/rslint_parser/test_data/inline/ok/object_expr_getter_setter.rast b/crates/rslint_parser/test_data/inline/ok/object_expr_getter_setter.rast index 92e40a57a93..13e30945267 100644 --- a/crates/rslint_parser/test_data/inline/ok/object_expr_getter_setter.rast +++ b/crates/rslint_parser/test_data/inline/ok/object_expr_getter_setter.rast @@ -23,7 +23,7 @@ JS_ROOT@0..43 WHITESPACE@14..15 " " NAME@15..18 IDENT@15..18 "foo" - PARAMETER_LIST@18..20 + JS_PARAMETER_LIST@18..20 L_PAREN@18..19 "(" LIST@19..19 R_PAREN@19..20 ")" diff --git a/crates/rslint_parser/test_data/inline/ok/object_expr_getter_setter_computed.rast b/crates/rslint_parser/test_data/inline/ok/object_expr_getter_setter_computed.rast index bda9ada03c4..f042770f72a 100644 --- a/crates/rslint_parser/test_data/inline/ok/object_expr_getter_setter_computed.rast +++ b/crates/rslint_parser/test_data/inline/ok/object_expr_getter_setter_computed.rast @@ -26,7 +26,7 @@ JS_ROOT@0..48 JS_REFERENCE_IDENTIFIER_EXPRESSION@17..20 IDENT@17..20 "foo" R_BRACK@20..21 "]" - PARAMETER_LIST@21..23 + JS_PARAMETER_LIST@21..23 L_PAREN@21..22 "(" LIST@22..22 R_PAREN@22..23 ")" diff --git a/crates/rslint_parser/test_data/inline/ok/object_expr_method.rast b/crates/rslint_parser/test_data/inline/ok/object_expr_method.rast index bca04f766ab..cd2cb44a431 100644 --- a/crates/rslint_parser/test_data/inline/ok/object_expr_method.rast +++ b/crates/rslint_parser/test_data/inline/ok/object_expr_method.rast @@ -21,7 +21,7 @@ JS_ROOT@0..23 METHOD@11..19 NAME@11..14 IDENT@11..14 "foo" - PARAMETER_LIST@14..16 + JS_PARAMETER_LIST@14..16 L_PAREN@14..15 "(" LIST@15..15 R_PAREN@15..16 ")" diff --git a/crates/rslint_parser/test_data/inline/ok/paren_or_arrow_expr.rast b/crates/rslint_parser/test_data/inline/ok/paren_or_arrow_expr.rast index 8bdcd317011..04a66eed605 100644 --- a/crates/rslint_parser/test_data/inline/ok/paren_or_arrow_expr.rast +++ b/crates/rslint_parser/test_data/inline/ok/paren_or_arrow_expr.rast @@ -11,7 +11,7 @@ JS_ROOT@0..85 WHITESPACE@6..7 "\n" JS_EXPRESSION_STATEMENT@7..19 ARROW_EXPR@7..18 - PARAMETER_LIST@7..12 + JS_PARAMETER_LIST@7..12 L_PAREN@7..8 "(" LIST@8..11 SINGLE_PATTERN@8..11 @@ -44,7 +44,7 @@ JS_ROOT@0..85 WHITESPACE@28..29 "\n" JS_EXPRESSION_STATEMENT@29..64 ARROW_EXPR@29..63 - PARAMETER_LIST@29..57 + JS_PARAMETER_LIST@29..57 L_PAREN@29..30 "(" LIST@30..56 OBJECT_PATTERN@30..56 @@ -93,7 +93,7 @@ JS_ROOT@0..85 WHITESPACE@64..65 "\n" JS_EXPRESSION_STATEMENT@65..84 ARROW_EXPR@65..84 - PARAMETER_LIST@65..78 + JS_PARAMETER_LIST@65..78 L_PAREN@65..66 "(" LIST@66..77 SINGLE_PATTERN@66..69 @@ -101,7 +101,7 @@ JS_ROOT@0..85 IDENT@66..69 "foo" COMMA@69..70 "," WHITESPACE@70..71 " " - REST_PATTERN@71..77 + JS_REST_PARAMETER@71..77 DOT2@71..74 "..." SINGLE_PATTERN@74..77 NAME@74..77 diff --git a/crates/rslint_parser/test_data/inline/ok/return_stmt.rast b/crates/rslint_parser/test_data/inline/ok/return_stmt.rast index 884b3fdeb04..fcf0edc05ff 100644 --- a/crates/rslint_parser/test_data/inline/ok/return_stmt.rast +++ b/crates/rslint_parser/test_data/inline/ok/return_stmt.rast @@ -3,7 +3,7 @@ JS_ROOT@0..43 LIST@0..42 JS_EXPRESSION_STATEMENT@0..42 ARROW_EXPR@0..42 - PARAMETER_LIST@0..2 + JS_PARAMETER_LIST@0..2 L_PAREN@0..1 "(" LIST@1..1 R_PAREN@1..2 ")" diff --git a/crates/rslint_parser/test_data/inline/ok/semicolons.rast b/crates/rslint_parser/test_data/inline/ok/semicolons.rast index a0ad291652b..d7c063b0827 100644 --- a/crates/rslint_parser/test_data/inline/ok/semicolons.rast +++ b/crates/rslint_parser/test_data/inline/ok/semicolons.rast @@ -66,12 +66,12 @@ JS_ROOT@0..84 NAME@49..52 IDENT@49..52 "foo" WHITESPACE@52..53 "\n" - FN_DECL@53..83 + JS_FUNCTION_DECLARATION@53..83 FUNCTION_KW@53..61 "function" WHITESPACE@61..62 " " - NAME@62..65 + JS_IDENTIFIER_BINDING@62..65 IDENT@62..65 "foo" - PARAMETER_LIST@65..67 + JS_PARAMETER_LIST@65..67 L_PAREN@65..66 "(" LIST@66..66 R_PAREN@66..67 ")" diff --git a/crates/rslint_parser/test_data/inline/ok/static_method.rast b/crates/rslint_parser/test_data/inline/ok/static_method.rast index 5b5ff20c418..ca48b1a6d67 100644 --- a/crates/rslint_parser/test_data/inline/ok/static_method.rast +++ b/crates/rslint_parser/test_data/inline/ok/static_method.rast @@ -16,7 +16,7 @@ JS_ROOT@0..99 WHITESPACE@19..20 " " NAME@20..23 IDENT@20..23 "foo" - PARAMETER_LIST@23..28 + JS_PARAMETER_LIST@23..28 L_PAREN@23..24 "(" LIST@24..27 SINGLE_PATTERN@24..27 @@ -36,7 +36,7 @@ JS_ROOT@0..99 STAR@40..41 "*" NAME@41..44 IDENT@41..44 "foo" - PARAMETER_LIST@44..46 + JS_PARAMETER_LIST@44..46 L_PAREN@44..45 "(" LIST@45..45 R_PAREN@45..46 ")" @@ -54,7 +54,7 @@ JS_ROOT@0..99 WHITESPACE@63..64 " " NAME@64..67 IDENT@64..67 "foo" - PARAMETER_LIST@67..69 + JS_PARAMETER_LIST@67..69 L_PAREN@67..68 "(" LIST@68..68 R_PAREN@68..69 ")" @@ -73,7 +73,7 @@ JS_ROOT@0..99 STAR@87..88 "*" NAME@88..91 IDENT@88..91 "foo" - PARAMETER_LIST@91..93 + JS_PARAMETER_LIST@91..93 L_PAREN@91..92 "(" LIST@92..92 R_PAREN@92..93 ")" diff --git a/crates/rslint_parser/test_data/inline/ok/with_statement.rast b/crates/rslint_parser/test_data/inline/ok/with_statement.rast index a906e3ab4ea..3fec63fa747 100644 --- a/crates/rslint_parser/test_data/inline/ok/with_statement.rast +++ b/crates/rslint_parser/test_data/inline/ok/with_statement.rast @@ -3,12 +3,12 @@ JS_ROOT@0..65 WHITESPACE@9..11 "\n\n" LIST@11..11 LIST@11..64 - FN_DECL@11..64 + JS_FUNCTION_DECLARATION@11..64 FUNCTION_KW@11..19 "function" WHITESPACE@19..20 " " - NAME@20..21 + JS_IDENTIFIER_BINDING@20..21 IDENT@20..21 "f" - PARAMETER_LIST@21..27 + JS_PARAMETER_LIST@21..27 L_PAREN@21..22 "(" LIST@22..26 SINGLE_PATTERN@22..23 diff --git a/crates/rslint_parser/test_data/inline/ok/yield_expr.rast b/crates/rslint_parser/test_data/inline/ok/yield_expr.rast index c6f8bb019b3..761d8627fe2 100644 --- a/crates/rslint_parser/test_data/inline/ok/yield_expr.rast +++ b/crates/rslint_parser/test_data/inline/ok/yield_expr.rast @@ -1,13 +1,13 @@ JS_ROOT@0..53 LIST@0..0 LIST@0..52 - FN_DECL@0..52 + JS_FUNCTION_DECLARATION@0..52 FUNCTION_KW@0..8 "function" WHITESPACE@8..9 " " STAR@9..10 "*" - NAME@10..13 + JS_IDENTIFIER_BINDING@10..13 IDENT@10..13 "foo" - PARAMETER_LIST@13..15 + JS_PARAMETER_LIST@13..15 L_PAREN@13..14 "(" LIST@14..14 R_PAREN@14..15 ")" diff --git a/crates/rslint_syntax/src/generated.rs b/crates/rslint_syntax/src/generated.rs index d1f0ac4ff03..cee306a04a0 100644 --- a/crates/rslint_syntax/src/generated.rs +++ b/crates/rslint_syntax/src/generated.rs @@ -187,7 +187,11 @@ pub enum SyntaxKind { JS_CATCH_DECLARATION, JS_FINALLY_CLAUSE, JS_DEBUGGER_STATEMENT, - FN_DECL, + JS_FUNCTION_DECLARATION, + JS_PARAMETER_LIST, + JS_REST_PARAMETER, + TS_RETURN_TYPE, + JS_IDENTIFIER_BINDING, NAME, JS_REFERENCE_IDENTIFIER_EXPRESSION, PARAMETER_LIST, diff --git a/xtask/js.ungram b/xtask/js.ungram index 10698d43e80..e407228e9b3 100644 --- a/xtask/js.ungram +++ b/xtask/js.ungram @@ -81,7 +81,7 @@ JsAnyStatement = | JsTryStatement | JsTryFinallyStatement | JsDebuggerStatement - | FnDecl + | JsFunctionDeclaration | ClassDecl | JsVariableDeclarationStatement | TsEnum @@ -350,7 +350,7 @@ ArrowExpr = ArrowExprParams = Name - | ParameterList + | JsParameterList // Object expression ObjectExpr = '{' props:(ObjectProp (',' ObjectProp)* ','?) '}' @@ -384,7 +384,7 @@ Constructor = accessibility:TsAccessibility? name:PropName type_params:TsTypeParams? - parameters:ParameterList + parameters:JsParameterList body:JsFunctionBody @@ -606,9 +606,9 @@ IdentProp = name: Name // MISCELLANEOUS /////////////// -Getter = 'get' key:PropName parameters:ParameterList body:JsFunctionBody +Getter = 'get' key:PropName parameters:JsParameterList body:JsFunctionBody -Setter = 'set' key:PropName parameters:ParameterList body:JsFunctionBody +Setter = 'set' key:PropName parameters:JsParameterList body:JsFunctionBody Method = 'static'? @@ -616,7 +616,7 @@ Method = '*'? name:PropName type_params:TsTypeParams? - parameters:ParameterList + parameters:JsParameterList ':'? return_type:TsType? body:JsFunctionBody @@ -635,17 +635,19 @@ JsReferenceIdentifierExpression = name: 'ident' // DECLARATIONS /////////////// -FnDecl = +JsFunctionDeclaration = 'async'? 'function' '*'? - name: Name - type_parameters:TsTypeParams? - parameters:ParameterList - ':'? - return_type:TsType? - body:JsFunctionBody + id: JsIdentifierBinding + type_parameters: TsTypeParams? + parameter_list: JsParameterList + return_type: TsReturnType? + body: JsFunctionBody +TsReturnType = + ':' + type: TsType JsVariableDeclarationStatement = declaration: JsVariableDeclaration ';'? @@ -680,7 +682,7 @@ ImportDecl = // export default declaration ExportDefaultDecl = 'export' 'default'? 'type'? decl:DefaultDecl DefaultDecl = - FnDecl + JsFunctionDeclaration | ClassDecl @@ -695,7 +697,7 @@ ExportWildcard = 'export' 'type'? '*' 'as'? ident: Ident? 'from' source: JsStrin ExportDecl = 'export' 'type'? decl: JsAnyExportDeclaration JsAnyExportDeclaration = - FnDecl + JsFunctionDeclaration | ClassDecl | JsVariableDeclarationStatement | TsEnum @@ -734,6 +736,15 @@ WildcardImport = '*' 'as'? ident: Ident? // @ematipico this one is not entirely correct I think.. ExportNamed = 'export' 'type'? 'from'? '{' specifiers:(Specifier (',' Specifier)* ','?) * '}' +/////////////// +// BINDINGS +/////////////// + +// Binds a value to an identifier. +// let x = OR function(test) {} +// ^ ^^^^ +JsIdentifierBinding = name: 'ident' + /////////////// // AUXILIARY /////////////// @@ -742,7 +753,18 @@ ExprOrBlock = JsAnyExpression | JsFunctionBody -ParameterList = '(' parameters:(Pattern (',' Pattern)* ','?) ')' + +JsParameterList = + '(' + parameters: (Pattern (',' Pattern)* ','?) + ')' + +JsAnyParameter = Pattern | JsRestParameter + +JsRestParameter = + '...' + binding: Pattern + ArgList = '(' args:(JsAnyExpression (',' JsAnyExpression)* ','?) ')' @@ -896,12 +918,12 @@ TsUnion = types:TsType* // typescript function type // Example: (something: String) => bool -TsFnType = params:ParameterList '=>' return_type:TsType? +TsFnType = params:JsParameterList '=>' return_type:TsType? // typescript constructor type // Exapmle: new (something: String): SomethingElse -TsConstructorType = 'new' params:ParameterList ':' return_type:TsType? +TsConstructorType = 'new' params:JsParameterList ':' return_type:TsType? // typescript conditional type @@ -974,11 +996,11 @@ TsTypeArgs = '<' args:TsType '>' // @ematipico we should make an example here -TsCallSignatureDecl = type_params:TsTypeParams parameters:ParameterList ':' return_type:TsType +TsCallSignatureDecl = type_params:TsTypeParams parameters:JsParameterList ':' return_type:TsType TsConstructSignatureDecl = 'new' type_params:TsTypeParams - parameters:ParameterList + parameters:JsParameterList // these two should be optional together ':'? return_type:TsType @@ -990,7 +1012,7 @@ TsMethodSignature = 'readonly'? key:JsAnyExpression type_params:TsTypeParams - parameters:ParameterList + parameters:JsParameterList '?'? ':' return_type:TsType diff --git a/xtask/src/codegen/kinds_src.rs b/xtask/src/codegen/kinds_src.rs index b548cbfaff7..bfc23b4deb7 100644 --- a/xtask/src/codegen/kinds_src.rs +++ b/xtask/src/codegen/kinds_src.rs @@ -201,7 +201,11 @@ pub const KINDS_SRC: KindsSrc = KindsSrc { "JS_CATCH_DECLARATION", "JS_FINALLY_CLAUSE", "JS_DEBUGGER_STATEMENT", - "FN_DECL", + "JS_FUNCTION_DECLARATION", + "JS_PARAMETER_LIST", + "JS_REST_PARAMETER", + "TS_RETURN_TYPE", + "JS_IDENTIFIER_BINDING", "NAME", "JS_REFERENCE_IDENTIFIER_EXPRESSION", "PARAMETER_LIST", diff --git a/xtask/src/coverage/test262 b/xtask/src/coverage/test262 index 19da3ca0757..26f1f4567ee 160000 --- a/xtask/src/coverage/test262 +++ b/xtask/src/coverage/test262 @@ -1 +1 @@ -Subproject commit 19da3ca0757248f7595ee09d532bb83dd438f2b5 +Subproject commit 26f1f4567ee7e33163d961c867d689173cbb9065 From 0befa4206de1a3379930748190116f36484f0459 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Mon, 15 Nov 2021 14:10:27 +0100 Subject: [PATCH 2/5] Rename `FnExpr` to `JsFunctionExpression` and remove decorators --- .../src/ts/expressions/expression.rs | 2 +- .../rslint_parser/src/ast/generated/nodes.rs | 299 +++++++++--------- crates/rslint_parser/src/state.rs | 2 - crates/rslint_parser/src/syntax/decl.rs | 58 +--- crates/rslint_parser/src/syntax/function.rs | 31 +- crates/rslint_parser/src/syntax/program.rs | 6 - crates/rslint_parser/src/syntax/stmt.rs | 124 +------- crates/rslint_parser/src/syntax/typescript.rs | 7 +- .../inline/err/function_decl_err.rast | 18 +- .../inline/ok/async_function_expr.rast | 4 +- .../test_data/inline/ok/await_expression.rast | 4 +- .../test_data/inline/ok/directives.rast | 2 +- .../test_data/inline/ok/function_decl.rast | 4 +- .../test_data/inline/ok/function_expr.rast | 4 +- crates/rslint_syntax/src/generated.rs | 2 +- xtask/js.ungram | 16 +- xtask/src/codegen/kinds_src.rs | 2 +- 17 files changed, 232 insertions(+), 353 deletions(-) diff --git a/crates/rome_formatter/src/ts/expressions/expression.rs b/crates/rome_formatter/src/ts/expressions/expression.rs index cf8795d2d7a..c6e47ade2dc 100644 --- a/crates/rome_formatter/src/ts/expressions/expression.rs +++ b/crates/rome_formatter/src/ts/expressions/expression.rs @@ -29,7 +29,7 @@ impl ToFormatElement for JsAnyExpression { JsAnyExpression::JsConditionalExpression(_) => todo!(), JsAnyExpression::AssignExpr(_) => todo!(), JsAnyExpression::JsSequenceExpression(expr) => expr.to_format_element(formatter), - JsAnyExpression::FnExpr(_) => todo!(), + JsAnyExpression::JsFunctionExpression(_) => todo!(), JsAnyExpression::ClassExpr(_) => todo!(), JsAnyExpression::NewTarget(_) => todo!(), JsAnyExpression::ImportMeta(_) => todo!(), diff --git a/crates/rslint_parser/src/ast/generated/nodes.rs b/crates/rslint_parser/src/ast/generated/nodes.rs index e3220d56616..d60d64b230b 100644 --- a/crates/rslint_parser/src/ast/generated/nodes.rs +++ b/crates/rslint_parser/src/ast/generated/nodes.rs @@ -831,6 +831,24 @@ impl JsConditionalExpression { } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct JsFunctionExpression { + pub(crate) syntax: SyntaxNode, +} +impl JsFunctionExpression { + pub fn async_token(&self) -> Option { support::token(&self.syntax, T![async]) } + pub fn function_token(&self) -> SyntaxResult { + support::required_token(&self.syntax, T![function]) + } + 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 parameters(&self) -> SyntaxResult { + support::required_node(&self.syntax) + } + pub fn return_type(&self) -> Option { support::node(&self.syntax) } + pub fn body(&self) -> SyntaxResult { support::required_node(&self.syntax) } +} +#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct JsImportCallExpression { pub(crate) syntax: SyntaxNode, } @@ -1064,23 +1082,6 @@ impl AssignExpr { } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct FnExpr { - pub(crate) syntax: SyntaxNode, -} -impl FnExpr { - pub fn async_token(&self) -> Option { support::token(&self.syntax, T![async]) } - pub fn function_token(&self) -> SyntaxResult { - support::required_token(&self.syntax, T![function]) - } - pub fn star_token(&self) -> Option { support::token(&self.syntax, T ! [*]) } - pub fn name(&self) -> SyntaxResult { support::required_node(&self.syntax) } - pub fn type_params(&self) -> Option { support::node(&self.syntax) } - pub fn parameters(&self) -> SyntaxResult { support::required_node(&self.syntax) } - pub fn colon_token(&self) -> Option { support::token(&self.syntax, T ! [:]) } - pub fn return_type(&self) -> Option { support::node(&self.syntax) } - pub fn body(&self) -> SyntaxResult { support::required_node(&self.syntax) } -} -#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct ClassExpr { pub(crate) syntax: SyntaxNode, } @@ -1225,6 +1226,15 @@ impl ArgList { } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct JsIdentifierBinding { + pub(crate) syntax: SyntaxNode, +} +impl JsIdentifierBinding { + pub fn name_token(&self) -> SyntaxResult { + support::required_token(&self.syntax, T![ident]) + } +} +#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct TsTypeParams { pub(crate) syntax: SyntaxNode, } @@ -1234,6 +1244,31 @@ impl TsTypeParams { pub fn r_angle_token(&self) -> Option { support::token(&self.syntax, T ! [>]) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct JsParameterList { + pub(crate) syntax: SyntaxNode, +} +impl JsParameterList { + pub fn l_paren_token(&self) -> SyntaxResult { + support::required_token(&self.syntax, T!['(']) + } + pub fn parameters(&self) -> AstSeparatedList { + support::separated_list(&self.syntax, 0usize) + } + pub fn r_paren_token(&self) -> SyntaxResult { + support::required_token(&self.syntax, T![')']) + } +} +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct TsReturnType { + pub(crate) syntax: SyntaxNode, +} +impl TsReturnType { + pub fn colon_token(&self) -> SyntaxResult { + support::required_token(&self.syntax, T ! [:]) + } + pub fn ty(&self) -> SyntaxResult { support::required_node(&self.syntax) } +} +#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct JsFunctionBody { pub(crate) syntax: SyntaxNode, } @@ -1252,21 +1287,6 @@ impl JsFunctionBody { } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct JsParameterList { - pub(crate) syntax: SyntaxNode, -} -impl JsParameterList { - pub fn l_paren_token(&self) -> SyntaxResult { - support::required_token(&self.syntax, T!['(']) - } - pub fn parameters(&self) -> AstSeparatedList { - support::separated_list(&self.syntax, 0usize) - } - pub fn r_paren_token(&self) -> SyntaxResult { - support::required_token(&self.syntax, T![')']) - } -} -#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct TsExprWithTypeArgs { pub(crate) syntax: SyntaxNode, } @@ -1664,25 +1684,6 @@ impl Condition { } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct JsIdentifierBinding { - pub(crate) syntax: SyntaxNode, -} -impl JsIdentifierBinding { - pub fn name_token(&self) -> SyntaxResult { - support::required_token(&self.syntax, T![ident]) - } -} -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct TsReturnType { - pub(crate) syntax: SyntaxNode, -} -impl TsReturnType { - pub fn colon_token(&self) -> SyntaxResult { - support::required_token(&self.syntax, T ! [:]) - } - pub fn ty(&self) -> SyntaxResult { support::required_node(&self.syntax) } -} -#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct JsVariableDeclarator { pub(crate) syntax: SyntaxNode, } @@ -2328,6 +2329,7 @@ pub enum JsAnyExpression { JsAwaitExpression(JsAwaitExpression), JsBinaryExpression(JsBinaryExpression), JsConditionalExpression(JsConditionalExpression), + JsFunctionExpression(JsFunctionExpression), JsImportCallExpression(JsImportCallExpression), JsLogicalExpression(JsLogicalExpression), JsParenthesizedExpression(JsParenthesizedExpression), @@ -2346,7 +2348,6 @@ pub enum JsAnyExpression { NewExpr(NewExpr), CallExpr(CallExpr), AssignExpr(AssignExpr), - FnExpr(FnExpr), ClassExpr(ClassExpr), NewTarget(NewTarget), ImportMeta(ImportMeta), @@ -2387,6 +2388,11 @@ pub enum JsAnyLiteral { JsRegexLiteral(JsRegexLiteral), } #[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub enum ArrowExprParams { + Name(Name), + JsParameterList(JsParameterList), +} +#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum TsType { TsAny(TsAny), TsUnknown(TsUnknown), @@ -2421,11 +2427,6 @@ pub enum TsType { TsInfer(TsInfer), } #[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub enum ArrowExprParams { - Name(Name), - JsParameterList(JsParameterList), -} -#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum ExprOrBlock { JsAnyExpression(JsAnyExpression), JsFunctionBody(JsFunctionBody), @@ -3191,6 +3192,17 @@ impl AstNode for JsConditionalExpression { } fn syntax(&self) -> &SyntaxNode { &self.syntax } } +impl AstNode for JsFunctionExpression { + fn can_cast(kind: SyntaxKind) -> bool { kind == JS_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 JsImportCallExpression { fn can_cast(kind: SyntaxKind) -> bool { kind == JS_IMPORT_CALL_EXPRESSION } fn cast(syntax: SyntaxNode) -> Option { @@ -3389,17 +3401,6 @@ impl AstNode for AssignExpr { } fn syntax(&self) -> &SyntaxNode { &self.syntax } } -impl AstNode for FnExpr { - fn can_cast(kind: SyntaxKind) -> bool { kind == FN_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 ClassExpr { fn can_cast(kind: SyntaxKind) -> bool { kind == CLASS_EXPR } fn cast(syntax: SyntaxNode) -> Option { @@ -3521,8 +3522,8 @@ impl AstNode for ArgList { } fn syntax(&self) -> &SyntaxNode { &self.syntax } } -impl AstNode for TsTypeParams { - fn can_cast(kind: SyntaxKind) -> bool { kind == TS_TYPE_PARAMS } +impl AstNode for JsIdentifierBinding { + fn can_cast(kind: SyntaxKind) -> bool { kind == JS_IDENTIFIER_BINDING } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { Some(Self { syntax }) @@ -3532,8 +3533,8 @@ impl AstNode for TsTypeParams { } fn syntax(&self) -> &SyntaxNode { &self.syntax } } -impl AstNode for JsFunctionBody { - fn can_cast(kind: SyntaxKind) -> bool { kind == JS_FUNCTION_BODY } +impl AstNode for TsTypeParams { + fn can_cast(kind: SyntaxKind) -> bool { kind == TS_TYPE_PARAMS } fn cast(syntax: SyntaxNode) -> Option { if Self::can_cast(syntax.kind()) { Some(Self { syntax }) @@ -3554,6 +3555,28 @@ impl AstNode for JsParameterList { } fn syntax(&self) -> &SyntaxNode { &self.syntax } } +impl AstNode for TsReturnType { + fn can_cast(kind: SyntaxKind) -> bool { kind == TS_RETURN_TYPE } + 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 JsFunctionBody { + fn can_cast(kind: SyntaxKind) -> bool { kind == JS_FUNCTION_BODY } + 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 TsExprWithTypeArgs { fn can_cast(kind: SyntaxKind) -> bool { kind == TS_EXPR_WITH_TYPE_ARGS } fn cast(syntax: SyntaxNode) -> Option { @@ -3928,28 +3951,6 @@ impl AstNode for Condition { } fn syntax(&self) -> &SyntaxNode { &self.syntax } } -impl AstNode for JsIdentifierBinding { - fn can_cast(kind: SyntaxKind) -> bool { kind == JS_IDENTIFIER_BINDING } - 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 TsReturnType { - fn can_cast(kind: SyntaxKind) -> bool { kind == TS_RETURN_TYPE } - 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 JsVariableDeclarator { fn can_cast(kind: SyntaxKind) -> bool { kind == JS_VARIABLE_DECLARATOR } fn cast(syntax: SyntaxNode) -> Option { @@ -4850,6 +4851,11 @@ impl From for JsAnyExpression { JsAnyExpression::JsConditionalExpression(node) } } +impl From for JsAnyExpression { + fn from(node: JsFunctionExpression) -> JsAnyExpression { + JsAnyExpression::JsFunctionExpression(node) + } +} impl From for JsAnyExpression { fn from(node: JsImportCallExpression) -> JsAnyExpression { JsAnyExpression::JsImportCallExpression(node) @@ -4918,9 +4924,6 @@ impl From for JsAnyExpression { impl From for JsAnyExpression { fn from(node: AssignExpr) -> JsAnyExpression { JsAnyExpression::AssignExpr(node) } } -impl From for JsAnyExpression { - fn from(node: FnExpr) -> JsAnyExpression { JsAnyExpression::FnExpr(node) } -} impl From for JsAnyExpression { fn from(node: ClassExpr) -> JsAnyExpression { JsAnyExpression::ClassExpr(node) } } @@ -4957,6 +4960,7 @@ impl AstNode for JsAnyExpression { | JS_AWAIT_EXPRESSION | JS_BINARY_EXPRESSION | JS_CONDITIONAL_EXPRESSION + | JS_FUNCTION_EXPRESSION | JS_IMPORT_CALL_EXPRESSION | JS_LOGICAL_EXPRESSION | JS_PARENTHESIZED_EXPRESSION @@ -4975,7 +4979,6 @@ impl AstNode for JsAnyExpression { | NEW_EXPR | CALL_EXPR | ASSIGN_EXPR - | FN_EXPR | CLASS_EXPR | NEW_TARGET | IMPORT_META @@ -4999,6 +5002,9 @@ impl AstNode for JsAnyExpression { JS_CONDITIONAL_EXPRESSION => { JsAnyExpression::JsConditionalExpression(JsConditionalExpression { syntax }) } + JS_FUNCTION_EXPRESSION => { + JsAnyExpression::JsFunctionExpression(JsFunctionExpression { syntax }) + } JS_IMPORT_CALL_EXPRESSION => { JsAnyExpression::JsImportCallExpression(JsImportCallExpression { syntax }) } @@ -5033,7 +5039,6 @@ impl AstNode for JsAnyExpression { NEW_EXPR => JsAnyExpression::NewExpr(NewExpr { syntax }), CALL_EXPR => JsAnyExpression::CallExpr(CallExpr { syntax }), ASSIGN_EXPR => JsAnyExpression::AssignExpr(AssignExpr { syntax }), - FN_EXPR => JsAnyExpression::FnExpr(FnExpr { syntax }), CLASS_EXPR => JsAnyExpression::ClassExpr(ClassExpr { syntax }), NEW_TARGET => JsAnyExpression::NewTarget(NewTarget { syntax }), IMPORT_META => JsAnyExpression::ImportMeta(ImportMeta { syntax }), @@ -5060,6 +5065,7 @@ impl AstNode for JsAnyExpression { JsAnyExpression::JsAwaitExpression(it) => &it.syntax, JsAnyExpression::JsBinaryExpression(it) => &it.syntax, JsAnyExpression::JsConditionalExpression(it) => &it.syntax, + JsAnyExpression::JsFunctionExpression(it) => &it.syntax, JsAnyExpression::JsImportCallExpression(it) => &it.syntax, JsAnyExpression::JsLogicalExpression(it) => &it.syntax, JsAnyExpression::JsParenthesizedExpression(it) => &it.syntax, @@ -5078,7 +5084,6 @@ impl AstNode for JsAnyExpression { JsAnyExpression::NewExpr(it) => &it.syntax, JsAnyExpression::CallExpr(it) => &it.syntax, JsAnyExpression::AssignExpr(it) => &it.syntax, - JsAnyExpression::FnExpr(it) => &it.syntax, JsAnyExpression::ClassExpr(it) => &it.syntax, JsAnyExpression::NewTarget(it) => &it.syntax, JsAnyExpression::ImportMeta(it) => &it.syntax, @@ -5255,6 +5260,34 @@ impl AstNode for JsAnyLiteral { } } } +impl From for ArrowExprParams { + fn from(node: Name) -> ArrowExprParams { ArrowExprParams::Name(node) } +} +impl From for ArrowExprParams { + fn from(node: JsParameterList) -> ArrowExprParams { ArrowExprParams::JsParameterList(node) } +} +impl AstNode for ArrowExprParams { + fn can_cast(kind: SyntaxKind) -> bool { + match kind { + NAME | JS_PARAMETER_LIST => true, + _ => false, + } + } + fn cast(syntax: SyntaxNode) -> Option { + let res = match syntax.kind() { + NAME => ArrowExprParams::Name(Name { syntax }), + JS_PARAMETER_LIST => ArrowExprParams::JsParameterList(JsParameterList { syntax }), + _ => return None, + }; + Some(res) + } + fn syntax(&self) -> &SyntaxNode { + match self { + ArrowExprParams::Name(it) => &it.syntax, + ArrowExprParams::JsParameterList(it) => &it.syntax, + } + } +} impl From for TsType { fn from(node: TsAny) -> TsType { TsType::TsAny(node) } } @@ -5433,34 +5466,6 @@ impl AstNode for TsType { } } } -impl From for ArrowExprParams { - fn from(node: Name) -> ArrowExprParams { ArrowExprParams::Name(node) } -} -impl From for ArrowExprParams { - fn from(node: JsParameterList) -> ArrowExprParams { ArrowExprParams::JsParameterList(node) } -} -impl AstNode for ArrowExprParams { - fn can_cast(kind: SyntaxKind) -> bool { - match kind { - NAME | JS_PARAMETER_LIST => true, - _ => false, - } - } - fn cast(syntax: SyntaxNode) -> Option { - let res = match syntax.kind() { - NAME => ArrowExprParams::Name(Name { syntax }), - JS_PARAMETER_LIST => ArrowExprParams::JsParameterList(JsParameterList { syntax }), - _ => return None, - }; - Some(res) - } - fn syntax(&self) -> &SyntaxNode { - match self { - ArrowExprParams::Name(it) => &it.syntax, - ArrowExprParams::JsParameterList(it) => &it.syntax, - } - } -} impl From for ExprOrBlock { fn from(node: JsFunctionBody) -> ExprOrBlock { ExprOrBlock::JsFunctionBody(node) } } @@ -6200,12 +6205,12 @@ impl std::fmt::Display for JsAnyLiteral { std::fmt::Display::fmt(self.syntax(), f) } } -impl std::fmt::Display for TsType { +impl std::fmt::Display for ArrowExprParams { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { std::fmt::Display::fmt(self.syntax(), f) } } -impl std::fmt::Display for ArrowExprParams { +impl std::fmt::Display for TsType { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { std::fmt::Display::fmt(self.syntax(), f) } @@ -6590,6 +6595,11 @@ impl std::fmt::Display for JsConditionalExpression { std::fmt::Display::fmt(self.syntax(), f) } } +impl std::fmt::Display for JsFunctionExpression { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + std::fmt::Display::fmt(self.syntax(), f) + } +} impl std::fmt::Display for JsImportCallExpression { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { std::fmt::Display::fmt(self.syntax(), f) @@ -6680,11 +6690,6 @@ impl std::fmt::Display for AssignExpr { std::fmt::Display::fmt(self.syntax(), f) } } -impl std::fmt::Display for FnExpr { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - std::fmt::Display::fmt(self.syntax(), f) - } -} impl std::fmt::Display for ClassExpr { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { std::fmt::Display::fmt(self.syntax(), f) @@ -6740,12 +6745,12 @@ impl std::fmt::Display for ArgList { std::fmt::Display::fmt(self.syntax(), f) } } -impl std::fmt::Display for TsTypeParams { +impl std::fmt::Display for JsIdentifierBinding { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { std::fmt::Display::fmt(self.syntax(), f) } } -impl std::fmt::Display for JsFunctionBody { +impl std::fmt::Display for TsTypeParams { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { std::fmt::Display::fmt(self.syntax(), f) } @@ -6755,6 +6760,16 @@ impl std::fmt::Display for JsParameterList { std::fmt::Display::fmt(self.syntax(), f) } } +impl std::fmt::Display for TsReturnType { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + std::fmt::Display::fmt(self.syntax(), f) + } +} +impl std::fmt::Display for JsFunctionBody { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + std::fmt::Display::fmt(self.syntax(), f) + } +} impl std::fmt::Display for TsExprWithTypeArgs { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { std::fmt::Display::fmt(self.syntax(), f) @@ -6925,16 +6940,6 @@ impl std::fmt::Display for Condition { std::fmt::Display::fmt(self.syntax(), f) } } -impl std::fmt::Display for JsIdentifierBinding { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - std::fmt::Display::fmt(self.syntax(), f) - } -} -impl std::fmt::Display for TsReturnType { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - std::fmt::Display::fmt(self.syntax(), f) - } -} impl std::fmt::Display for JsVariableDeclarator { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { std::fmt::Display::fmt(self.syntax(), f) diff --git a/crates/rslint_parser/src/state.rs b/crates/rslint_parser/src/state.rs index 67efc714554..eb227c26161 100644 --- a/crates/rslint_parser/src/state.rs +++ b/crates/rslint_parser/src/state.rs @@ -49,7 +49,6 @@ pub struct ParserState { pub(crate) no_recovery: bool, pub in_declare: bool, pub in_binding_list_for_signature: bool, - pub decorators_were_valid: bool, pub in_default: bool, pub for_head_error: Option, } @@ -84,7 +83,6 @@ impl Default for ParserState { no_recovery: false, in_declare: false, in_binding_list_for_signature: false, - decorators_were_valid: false, in_default: false, for_head_error: None, } diff --git a/crates/rslint_parser/src/syntax/decl.rs b/crates/rslint_parser/src/syntax/decl.rs index 5e10c010162..b48a40a366d 100644 --- a/crates/rslint_parser/src/syntax/decl.rs +++ b/crates/rslint_parser/src/syntax/decl.rs @@ -1,10 +1,9 @@ //! Class and function declarations. -use super::expr::{assign_expr, identifier_name, object_prop_name, STARTS_EXPR}; +use super::expr::{assign_expr, identifier_name, object_prop_name}; use super::pat::{binding_identifier, pattern}; use super::typescript::*; -use crate::syntax::function::{args_body, fn_body}; -use crate::syntax::stmt::function_body; +use crate::syntax::function::{args_body, function_body, function_body_or_declaration}; use crate::{SyntaxKind::*, *}; pub const BASE_METHOD_RECOVERY_SET: TokenSet = token_set![ @@ -18,39 +17,6 @@ pub const BASE_METHOD_RECOVERY_SET: TokenSet = token_set![ JS_STRING_LITERAL_TOKEN ]; -pub fn decorators(p: &mut Parser) -> Vec { - let mut decorators = Vec::with_capacity(1); - while p.at(T![@]) { - let decorator = decorator(p); - if !p.syntax.decorators { - let err = p - .err_builder("decorators are not allowed") - .primary(decorator.range(p), ""); - - p.error(err); - } - decorators.push(decorator); - } - decorators -} - -pub fn decorator(p: &mut Parser) -> CompletedMarker { - let m = p.start(); - p.expect(T![@]); - - if !STARTS_EXPR.contains(p.cur()) { - let err = p - .err_builder("decorators require an expression to call") - .primary(p.cur_tok().range, ""); - - p.error(err); - } else { - assign_expr(p); - } - - m.complete(p, TS_DECORATOR) -} - pub fn maybe_private_name(p: &mut Parser) -> Option { if p.at(T![#]) { let m = p.start(); @@ -242,13 +208,7 @@ fn parameters_common(p: &mut Parser, constructor_params: bool) -> CompletedMarke p.expect(T![,]); } - let decorator = if p.at(T![@]) { - decorators(p).into_iter().next() - } else { - None - }; - - let marker = if p.at(T![...]) { + if p.at(T![...]) { let m = p.start(); p.bump_any(); pattern(p, true, false); @@ -339,13 +299,6 @@ fn parameters_common(p: &mut Parser, constructor_params: bool) -> CompletedMarke None } }; - - if let (Some(res), Some(decorator)) = (marker, decorator) { - let kind = res.kind(); - let m = decorator.precede(p); - res.undo_completion(p).abandon(p); - m.complete(p, kind); - } } parameters_list.complete(p, LIST); @@ -360,7 +313,7 @@ pub fn arrow_body(p: &mut Parser) -> Option { ..p.state.clone() }); if guard.at(T!['{']) { - function_body(&mut *guard, None) + function_body(&mut *guard) } else { assign_expr(&mut *guard) } @@ -695,7 +648,6 @@ fn consume_leading_tokens( fn class_member_no_semi(p: &mut Parser) -> Option { let m = p.start(); - decorators(p); let has_accessibility = matches!(p.cur_src(), "public" | "private" | "protected"); let mut offset = has_accessibility as usize; let declare = p.nth_src(offset) == "declare"; @@ -938,7 +890,7 @@ fn class_member_no_semi(p: &mut Parser) -> Option { p.error(err); } - fn_body(p); + function_body_or_declaration(p); // FIXME(RDambrosio016): if there is no body we need to issue errors for any assign patterns diff --git a/crates/rslint_parser/src/syntax/function.rs b/crates/rslint_parser/src/syntax/function.rs index c58bc3c8a81..7f44677d027 100644 --- a/crates/rslint_parser/src/syntax/function.rs +++ b/crates/rslint_parser/src/syntax/function.rs @@ -1,10 +1,11 @@ use crate::syntax::decl::{is_semi, parameter_list}; -use crate::syntax::pat::{binding_identifier, opt_binding_identifier}; -use crate::syntax::stmt::function_body; +use crate::syntax::pat::opt_binding_identifier; +use crate::syntax::stmt::block_impl; use crate::syntax::typescript::{ts_type_or_type_predicate_ann, ts_type_params}; use crate::{CompletedMarker, Parser, ParserState}; use rslint_syntax::SyntaxKind::{ - ERROR, FN_EXPR, JS_FUNCTION_DECLARATION, JS_IDENTIFIER_BINDING, TS_RETURN_TYPE, + ERROR, JS_FUNCTION_BODY, JS_FUNCTION_DECLARATION, JS_FUNCTION_EXPRESSION, + JS_IDENTIFIER_BINDING, TS_RETURN_TYPE, }; use rslint_syntax::{SyntaxKind, T}; use std::collections::HashMap; @@ -25,7 +26,7 @@ pub(super) fn function_declaration(p: &mut Parser) -> CompletedMarker { } pub(super) fn function_expression(p: &mut Parser) -> CompletedMarker { - function(p, FN_EXPR) + function(p, JS_FUNCTION_EXPRESSION) } fn function(p: &mut Parser, kind: SyntaxKind) -> CompletedMarker { @@ -69,17 +70,31 @@ fn function(p: &mut Parser, kind: SyntaxKind) -> CompletedMarker { parameter_types(guard); parameter_list(guard); return_type(guard); - fn_body(guard); + + if kind == JS_FUNCTION_DECLARATION { + function_body_or_declaration(guard); + } else { + function_body(guard); + } m.complete(guard, kind) } -pub(super) fn fn_body(p: &mut Parser) { +pub(super) fn function_body(p: &mut Parser) -> Option { + block_impl(p, JS_FUNCTION_BODY, None) +} + +// TODO 1725 This is probably not ideal (same with the `declare` keyword). We should +// use a different AST type for function declarations. For example, a function declaration should +// never have a body but that would be allowed with this approach. Same for interfaces, interface +// methods should never have a body +/// Either parses a typescript declaration body or the function body +pub(crate) fn function_body_or_declaration(p: &mut Parser) { // omitting the body is allowed in ts if p.typescript() && !p.at(T!['{']) && is_semi(p, 0) { p.eat(T![;]); } else { - let mut complete = function_body(p, None); + let mut complete = function_body(p); if let Some(ref mut block) = complete { if p.state.in_declare { let err = p @@ -99,7 +114,7 @@ pub(super) fn args_body(p: &mut Parser) { parameter_types(p); parameter_list(p); return_type(p); - fn_body(p); + function_body_or_declaration(p); } fn parameter_types(p: &mut Parser) { diff --git a/crates/rslint_parser/src/syntax/program.rs b/crates/rslint_parser/src/syntax/program.rs index 34845c7eb71..0c3e51fd0f9 100644 --- a/crates/rslint_parser/src/syntax/program.rs +++ b/crates/rslint_parser/src/syntax/program.rs @@ -364,7 +364,6 @@ pub fn export_decl(p: &mut Parser) -> CompletedMarker { if !only_ty && !exports_ns && p.eat(T![default]) { if p.cur_src() == "abstract" && p.nth_at(1, T![class]) { - p.state.decorators_were_valid = true; let inner = p.start(); if !p.typescript() { let err = p @@ -398,7 +397,6 @@ pub fn export_decl(p: &mut Parser) -> CompletedMarker { } if p.at(T![class]) { - p.state.decorators_were_valid = true; class_decl( &mut *p.with_state(ParserState { in_default: true, @@ -410,7 +408,6 @@ pub fn export_decl(p: &mut Parser) -> CompletedMarker { } if p.cur_src() == "async" && p.nth_at(1, T![function]) && !p.has_linebreak_before_n(1) { - p.state.decorators_were_valid = true; function_declaration(p); return m.complete(p, EXPORT_DEFAULT_DECL); } @@ -425,17 +422,14 @@ pub fn export_decl(p: &mut Parser) -> CompletedMarker { } if !only_ty && p.at(T![class]) { - p.state.decorators_were_valid = true; class_decl(p, false); } else if !only_ty && p.cur_src() == "async" && p.nth_at(1, T![function]) && !p.has_linebreak_before_n(1) { - p.state.decorators_were_valid = true; function_declaration(p); } else if !only_ty && p.at(T![function]) { - p.state.decorators_were_valid = true; function_declaration(p); } else if !only_ty && p.at(T![const]) && p.nth_src(1) == "enum" { ts_enum(p).err_if_not_ts(p, "enums can only be used in TypeScript files"); diff --git a/crates/rslint_parser/src/syntax/stmt.rs b/crates/rslint_parser/src/syntax/stmt.rs index 1ee10a1d9d7..446355e4a6f 100644 --- a/crates/rslint_parser/src/syntax/stmt.rs +++ b/crates/rslint_parser/src/syntax/stmt.rs @@ -2,13 +2,13 @@ //! //! See the [ECMAScript spec](https://www.ecma-international.org/ecma-262/5.1/#sec-12). -use super::decl::{class_decl, decorators}; +use super::decl::class_decl; use super::expr::{assign_expr, expr, EXPR_RECOVERY_SET, STARTS_EXPR}; use super::pat::*; use super::program::{export_decl, import_decl}; use super::typescript::*; use super::util::{check_for_stmt_declaration, check_label_use, check_lhs}; -use crate::syntax::function::{function_declaration, function_expression}; +use crate::syntax::function::function_declaration; use crate::{SyntaxKind::*, *}; pub const STMT_RECOVERY_SET: TokenSet = token_set![ @@ -66,16 +66,7 @@ pub fn semi(p: &mut Parser, err_range: Range) { } /// A generic statement such as a block, if, while, with, etc -pub fn stmt( - p: &mut Parser, - recovery_set: impl Into>, - decorator: Option, -) -> Option { - let decorator = if p.at(T![@]) { - decorators(p).into_iter().next() // should be always Some - } else { - decorator - }; +pub fn stmt(p: &mut Parser, recovery_set: impl Into>) -> Option { let res = match p.cur() { T![;] => empty_stmt(p), T!['{'] => block_stmt(p, recovery_set).unwrap(), // It is only ever None if there is no `{`, @@ -97,47 +88,21 @@ pub fn stmt( T![continue] => continue_stmt(p), T![throw] => throw_stmt(p), T![debugger] => debugger_stmt(p), - T![function] => { - p.state.decorators_were_valid = true; - // TODO: Should we change this to fn_expr if there is no name? - function_declaration(p) - } - T![class] => { - p.state.decorators_were_valid = true; - let complete = class_decl(p, false); - if let Some(decorator) = decorator { - let new = decorator.precede(p); - complete.undo_completion(p); - new.complete(p, CLASS_DECL) - } else { - complete - } - } + T![function] => function_declaration(p), + T![class] => class_decl(p, false), T![ident] if p.cur_src() == "async" && p.nth_at(1, T![function]) && !p.has_linebreak_before_n(1) => { - p.state.decorators_were_valid = true; - function_expression(p) + function_declaration(p) } T![ident] if p.cur_src() == "let" && FOLLOWS_LET.contains(p.nth(1)) => { variable_declaration_statement(p) } // TODO: handle `() => {};` with less of a hack - _ if p.at_ts(STARTS_EXPR) || p.at(T![<]) => { - let complete = expr_stmt(p, decorator); - if let Some(decorator) = decorator.filter(|_| !p.state.decorators_were_valid) { - let err = p - .err_builder("decorators are not valid in this position") - .primary(decorator.range(p), ""); - - p.error(err); - } - p.state.decorators_were_valid = false; - return complete; - } + _ if p.at_ts(STARTS_EXPR) || p.at(T![<]) => return expr_stmt(p), _ => { let err = p .err_builder("Expected a statement or declaration, but found none") @@ -157,19 +122,10 @@ pub fn stmt( } }; - if let Some(decorator) = decorator.filter(|_| !p.state.decorators_were_valid) { - let err = p - .err_builder("decorators are not valid in this position") - .primary(decorator.range(p), ""); - - p.error(err); - } - p.state.decorators_were_valid = false; - Some(res) } -fn expr_stmt(p: &mut Parser, decorator: Option) -> Option { +fn expr_stmt(p: &mut Parser) -> Option { let start = p.cur_tok().range.start; // this is *technically* wrong because it would be an expr stmt in js but for our purposes // we treat these as always being ts declarations since ambiguity is inefficient in this style of @@ -181,12 +137,6 @@ fn expr_stmt(p: &mut Parser, decorator: Option) -> Option) -> Option CompletedMarker { m.complete(p, JS_EMPTY_STATEMENT) } -pub(crate) fn function_body( - p: &mut Parser, - recovery_set: impl Into>, -) -> Option { - block_impl(p, JS_FUNCTION_BODY, recovery_set) -} - /// A block statement consisting of statements wrapped in curly brackets. // test block_stmt // {} @@ -441,7 +384,7 @@ pub(crate) fn block_stmt( } /// A block wrapped in curly brackets. Can either be a function body or a block statement. -fn block_impl( +pub(super) fn block_impl( p: &mut Parser, block_kind: SyntaxKind, recovery_set: impl Into>, @@ -544,13 +487,6 @@ pub(crate) fn statements( break; } - let decorator = if p.at(T![@]) { - decorators(p).into_iter().next() - } else { - None - }; - let mut is_import_export = false; - match p.cur() { // test_err import_decl_not_top_level // { @@ -559,14 +495,7 @@ pub(crate) fn statements( // make sure we dont try parsing import.meta or import() as declarations T![import] if !token_set![T![.], T!['(']].contains(p.nth(1)) => { - is_import_export = true; let mut m = import_decl(p); - if let Some(decorator) = decorator { - let kind = m.kind(); - let new = decorator.precede(p); - m.undo_completion(p); - m = new.complete(p, kind) - } if !p.state.is_module && !p.typescript() { let err = p .err_builder("Illegal use of an import declaration outside of a module") @@ -589,14 +518,7 @@ pub(crate) fn statements( // export { pain } from "life"; // } T![export] => { - is_import_export = true; let mut m = export_decl(p); - if let Some(decorator) = decorator { - let kind = m.kind(); - let new = decorator.precede(p); - m.undo_completion(p); - m = new.complete(p, kind) - } if !p.state.is_module && !p.typescript() { let err = p .err_builder("Illegal use of an export declaration outside of a module") @@ -615,20 +537,9 @@ pub(crate) fn statements( } } _ => { - stmt(p, recovery_set, decorator); + stmt(p, recovery_set); } }; - - if let Some(decorator) = - decorator.filter(|_| !p.state.decorators_were_valid && is_import_export) - { - let err = p - .err_builder("decorators are not valid in this position") - .primary(decorator.range(p), ""); - - p.error(err); - } - p.state.decorators_were_valid = false; } list_start.complete(p, LIST); @@ -672,13 +583,13 @@ pub fn if_stmt(p: &mut Parser) -> CompletedMarker { // body // allows us to recover from `if (true) else {}` - stmt(p, STMT_RECOVERY_SET.union(token_set![T![else]]), None); + stmt(p, STMT_RECOVERY_SET.union(token_set![T![else]])); // else clause if p.at(T![else]) { let else_clause = p.start(); p.eat(T![else]); - stmt(p, None, None); + stmt(p, None); else_clause.complete(p, JS_ELSE_CLAUSE); } @@ -691,7 +602,7 @@ pub fn with_stmt(p: &mut Parser) -> CompletedMarker { p.expect(T![with]); parenthesized_expression(p); - stmt(p, None, None); + stmt(p, None); let mut complete = m.complete(p, JS_WITH_STATEMENT); if p.state.strict.is_some() { @@ -726,7 +637,6 @@ pub fn while_stmt(p: &mut Parser) -> CompletedMarker { ..p.state.clone() }), None, - None, ); m.complete(p, JS_WHILE_STATEMENT) } @@ -893,7 +803,6 @@ pub fn do_stmt(p: &mut Parser) -> CompletedMarker { ..p.state.clone() }), None, - None, ); p.expect(T![while]); parenthesized_expression(p); @@ -1016,7 +925,6 @@ pub fn for_stmt(p: &mut Parser) -> CompletedMarker { ..p.state.clone() }), None, - None, ); m.complete(p, kind) } @@ -1034,7 +942,7 @@ fn switch_clause(p: &mut Parser) -> Option> { let end = p.cur_tok().range.end; let cons_list = p.start(); while !p.at_ts(token_set![T![default], T![case], T!['}'], EOF]) { - stmt(p, None, None); + stmt(p, None); } cons_list.complete(p, LIST); m.complete(p, JS_DEFAULT_CLAUSE); @@ -1046,7 +954,7 @@ fn switch_clause(p: &mut Parser) -> Option> { p.expect(T![:]); let cons_list = p.start(); while !p.at_ts(token_set![T![default], T![case], T!['}'], EOF]) { - stmt(p, None, None); + stmt(p, None); } cons_list.complete(p, LIST); m.complete(p, JS_CASE_CLAUSE); diff --git a/crates/rslint_parser/src/syntax/typescript.rs b/crates/rslint_parser/src/syntax/typescript.rs index ba901c478e4..58838ad1afc 100644 --- a/crates/rslint_parser/src/syntax/typescript.rs +++ b/crates/rslint_parser/src/syntax/typescript.rs @@ -134,12 +134,8 @@ pub(crate) fn ts_declare(p: &mut Parser) -> Option { ..p.state.clone() }); Some(match p.nth(1) { - T![function] => { - p.state.decorators_were_valid = true; - function_declaration(p) - } + T![function] => function_declaration(p), T![class] => { - p.state.decorators_were_valid = true; let m = p.start(); p.bump_remap(T![declare]); class_decl(p, false).undo_completion(p).abandon(p); @@ -196,7 +192,6 @@ pub(crate) fn ts_declare(p: &mut Parser) -> Option { pub(crate) fn ts_decl(p: &mut Parser) -> Option { if p.cur_src() == "abstract" { - p.state.decorators_were_valid = true; let m = p.start(); let range = p.cur_tok().range; p.bump_remap(T![abstract]); diff --git a/crates/rslint_parser/test_data/inline/err/function_decl_err.rast b/crates/rslint_parser/test_data/inline/err/function_decl_err.rast index d899cb0cfd9..631f69ea072 100644 --- a/crates/rslint_parser/test_data/inline/err/function_decl_err.rast +++ b/crates/rslint_parser/test_data/inline/err/function_decl_err.rast @@ -39,7 +39,7 @@ JS_ROOT@0..114 LIST@40..40 R_CURLY@40..41 "}" WHITESPACE@41..42 "\n" - FN_EXPR@42..61 + JS_FUNCTION_DECLARATION@42..61 ASYNC_KW@42..47 "async" WHITESPACE@47..48 " " FUNCTION_KW@48..56 "function" @@ -54,7 +54,7 @@ JS_ROOT@0..114 LIST@60..60 R_CURLY@60..61 "}" WHITESPACE@61..62 "\n" - FN_EXPR@62..83 + JS_FUNCTION_DECLARATION@62..83 ASYNC_KW@62..67 "async" WHITESPACE@67..68 " " FUNCTION_KW@68..76 "function" @@ -153,6 +153,20 @@ error[SyntaxError]: Expected an identifier or pattern, but found none 3 │ function *() {} │ ^ +-- +error[SyntaxError]: expected a name for the function in a function declaration, but found none + ┌─ function_decl_err.js:4:15 + │ +4 │ async function() {} + │ ^ + +-- +error[SyntaxError]: expected a name for the function in a function declaration, but found none + ┌─ function_decl_err.js:5:17 + │ +5 │ async function *() {} + │ ^ + -- error[SyntaxError]: Expected a semicolon or an implicit semicolon after a statement, but found none ┌─ function_decl_err.js:7:7 diff --git a/crates/rslint_parser/test_data/inline/ok/async_function_expr.rast b/crates/rslint_parser/test_data/inline/ok/async_function_expr.rast index 7d3c6104a5f..c3fab3ed2bf 100644 --- a/crates/rslint_parser/test_data/inline/ok/async_function_expr.rast +++ b/crates/rslint_parser/test_data/inline/ok/async_function_expr.rast @@ -14,7 +14,7 @@ JS_ROOT@0..62 JS_EQUAL_VALUE_CLAUSE@6..27 EQ@6..7 "=" WHITESPACE@7..8 " " - FN_EXPR@8..27 + JS_FUNCTION_EXPRESSION@8..27 ASYNC_KW@8..13 "async" WHITESPACE@13..14 " " FUNCTION_KW@14..22 "function" @@ -43,7 +43,7 @@ JS_ROOT@0..62 JS_EQUAL_VALUE_CLAUSE@35..60 EQ@35..36 "=" WHITESPACE@36..37 " " - FN_EXPR@37..60 + JS_FUNCTION_EXPRESSION@37..60 ASYNC_KW@37..42 "async" WHITESPACE@42..43 " " FUNCTION_KW@43..51 "function" diff --git a/crates/rslint_parser/test_data/inline/ok/await_expression.rast b/crates/rslint_parser/test_data/inline/ok/await_expression.rast index 5a0b2d679c7..bfaf1afe116 100644 --- a/crates/rslint_parser/test_data/inline/ok/await_expression.rast +++ b/crates/rslint_parser/test_data/inline/ok/await_expression.rast @@ -1,7 +1,7 @@ JS_ROOT@0..116 LIST@0..0 LIST@0..115 - FN_EXPR@0..76 + JS_FUNCTION_DECLARATION@0..76 ASYNC_KW@0..5 "async" WHITESPACE@5..6 " " FUNCTION_KW@6..14 "function" @@ -63,7 +63,7 @@ JS_ROOT@0..116 WHITESPACE@74..75 "\n" R_CURLY@75..76 "}" WHITESPACE@76..78 "\n\n" - FN_EXPR@78..115 + JS_FUNCTION_DECLARATION@78..115 ASYNC_KW@78..83 "async" WHITESPACE@83..84 " " FUNCTION_KW@84..92 "function" diff --git a/crates/rslint_parser/test_data/inline/ok/directives.rast b/crates/rslint_parser/test_data/inline/ok/directives.rast index c52cfc14486..f27666dfffd 100644 --- a/crates/rslint_parser/test_data/inline/ok/directives.rast +++ b/crates/rslint_parser/test_data/inline/ok/directives.rast @@ -76,7 +76,7 @@ JS_ROOT@0..321 JS_EXPRESSION_STATEMENT@135..217 JS_PARENTHESIZED_EXPRESSION@135..216 L_PAREN@135..136 "(" - FN_EXPR@136..215 + JS_FUNCTION_EXPRESSION@136..215 FUNCTION_KW@136..144 "function" WHITESPACE@144..145 " " JS_PARAMETER_LIST@145..147 diff --git a/crates/rslint_parser/test_data/inline/ok/function_decl.rast b/crates/rslint_parser/test_data/inline/ok/function_decl.rast index e01f439bb6a..ffa409941c5 100644 --- a/crates/rslint_parser/test_data/inline/ok/function_decl.rast +++ b/crates/rslint_parser/test_data/inline/ok/function_decl.rast @@ -53,7 +53,7 @@ JS_ROOT@0..142 LIST@58..58 R_CURLY@58..59 "}" WHITESPACE@59..60 "\n" - FN_EXPR@60..84 + JS_FUNCTION_DECLARATION@60..84 ASYNC_KW@60..65 "async" WHITESPACE@65..66 " " FUNCTION_KW@66..74 "function" @@ -72,7 +72,7 @@ JS_ROOT@0..142 LIST@83..83 R_CURLY@83..84 "}" WHITESPACE@84..85 "\n" - FN_EXPR@85..108 + JS_FUNCTION_DECLARATION@85..108 ASYNC_KW@85..90 "async" WHITESPACE@90..91 " " FUNCTION_KW@91..99 "function" diff --git a/crates/rslint_parser/test_data/inline/ok/function_expr.rast b/crates/rslint_parser/test_data/inline/ok/function_expr.rast index 6e375a567f7..71bac515774 100644 --- a/crates/rslint_parser/test_data/inline/ok/function_expr.rast +++ b/crates/rslint_parser/test_data/inline/ok/function_expr.rast @@ -14,7 +14,7 @@ JS_ROOT@0..48 JS_EQUAL_VALUE_CLAUSE@6..21 EQ@6..7 "=" WHITESPACE@7..8 " " - FN_EXPR@8..21 + JS_FUNCTION_EXPRESSION@8..21 FUNCTION_KW@8..16 "function" JS_PARAMETER_LIST@16..18 L_PAREN@16..17 "(" @@ -40,7 +40,7 @@ JS_ROOT@0..48 JS_EQUAL_VALUE_CLAUSE@28..47 EQ@28..29 "=" WHITESPACE@29..30 " " - FN_EXPR@30..47 + JS_FUNCTION_EXPRESSION@30..47 FUNCTION_KW@30..38 "function" WHITESPACE@38..39 " " JS_IDENTIFIER_BINDING@39..42 diff --git a/crates/rslint_syntax/src/generated.rs b/crates/rslint_syntax/src/generated.rs index cee306a04a0..12654b30d16 100644 --- a/crates/rslint_syntax/src/generated.rs +++ b/crates/rslint_syntax/src/generated.rs @@ -204,7 +204,7 @@ pub enum SyntaxKind { SETTER, JS_PARENTHESIZED_EXPRESSION, NEW_EXPR, - FN_EXPR, + JS_FUNCTION_EXPRESSION, BRACKET_EXPR, DOT_EXPR, CALL_EXPR, diff --git a/xtask/js.ungram b/xtask/js.ungram index e407228e9b3..da7cf05e20e 100644 --- a/xtask/js.ungram +++ b/xtask/js.ungram @@ -246,6 +246,7 @@ JsAnyExpression = | JsAwaitExpression | JsBinaryExpression | JsConditionalExpression + | JsFunctionExpression | JsImportCallExpression | JsLogicalExpression | JsParenthesizedExpression @@ -264,7 +265,6 @@ JsAnyExpression = | NewExpr | CallExpr | AssignExpr - | FnExpr | ClassExpr | NewTarget | ImportMeta @@ -326,17 +326,15 @@ JsConditionalExpression = // import meta expression ImportMeta = 'import' '.' -// function expression -FnExpr = +JsFunctionExpression = 'async'? 'function' '*'? - name:Name - type_params:TsTypeParams? - parameters:ArgList - ':'? - return_type:TsType? - body:JsFunctionBody + id: JsIdentifierBinding? + type_params: TsTypeParams? + parameters: JsParameterList + return_type: TsReturnType? + body: JsFunctionBody // Arrow expression ArrowExpr = diff --git a/xtask/src/codegen/kinds_src.rs b/xtask/src/codegen/kinds_src.rs index bfc23b4deb7..fab2322949b 100644 --- a/xtask/src/codegen/kinds_src.rs +++ b/xtask/src/codegen/kinds_src.rs @@ -218,7 +218,7 @@ pub const KINDS_SRC: KindsSrc = KindsSrc { "SETTER", "JS_PARENTHESIZED_EXPRESSION", "NEW_EXPR", - "FN_EXPR", + "JS_FUNCTION_EXPRESSION", "BRACKET_EXPR", "DOT_EXPR", "CALL_EXPR", From 4361fcab2b6779f9831c806d8c115d9dd1f42fa5 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Mon, 15 Nov 2021 14:54:45 +0100 Subject: [PATCH 3/5] 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 d1f23cff0e8..76cb2fa965d 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 bdc4d97ff3e..00000000000 --- 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 b9ca823b5cd..a9bdebf8120 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 c6e47ade2dc..5376579071e 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 176c9ef5fbd..3bbdca88191 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 7ce42becca2..022d5de31ce 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 d60d64b230b..b3f96ad845d 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