Deprecated: Function get_magic_quotes_gpc() is deprecated in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 99

Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 619

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1169

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176
8000 fix(lint): improve `useHookAtTopLevel` lint by andogq · Pull Request #7749 · biomejs/biome · GitHub
Nothing Special   »   [go: up one dir, main page]

Skip to content

Conversation

andogq
Copy link
@andogq andogq commented Oct 14, 2025

Summary

This PR addresses a number of items discussed in #1984, in order to bring detection of invalid react hooks closer in line with eslint's rules-of-hooks. Specifically:

  • Module level hook usage now produces a lint.
  • Hook usage in any non-component/hook function now produces a lint. Previously, a lint would only be generated if that function was eventually used in a component. The exception to this is if the hook is called from within an anonymous function (JsArrowFunctionExpression or JsFunctionExpression).

These changes extend the current lint behaviour, so any previous behaviour is retained (ie. hook recursion and indirect hook checks are run before non-component/hook checks).

I've based my implementation form @arendjr's comment:

That means we keep the current rules inside components (hooks can only be called from the top-level of the component, calls inside any nested functions are forbidden), while applying the same heuristics as ESLint outside components (hooks can only be called from named functions that are components or hooks based on their naming conventions, while calls from anonymous functions are always allowed).

Based on this same comment I have created a minor changeset too.

I am open to any suggestions or ideas for how to better apply these lint changes.

Test Plan

I have added additional test cases validating these changes. I have also had to remove one of the previously valid test cases, as this lint will now reject it:

function helper() {
    useEffect();
}

function Component2({ a }) {
    helper();
}

Docs

I don't believe this is applicable, as the existing docs still seem fine.

Before performing hook call recursion checks, walk the call's ancestors
searching for `AnyJsFunctionOrMethod` nodes. If one is not encountered,
it is assumed the call is at the top of the file (or in a top-level
expression statement), and the new `SuggestionKind::TopLevel` lint is
generated.
Add additional check that the hook was called from either:

- Named function that is a component or hook

- An anonymous function (`JsArrowFunctionExpression` or
  `JsFunctionExpression`)

This is a check that occurs after any existing checks, so the previous
lints which trace indirect calls will still function as they previously
did.
Copy link
changeset-bot bot commented Oct 14, 2025

🦋 Changeset detected

Latest commit: 94c4f97

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 14 packages 8000
Name Type
@biomejs/biome Minor
@biomejs/cli-win32-x64 Minor
@biomejs/cli-win32-arm64 Minor
@biomejs/cli-darwin-x64 Minor
@biomejs/cli-darwin-arm64 Minor
@biomejs/cli-linux-x64 Minor
@biomejs/cli-linux-arm64 Minor
@biomejs/cli-linux-x64-musl Minor
@biomejs/cli-linux-arm64-musl Minor
@biomejs/wasm-web Minor
@biomejs/wasm-bundler Minor
@biomejs/wasm-nodejs Minor
@biomejs/backend-jsonrpc Patch
@biomejs/js-api Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added A-Linter Area: linter L-JavaScript Language: JavaScript and super languages labels Oct 14, 2025
@andogq andogq changed the title fix(lint): Improve useHookAtTopLevel lint fix(lint): improve useHookAtTopLevel lint Oct 14, 2025
Copy link
Contributor
coderabbitai bot commented Oct 14, 2025

Walkthrough

Updates a changeset to bump the @biomejs/biome minor and documents enhancements to the useHookAtTopLevel lint. The analyzer now detects hook calls at module/top-level, adds a ComponentOrHook scenario, introduces an internal is_function_expression helper and is_top_level_call detection, and extends the SuggestionKind enum with TopLevel, ComponentOrHook and Regular. Diagnostic messages and notes were adjusted and tests were expanded with a new hookLocations.js and edits to valid.js.

Possibly related PRs

Suggested labels

A-Diagnostic

Suggested reviewers

  • ematipico
  • dyc3

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Title Check ✅ Passed The title uses a conventional commit prefix and succinctly describes the primary change of improving the useHookAtTopLevel lint rule, making it immediately clear what the pull request addresses.
Description Check ✅ Passed The description clearly explains the motivation, references the related issue, outlines implementation details, lists test updates and addresses documentation impact, all of which directly relate to the proposed changes.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 786b693 and 94c4f97.

