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

feat(lint): implement duplicate dependency error #2991

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

Contributed by @lutaok

- Make [useExhaustiveDependencies](https://biomejs.dev/linter/rules/use-exhaustive-dependencies/) report duplicate dependencies. Contributed by @tunamaguro

#### Bug fixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,23 @@ impl Rule for UseExhaustiveDependencies {
})
});

// Find duplicated deps from specified ones
{
let mut dep_list: BTreeMap<String, AnyJsExpression> = BTreeMap::new();
for dep in correct_deps.iter() {
let expression_name = dep.to_string();
if dep_list.contains_key(&expression_name) {
signals.push(Fix::RemoveDependency {
function_name_range: result.function_name_range,
component_function: component_function.clone(),
dependencies: vec![dep.clone()],
});
continue;
}
dep_list.insert(expression_name, dep.clone());
}
}

// Find correctly specified dependencies with an unstable identity,
// since they would trigger re-evaluation on every render.
let unstable_deps = correct_deps.into_iter().filter_map(|dep| {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useCallback } from "react";

function Component1({ a }) {
const handle = useCallback(() => {
console.log(a);
}, [a, a]);
}

function Component2() {
const [local,SetLocal] = useState(0);
const handle = useCallback(() => {
console.log(local);
}, [local, local]);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: duplicateDependencies.js
---
# Input
```jsx
import { useCallback } from "react";

function Component1({ a }) {
const handle = useCallback(() => {
console.log(a);
}, [a, a]);
}

function Component2() {
const [local,SetLocal] = useState(0);
const handle = useCallback(() => {
console.log(local);
}, [local, local]);
}
```

# Diagnostics
```
duplicateDependencies.js:4:20 lint/correctness/useExhaustiveDependencies ━━━━━━━━━━━━━━━━━━━━━━━━━━━

! This hook specifies more dependencies than necessary: a

3 │ function Component1({ a }) {
> 4 │ const handle = useCallback(() => {
│ ^^^^^^^^^^^
5 │ console.log(a);
6 │ }, [a, a]);

i Outer scope values aren't valid dependencies because mutating them doesn't re-render the component.

4 │ const handle = useCallback(() => {
5 │ console.log(a);
> 6 │ }, [a, a]);
│ ^
7 │ }
8 │


```

```
duplicateDependencies.js:11:20 lint/correctness/useExhaustiveDependencies ━━━━━━━━━━━━━━━━━━━━━━━━━━

! This hook specifies more dependencies than necessary: local

9 │ function Component2() {
10 │ const [local,SetLocal] = useState(0);
> 11 │ const handle = useCallback(() => {
│ ^^^^^^^^^^^
12 │ console.log(local);
13 │ }, [local, local]);

i This dependency can be removed from the list.

11 │ const handle = useCallback(() => {
12 │ console.log(local);
> 13 │ }, [local, local]);
│ ^^^^^
14 │ }


```