Skip to content

Commit

Permalink
fix: clippy errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvxa committed May 12, 2024
1 parent 6dde3fb commit 33633ba
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions crates/oxc_linter/src/rules/react/rules_of_hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use oxc_semantic::{
pg::neighbors_filtered_by_edge_weight,
AstNodeId, AstNodes, BasicBlockElement, EdgeType, Register,
};
use oxc_span::{Atom, CompactStr, Span};
use oxc_span::Atom;

use crate::{
context::LintContext,
Expand All @@ -19,10 +19,10 @@ use crate::{
};

mod diagnostics {
use super::*;
use oxc_diagnostics::OxcDiagnostic as D;
use oxc_span::Span;

pub(super) fn function_error(span: Span, hook_name: CompactStr, func_name: CompactStr) -> D {
pub(super) fn function_error(span: Span, hook_name: &str, func_name: &str) -> D {
D::error(format!(
"eslint-plugin-react-hooks(rules-of-hooks): \
React Hook {hook_name:?} is called in function {func_name:?} that is neither \
Expand All @@ -33,7 +33,7 @@ mod diagnostics {
.with_label(span)
}

pub(super) fn conditional_hook(span: Span, hook_name: CompactStr) -> D {
pub(super) fn conditional_hook(span: Span, hook_name: &str) -> D {
D::error(format!(
"eslint-plugin-react-hooks(rules-of-hooks): \
React Hook {hook_name:?} is called conditionally. React Hooks must be \
Expand All @@ -42,7 +42,7 @@ mod diagnostics {
.with_label(span)
}

pub(super) fn look_hook(span: Span, hook_name: CompactStr) -> D {
pub(super) fn look_hook(span: Span, hook_name: &str) -> D {
D::error(format!(
"eslint-plugin-react-hooks(rules-of-hooks): \
React Hook {hook_name:?} may be executed more than once. Possibly \
Expand All @@ -52,7 +52,7 @@ mod diagnostics {
.with_label(span)
}

pub(super) fn top_level_hook(span: Span, hook_name: CompactStr) -> D {
pub(super) fn top_level_hook(span: Span, hook_name: &str) -> D {
D::error(format!(
"eslint-plugin-react-hooks(rules-of-hooks): \
React Hook {hook_name:?} cannot be called at the top level. React Hooks \
Expand All @@ -62,15 +62,15 @@ mod diagnostics {
.with_label(span)
}

pub(super) fn async_component(span: Span, func_name: CompactStr) -> D {
pub(super) fn async_component(span: Span, func_name: &str) -> D {
D::error(format!(
"eslint-plugin-react-hooks(rules-of-hooks): \
message: `React Hook {func_name:?} cannot be called in an async function. "
))
.with_label(span)
}

pub(super) fn class_component(span: Span, hook_name: CompactStr) -> D {
pub(super) fn class_component(span: Span, hook_name: &str) -> D {
D::error(format!(
"eslint-plugin-react-hooks(rules-of-hooks): \
React Hook {hook_name:?} cannot be called in a class component. React Hooks \
Expand All @@ -80,7 +80,7 @@ mod diagnostics {
.with_label(span)
}

pub(super) fn generic_error(span: Span, hook_name: CompactStr) -> D {
pub(super) fn generic_error(span: Span, hook_name: &str) -> D {
D::error(format!(
"eslint-plugin-react-hooks(rules-of-hooks): \
React Hook {hook_name:?} cannot be called inside a callback. React Hooks \
Expand Down Expand Up @@ -113,9 +113,8 @@ impl Rule for RulesOfHooks {
return;
}
let span = call.span;
let hook_name = CompactStr::from(
call.callee_name().expect("We identify hooks using their names so it should be named."),
);
let hook_name =
call.callee_name().expect("We identify hooks using their names so it should be named.");

let semantic = ctx.semantic();
let nodes = semantic.nodes();
Expand Down Expand Up @@ -146,21 +145,20 @@ impl Rule for RulesOfHooks {
return ctx.diagnostic(diagnostics::function_error(
id.span,
hook_name,
id.name.to_compact_str(),
id.name.as_str(),
));
}
// Hooks can't be called from async function.
AstKind::Function(Function { id: Some(id), r#async: true, .. }) => {
return ctx
.diagnostic(diagnostics::async_component(id.span, id.name.to_compact_str()));
return ctx.diagnostic(diagnostics::async_component(id.span, id.name.as_str()));
}
// Hooks can't be called from async arrow function.
AstKind::ArrowFunctionExpression(ArrowFunctionExpression {
span,
r#async: true,
..
}) => {
return ctx.diagnostic(diagnostics::async_component(*span, "Anonymous".into()));
return ctx.diagnostic(diagnostics::async_component(*span, "Anonymous"));
}
// Hooks are allowed inside of unnamed functions used as arguments. As long as they are
// not used as a callback inside of components or hooks.
Expand Down Expand Up @@ -203,7 +201,7 @@ impl Rule for RulesOfHooks {
return ctx.diagnostic(diagnostics::function_error(
*span,
hook_name,
"Anonymous".into(),
"Anonymous",
));
}
}
Expand Down

0 comments on commit 33633ba

Please sign in to comment.