+
Skip to content
This repository was archived by the owner on Aug 31, 2023. It is now read-only.

feat(rome_js_analyze): update linter logic for using declaration #4759

Merged
merged 1 commit into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ declare_rule! {
impl Rule for UseSingleVarDeclarator {
type Query = Ast<JsVariableStatement>;
type State = (
Option<JsSyntaxToken>,
JsSyntaxToken,
JsVariableDeclaratorList,
Option<JsSyntaxToken>,
Expand All @@ -53,7 +54,7 @@ impl Rule for UseSingleVarDeclarator {
} = node.as_fields();

let JsVariableDeclarationFields {
await_token: _,
await_token,
kind,
declarators,
} = declaration.ok()?.as_fields();
Expand All @@ -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<Self>, _state: &Self::State) -> Option<RuleDiagnostic> {
Expand All @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,7 @@ enum Named {
LocalConst,
LocalLet,
LocalVar,
LocalUsing,
Namespace,
ObjectGetter,
ObjectMethod,
Expand Down Expand Up @@ -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,
})
}

Expand Down Expand Up @@ -881,6 +884,7 @@ impl Named {
| Named::LocalConst
| Named::LocalLet
| Named::LocalVar
| Named::LocalUsing
| Named::ObjectGetter
| Named::ObjectMethod
| Named::ObjectProperty
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,16 @@ impl FormatNodeRule<JsForVariableDeclaration> 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()
])]
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,16 @@ impl FormatNodeRule<JsVariableDeclaration> 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()
])]
]
}
}
18 changes: 3 additions & 15 deletions crates/rome_js_syntax/src/stmt_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub enum JsVariableKind {
Const,
Let,
Var,
Using,
}

impl JsVariableDeclaration {
Expand All @@ -60,6 +61,7 @@ impl JsVariableDeclaration {
T![const] => JsVariableKind::Const,
T![let] => JsVariableKind::Let,
T![var] => JsVariableKind::Var,
T![using] => JsVariableKind::Using,
_ => unreachable!(),
})
}
Expand Down Expand Up @@ -88,6 +90,7 @@ impl JsForVariableDeclaration {
T![const] => JsVariableKind::Const,
T![let] => JsVariableKind::Let,
T![var] => JsVariableKind::Var,
T![using] => JsVariableKind::Using,
_ => unreachable!(),
})
}
Expand All @@ -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<JsVariableKind> {
match self {
AnyJsVariableDeclaration::JsForVariableDeclaration(decl) => decl.variable_kind(),
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载