diff --git a/crates/rome_js_analyze/src/analyzers/style/use_single_var_declarator.rs b/crates/rome_js_analyze/src/analyzers/style/use_single_var_declarator.rs index 1142dce4234..005dba8ce9d 100644 --- a/crates/rome_js_analyze/src/analyzers/style/use_single_var_declarator.rs +++ b/crates/rome_js_analyze/src/analyzers/style/use_single_var_declarator.rs @@ -37,6 +37,7 @@ declare_rule! { impl Rule for UseSingleVarDeclarator { type Query = Ast; type State = ( + Option, JsSyntaxToken, JsVariableDeclaratorList, Option, @@ -53,7 +54,7 @@ impl Rule for UseSingleVarDeclarator { } = node.as_fields(); let JsVariableDeclarationFields { - await_token: _, + await_token, kind, declarators, } = declaration.ok()?.as_fields(); @@ -64,7 +65,7 @@ impl Rule for UseSingleVarDeclarator { return None; } - Some((kind, declarators, semicolon_token)) + Some((await_token, kind, declarators, semicolon_token)) } fn diagnostic(ctx: &RuleContext, _state: &Self::State) -> Option { @@ -87,7 +88,7 @@ impl Rule for UseSingleVarDeclarator { return None; } - let (kind, declarators, semicolon_token) = state; + let (await_token, kind, declarators, semicolon_token) = state; let index = prev_parent .children() @@ -198,14 +199,18 @@ impl Rule for UseSingleVarDeclarator { ) }; - let mut builder = make::js_variable_statement( - make::js_variable_declaration( - kind, - make::js_variable_declarator_list([declarator], []), - ) - .build(), + let mut variable_declaration = make::js_variable_declaration( + kind, + make::js_variable_declarator_list([declarator], []), ); + if let Some(await_token) = await_token { + variable_declaration = + variable_declaration.with_await_token(await_token.clone()); + } + + let mut builder = make::js_variable_statement(variable_declaration.build()); + let semicolon_token = if index + 1 == declarators_len { last_semicolon_token } else { diff --git a/crates/rome_js_analyze/src/semantic_analyzers/nursery/use_naming_convention.rs b/crates/rome_js_analyze/src/semantic_analyzers/nursery/use_naming_convention.rs index f717618ee32..3a4e89cf4b5 100644 --- a/crates/rome_js_analyze/src/semantic_analyzers/nursery/use_naming_convention.rs +++ b/crates/rome_js_analyze/src/semantic_analyzers/nursery/use_naming_convention.rs @@ -607,6 +607,7 @@ enum Named { LocalConst, LocalLet, LocalVar, + LocalUsing, Namespace, ObjectGetter, ObjectMethod, @@ -828,9 +829,11 @@ impl Named { (JsVariableKind::Const, false) => Named::LocalConst, (JsVariableKind::Let, false) => Named::LocalLet, (JsVariableKind::Var, false) => Named::LocalVar, + (JsVariableKind::Using, false) => Named::LocalUsing, (JsVariableKind::Const, true) => Named::TopLevelConst, (JsVariableKind::Let, true) => Named::TopLevelLet, (JsVariableKind::Var, true) => Named::TopLevelVar, + (JsVariableKind::Using, true) => Named::LocalUsing, }) } @@ -881,6 +884,7 @@ impl Named { | Named::LocalConst | Named::LocalLet | Named::LocalVar + | Named::LocalUsing | Named::ObjectGetter | Named::ObjectMethod | Named::ObjectProperty @@ -938,6 +942,7 @@ impl std::fmt::Display for Named { Named::LocalConst => "local const", Named::LocalLet => "local let", Named::LocalVar => "local var", + Named::LocalUsing => "local using", Named::Namespace => "namespace", Named::ObjectGetter => "object getter", Named::ObjectMethod => "object method", diff --git a/crates/rome_js_formatter/src/js/declarations/for_variable_declaration.rs b/crates/rome_js_formatter/src/js/declarations/for_variable_declaration.rs index eb9134018ff..7775fbdb8dd 100644 --- a/crates/rome_js_formatter/src/js/declarations/for_variable_declaration.rs +++ b/crates/rome_js_formatter/src/js/declarations/for_variable_declaration.rs @@ -16,25 +16,16 @@ impl FormatNodeRule for FormatJsForVariableDeclaration } = node.as_fields(); if let Some(await_token) = await_token { - write![ - f, - [group(&format_args![ - await_token.format(), - space(), - kind_token.format(), - space(), - declarator.format() - ])] - ] - } else { - write![ - f, - [group(&format_args![ - kind_token.format(), - space(), - declarator.format() - ])] - ] + write!(f, [await_token.format(), space()])?; } + + write![ + f, + [group(&format_args![ + kind_token.format(), + space(), + declarator.format() + ])] + ] } } diff --git a/crates/rome_js_formatter/src/js/declarations/variable_declaration.rs b/crates/rome_js_formatter/src/js/declarations/variable_declaration.rs index 0741b3bae70..eac7358caff 100644 --- a/crates/rome_js_formatter/src/js/declarations/variable_declaration.rs +++ b/crates/rome_js_formatter/src/js/declarations/variable_declaration.rs @@ -16,25 +16,16 @@ impl FormatNodeRule for FormatJsVariableDeclaration { } = node.as_fields(); if let Some(await_token) = await_token { - write![ - f, - [group(&format_args![ - await_token.format(), - space(), - kind.format(), - space(), - declarators.format() - ])] - ] - } else { - write![ - f, - [group(&format_args![ - kind.format(), - space(), - declarators.format() - ])] - ] + write!(f, [await_token.format(), space()])?; } + + write![ + f, + [group(&format_args![ + kind.format(), + space(), + declarators.format() + ])] + ] } } diff --git a/crates/rome_js_syntax/src/stmt_ext.rs b/crates/rome_js_syntax/src/stmt_ext.rs index 170f49aafec..56f058bfa94 100644 --- a/crates/rome_js_syntax/src/stmt_ext.rs +++ b/crates/rome_js_syntax/src/stmt_ext.rs @@ -35,6 +35,7 @@ pub enum JsVariableKind { Const, Let, Var, + Using, } impl JsVariableDeclaration { @@ -60,6 +61,7 @@ impl JsVariableDeclaration { T![const] => JsVariableKind::Const, T![let] => JsVariableKind::Let, T![var] => JsVariableKind::Var, + T![using] => JsVariableKind::Using, _ => unreachable!(), }) } @@ -88,6 +90,7 @@ impl JsForVariableDeclaration { T![const] => JsVariableKind::Const, T![let] => JsVariableKind::Let, T![var] => JsVariableKind::Var, + T![using] => JsVariableKind::Using, _ => unreachable!(), }) } @@ -98,21 +101,6 @@ declare_node_union! { } impl AnyJsVariableDeclaration { - /// Whether the declaration is a const declaration - pub fn is_const(&self) -> bool { - self.variable_kind() == Ok(JsVariableKind::Const) - } - - /// Whether the declaration is a let declaration - pub fn is_let(&self) -> bool { - self.variable_kind() == Ok(JsVariableKind::Let) - } - - /// Whether the declaration is a var declaration - pub fn is_var(&self) -> bool { - self.variable_kind() == Ok(JsVariableKind::Var) - } - pub fn variable_kind(&self) -> SyntaxResult { match self { AnyJsVariableDeclaration::JsForVariableDeclaration(decl) => decl.variable_kind(),