📒 Files selected for processing (1)
  • .changeset/huge-cycles-dance.md (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • .changeset/huge-cycles-dance.md

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor
@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (6)
crates/biome_js_analyze/tests/specs/correctness/useHookAtTopLevel/hookLocations.js (2)

24-31: Add a case for named function expressions to lock intent.

You’re allowing JsFunctionExpression wholesale. Add:

  • test("named fn expr", function Named() { useHook(); }); // expected: allowed by current implementation.

This guards the “anonymous” = expression-node heuristic.


14-17: Also test object/class methods that aren’t hooks/components.

Please add invalid cases like:

  • const o = { notHook() { useHook(); } };
  • class C { notHook() { useHook(); } }

These should produce the new “ComponentOrHook” diagnostic.

crates/biome_js_analyze/src/lint/correctness/use_hook_at_top_level.rs (3)

258-268: Clarify “anonymous” semantics (or enforce it).

is_anonymous_function returns true for all JsFunctionExpression, including named function expressions. If you truly mean “anonymous only”, check the function expression’s identifier; otherwise, consider renaming for clarity.

Two options:

  • Keep behaviour; rename helper for clarity.
  • Enforce anonymity; add an id() check.

Suggested rename (if keeping behaviour):

- fn is_anonymous_function(function: &AnyJsFunctionOrMethod) -> bool {
+ fn is_function_expression_or_arrow(function: &AnyJsFunctionOrMethod) -> bool {

And use site:

- !function.is_react_component_or_hook() && !is_anonymous_function(function)
+ !function.is_react_component_or_hook() && !is_function_expression_or_arrow(function)

Also applies to: 554-566


583-589: Diagnostics: small polish.

  • Consider mentioning “function or method” in ComponentOrHook message to reflect class/object methods.
  • Update the docs link to the current React site.

Suggested tweaks:

- "This hook is being called from within a function that is not a hook or component."
+ "This hook is being called from within a function or method that is not a hook or component."
- "See https://reactjs.org/docs/hooks-rules.html#only-call-hooks-at-the-top-level"
+ "See https://react.dev/reference/rules/rules-of-hooks#only-call-hooks-at-the-top-level"

Also applies to: 618-626, 635-637


28-74: Rule docs: add examples for new cases.

Consider adding minimal examples for:

  • Invalid: module‑level hook.
  • Invalid: hook in a named non‑component/hook function.
  • Valid: hook in an arrow/function expression callback.

Keeps rustdoc aligned with behaviour.

.changeset/huge-cycles-dance.md (1)

5-5: Optional: start with the issue reference.

Guidelines prefer bug‑fix changesets to start with “Fixed [#…] …”. Consider:

“Fixed #1984. Updated useHookAtTopLevel to better catch invalid hook usage.”

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6fcbc07 and 04f7c37.

⛔ Files ignored due to path filters (2)
  • crates/biome_js_analyze/tests/specs/correctness/useHookAtTopLevel/hookLocations.js.snap is excluded by !**/*.snap and included by **
  • crates/biome_js_analyze/tests/specs/correctness/useHookAtTopLevel/valid.js.snap is excluded by !**/*.snap and included by **
📒 Files selected for processing (4)
  • .changeset/huge-cycles-dance.md (1 hunks)
  • crates/biome_js_analyze/src/lint/correctness/use_hook_at_top_level.rs (8 hunks)
  • crates/biome_js_analyze/tests/specs/correctness/useHookAtTopLevel/hookLocations.js (1 hunks)
  • crates/biome_js_analyze/tests/specs/correctness/useHookAtTopLevel/valid.js (1 hunks)
🧰 Additional context used
📓 Path-based instructions (6)
.changeset/**/*.md

📄 CodeRabbit inference engine (CONTRIBUTING.md)

.changeset/**/*.md: Create changesets using the just new-changeset command; do not author them manually
In changeset markdown, only use headers #### or #####
Changeset descriptions must end every sentence with a full stop (.)
For bug fixes, start the changeset description with a linked issue reference like “Fixed #1234
Prefer past tense for what was done and present tense for current behavior in changesets

Files:

  • .changeset/huge-cycles-dance.md
crates/biome_*_{syntax,parser,formatter,analyze,factory,semantic}/**

📄 CodeRabbit inference engine (CLAUDE.md)

Maintain the per-language crate structure: biome_{lang}_{syntax,parser,formatter,analyze,factory,semantic}

Files:

  • crates/biome_js_analyze/tests/specs/correctness/useHookAtTopLevel/hookLocations.js
  • crates/biome_js_analyze/tests/specs/correctness/useHookAtTopLevel/valid.js
  • crates/biome_js_analyze/src/lint/correctness/use_hook_at_top_level.rs
crates/biome_*/**

📄 CodeRabbit inference engine (CLAUDE.md)

Place core crates under /crates/biome_*/

Files:

  • crates/biome_js_analyze/tests/specs/correctness/useHookAtTopLevel/hookLocations.js
  • crates/biome_js_analyze/tests/specs/correctness/useHookAtTopLevel/valid.js
  • crates/biome_js_analyze/src/lint/correctness/use_hook_at_top_level.rs
**/tests/**

📄 CodeRabbit inference engine (CLAUDE.md)

Place test files under a tests/ directory in each crate

Files:

  • crates/biome_js_analyze/tests/specs/correctness/useHookAtTopLevel/hookLocations.js
  • crates/biome_js_analyze/tests/specs/correctness/useHookAtTopLevel/valid.js
**/*.{rs,toml}

📄 CodeRabbit inference engine (CONTRIBUTING.md)

Before committing, format Rust and TOML files (e.g., via just f/just format)

Files:

  • crates/biome_js_analyze/src/lint/correctness/use_hook_at_top_level.rs
**/*.rs

📄 CodeRabbit inference engine (CONTRIBUTING.md)

Document rules, assists, and options via inline rustdoc in Rust source

Files:

  • crates/biome_js_analyze/src/lint/correctness/use_hook_at_top_level.rs
🧬 Code graph analysis (2)
crates/biome_js_analyze/tests/specs/correctness/useHookAtTopLevel/hookLocations.js (1)
crates/biome_js_analyze/tests/specs/correctness/useHookAtTopLevel/valid.js (1)
  • renderHook (116-116)
crates/biome_js_analyze/src/lint/correctness/use_hook_at_top_level.rs (1)
crates/biome_rowan/src/ast/mod.rs (2)
  • try_cast (181-187)
  • cast_ref (142-151)
🔇 Additional comments (3)
crates/biome_js_analyze/tests/specs/correctness/useHookAtTopLevel/valid.js (1)

68-96: Renames look good; coverage unchanged.

The component examples still exercise top‑level usage correctly. No action.

Also applies to: 80-89

crates/biome_js_analyze/src/lint/correctness/use_hook_at_top_level.rs (2)

251-256: Top‑level detection is correct and cheap.

Using ancestor scan against AnyJsFunctionOrMethod is a clear way to catch module‑scope calls. Nice.


469-475: Early return on module‑level hooks preserves existing flow.

Short‑circuiting TopLevel before call‑graph traversal avoids redundant work and duplicate diagnostics. Good call.

Copy link
codspeed-hq bot commented Oct 14, 2025

CodSpeed Performance Report

Merging #7749 will not alter performance

Comparing andogq:fix/use-hook-at-top-level-improvements (94c4f97) with next (d3aac63)

Summary

✅ 53 untouched
⏩ 85 skipped1

Footnotes

  1. 85 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Copy link
Contributor
@arendjr arendjr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also left a few minor comments, but otherwise looks good to me!

- add `is_function_expression` method to `AnyJsFunctionOrMethod`,
  implemented with `matches!` (replaces `is_anonymous_function`)

- use `can_cast(...)` in place of `try_cast(...).is_ok()`

- use `is_some_and(...)` in place of `filter(...).is_some()`

- provide additional hook examples in documentation

- add test cases for named function expressions, class methods, and
  methods on objects

- alter lint wording to to include `function or method`, and update hint
  URL to new `react.dev` docs
@andogq andogq force-pushed the fix/use-hook-at-top-level-improvements branch from 04f7c37 to 786b693 Compare October 15, 2025 01:16
Copy link
Contributor
@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 04f7c37 and 786b693.

⛔ Files ignored due to path filters (7)
  • crates/biome_js_analyze/tests/specs/correctness/useHookAtTopLevel/customHook.js.snap is excluded by !**/*.snap and included by **
  • crates/biome_js_analyze/tests/specs/correctness/useHookAtTopLevel/hookLocations.js.snap is excluded by !**/*.snap and included by **
  • crates/biome_js_analyze/tests/specs/correctness/useHookAtTopLevel/invalid.js.snap is excluded by !**/*.snap and included by **
  • crates/biome_js_analyze/tests/specs/correctness/useHookAtTopLevel/invalid.jsx.snap is excluded by !**/*.snap and included by **
  • crates/biome_js_analyze/tests/specs/correctness/useHookAtTopLevel/invalid.ts.snap is excluded by !**/*.snap and included by **
  • crates/biome_js_analyze/tests/specs/correctness/useHookAtTopLevel/invalidCompositeHook.js.snap is excluded by !**/*.snap and included by **
  • crates/biome_js_analyze/tests/specs/correctness/useHookAtTopLevel/invalidWrapped.js.snap is excluded by !**/*.snap and included by **
📒 Files selected for processing (3)
  • .changeset/huge-cycles-dance.md (1 hunks)
  • crates/biome_js_analyze/src/lint/correctness/use_hook_at_top_level.rs (10 hunks)
  • crates/biome_js_analyze/tests/specs/correctness/useHookAtTopLevel/hookLocations.js (1 hunks)
🧰 Additional context used
📓 Path-based instructions (6)
crates/biome_*_{syntax,parser,formatter,analyze,factory,semantic}/**

📄 CodeRabbit inference engine (CLAUDE.md)

Maintain the per-language crate structure: biome_{lang}_{syntax,parser,formatter,analyze,factory,semantic}

Files:

  • crates/biome_js_analyze/src/lint/correctness/use_hook_at_to 8000 p_level.rs
  • crates/biome_js_analyze/tests/specs/correctness/useHookAtTopLevel/hookLocations.js
crates/biome_*/**

📄 CodeRabbit inference engine (CLAUDE.md)

Place core crates under /crates/biome_*/

Files:

  • crates/biome_js_analyze/src/lint/correctness/use_hook_at_top_level.rs
  • crates/biome_js_analyze/tests/specs/correctness/useHookAtTopLevel/hookLocations.js
**/*.{rs,toml}

📄 CodeRabbit inference engine (CONTRIBUTING.md)

Before committing, format Rust and TOML files (e.g., via just f/just format)

Files:

  • crates/biome_js_analyze/src/lint/correctness/use_hook_at_top_level.rs
**/*.rs

📄 CodeRabbit inference engine (CONTRIBUTING.md)

Document rules, assists, and options via inline rustdoc in Rust source

Files:

  • crates/biome_js_analyze/src/lint/correctness/use_hook_at_top_level.rs
**/tests/**

📄 CodeRabbit inference engine (CLAUDE.md)

Place test files under a tests/ directory in each crate

Files:

  • crates/biome_js_analyze/tests/specs/correctness/useHookAtTopLevel/hookLocations.js
.changeset/**/*.md

📄 CodeRabbit inference engine (CONTRIBUTING.md)

.changeset/**/*.md: Create changesets using the just new-changeset command; do not author them manually
In changeset markdown, only use headers #### or #####
Changeset descriptions must end every sentence with a full stop (.)
For bug fixes, start the changeset description with a linked issue reference like “Fixed #1234
Prefer past tense for what was done and present tense for current behavior in changesets

Files:

  • .changeset/huge-cycles-dance.md
🧬 Code graph analysis (1)
crates/biome_js_analyze/tests/specs/correctness/useHookAtTopLevel/hookLocations.js (1)
crates/biome_js_analyze/tests/specs/correctness/useHookAtTopLevel/valid.js (1)
  • renderHook (116-116)
🪛 LanguageTool
.changeset/huge-cycles-dance.md

[style] ~9-~9: This phrase is redundant. Consider using “outside”.
Context: ...d at the module level (top of the file, outside of any function) - A hook is used within ...

(OUTSIDE_OF)

@andogq andogq force-pushed the fix/use-hook-at-top-level-improvements branch from 786b693 to 94c4f97 Compare October 15, 2025 01:21
@andogq andogq requested review from arendjr and ematipico October 15, 2025 01:26
@andogq
Copy link
Author
andogq commented Oct 15, 2025

While looking through the biome_js_syntax crate, I've just come across JsCallExpression::is_test_call_expression. Would there be any interest in me updating the function expression heuristics to only allow hook usage if the function expression is a parameter of a call expression that passes this check?

It does run the risk of false-negatives if a test call isn't correctly identified, however the lint's accuracy could improve as is_test_call_expression is refined, rather than being logic specific to this lint. Potentially this stricter check could be opt-in with a configuration option, although I'm aware hesitancy to configuration for this rule has previously been expressed.

@dyc3
Copy link
Contributor
dyc3 commented Oct 16, 2025

We don't publish crate updates regularly, so the documentation you are referencing is likely outdated regarding is_test_call_expression.

Would there be any interest in me updating the function expression heuristics to only allow hook usage if the function expression is a parameter of a call expression that passes this check?

This is a little hard to parse. Could you provide a code sample for what you are talking about?

@andogq
Copy link
Author
andogq commented Oct 16, 2025

Given the following code:

test("something", () => {
  useHook(); // Invalid as it should be called within `renderHook` (or similar) within a test.

  renderHook(() => useHook()); // Valid
});

In the current implementation, both hook uses are allowed as they exist within a function expression. I'm proposing the lint is updated to perform the following checks:

  1. Is the hook is called within a function expression?
// Yes:
() => useHook()

// No:
useHook()
  1. Does this function expression exist (somewhere) within another function expression?
// Yes:
() => {
  renderHook(() => useHook());
}
  1. Is this outer function expression a parameter to a function call?
someFunc(arg1, arg2, () => {
  renderHook(() => useHook());
});
  1. Does the function call pass the is_test_call_expression check?

It's certainly more involved than the current lint, and even as I'm typing it out I'm not 100% convinced it's the best move. It just crossed my mind, and I figured someone else may have a better approach/opinion on it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Linter Area: linter L-JavaScript Language: JavaScript and super languages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

0