Skip to content
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 @@ -5,6 +5,7 @@
},
"rules": {
"react/exhaustive-deps": "warn",
"react/rules-of-hooks": "error",
"typescript/no-unsafe-declaration-merging": "error"
}
}
12 changes: 12 additions & 0 deletions apps/oxlint/fixtures/disable_directive_issue_13311/test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,15 @@ function Component() {
return null;
}

// Test case for issue #14233 - disable directive not working for rules-of-hooks
function useMostRelevantBreakdownType(params, filters) {
// Helper function that starts with "use" but isn't a React hook
console.log(params, filters);
}

const cleanBreakdownParams = (cleanedParams, filters) => {
// this isn't a react hook
// oxlint-disable-next-line react-hooks/rules-of-hooks
useMostRelevantBreakdownType(cleanedParams, filters);
}

15 changes: 13 additions & 2 deletions crates/oxc_linter/src/rules/react/rules_of_hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use oxc_cfg::{
};
use oxc_macros::declare_oxc_lint;
use oxc_semantic::{AstNodes, NodeId};
use oxc_span::GetSpan;
use oxc_syntax::operator::AssignmentOperator;

use crate::{
Expand All @@ -24,14 +25,22 @@ mod diagnostics {
use oxc_span::Span;
const SCOPE: &str = "eslint-plugin-react-hooks";

pub(super) fn function_error(span: Span, hook_name: &str, func_name: &str) -> OxcDiagnostic {
pub(super) fn function_error(
react_hook_span: Span,
outer_function_span: Span,
hook_name: &str,
func_name: &str,
) -> OxcDiagnostic {
OxcDiagnostic::warn(format!(
"React Hook {hook_name:?} is called in function {func_name:?} that is neither \
a React function component nor a custom React Hook function. \
React component names must start with an uppercase letter. \
React Hook names must start with the word \"use\".",
))
.with_label(span)
.with_labels(vec![
react_hook_span.primary_label("Hook is called here"),
outer_function_span.label("Outer function"),
])
.with_error_code_scope(SCOPE)
}

Expand Down Expand Up @@ -202,6 +211,7 @@ impl Rule for RulesOfHooks {
if !is_react_component_or_hook_name(&id.name) =>
{
return ctx.diagnostic(diagnostics::function_error(
call.callee.span(),
id.span,
hook_name,
id.name.as_str(),
Expand Down Expand Up @@ -247,6 +257,7 @@ impl Rule for RulesOfHooks {
// }
if ident.is_some_and(|name| !is_react_component_or_hook_name(&name)) {
return ctx.diagnostic(diagnostics::function_error(
call.callee.span(),
*span,
hook_name,
"Anonymous",
Expand Down
Loading
Loading