Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(useHookAtTopLevel): avoid diagnostics on non react projects #3826

Closed
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b

#### Bug fixes

- Fix `useHookAtTopLevel` to avoid reporting diagnostics in non React projects [#3476](https://github.com/biomejs/biome/issues/3476). Contributed by @Javimtib92

- `biome lint --write` now takes `--only` and `--skip` into account ([#3470](https://github.com/biomejs/biome/issues/3470)). Contributed by @Conaclos

- Fix [#3368](https://github.com/biomejs/biome/issues/3368), now the reporter `github` tracks the diagnostics that belong to formatting and organize imports. Contributed by @ematipico
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::react::hooks::{is_react_component, is_react_hook, is_react_hook_call};
use crate::services::manifest::ManifestServices;
use crate::services::semantic::{SemanticModelBuilderVisitor, SemanticServices};
use biome_analyze::RuleSource;
use biome_analyze::{
Expand All @@ -19,6 +20,7 @@ use biome_js_syntax::{
JsObjectBindingPatternShorthandProperty, JsReturnStatement, JsSyntaxKind, JsSyntaxNode,
JsTryFinallyStatement, TextRange,
};
use biome_project::PackageJson;
use biome_rowan::{declare_node_union, AstNode, Language, SyntaxNode, WalkEvent};
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -231,6 +233,13 @@ fn is_nested_function_inside_component_or_hook(function: &AnyJsFunctionOrMethod)
})
}

fn is_within_react_project(manifest: &Option<PackageJson>) -> bool {
manifest.as_ref().map_or(true, |package_json| {
package_json.dependencies.contains("react")
|| package_json.dev_dependencies.contains("react")
})
}

/// Model for tracking which function calls are preceeded by an early return.
///
/// The keys in the model are call sites and each value is the text range of an
Expand Down Expand Up @@ -330,6 +339,7 @@ impl Visitor for FunctionCallVisitor {
pub struct FunctionCallServices {
early_returns: EarlyReturnsModel,
semantic_services: SemanticServices,
manifest_services: ManifestServices,
}

impl FunctionCallServices {
Expand All @@ -340,6 +350,10 @@ impl FunctionCallServices {
fn semantic_model(&self) -> &SemanticModel {
self.semantic_services.model()
}

fn manifest(&self) -> &Option<PackageJson> {
self.manifest_services.manifest.as_ref()
}
}

impl FromServices for FunctionCallServices {
Expand All @@ -353,6 +367,7 @@ impl FromServices for FunctionCallServices {
Ok(Self {
early_returns: early_returns.clone(),
semantic_services: SemanticServices::from_services(rule_key, services)?,
manifest_services: ManifestServices::from_services(rule_key, services)?,
})
}
}
Expand Down Expand Up @@ -411,8 +426,14 @@ impl Rule for UseHookAtTopLevel {
Err(_) => None,
};

// Early return for any function call that's not a hook call:
if !is_react_hook_call(call) {
// Since we can't definitively know if a function is a React hook, we use the following heuristics:
//
// - Check if the function is within the scope of a react project by checking package.json
// - Verify if the call matches the pattern of a React hook
// If any of these checks fail, we assume it's not a React hook call and we return early:
let manifest = ctx.manifest();

if !is_within_react_project(manifest) || !is_react_hook_call(call) {
return None;
}

Expand Down
Loading