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(lint/useExhaustiveDependencies): ignore optional chaining #666

Merged
merged 11 commits into from
Nov 6, 2023
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom

- Fix [#591](https://github.com/biomejs/biome/issues/591) which made [noRedeclare](https://biomejs.dev/linter/rules/no-redeclare) report type parameters with identical names but in different method signatures. Contributed by @Conaclos
- Support more a11y roles and fix some methods for a11y lint rules Contributed @nissy-dev
- Fix `useExhaustiveDependencies`, by removing `useContext`, `useId` and `useSyncExternalStore` from the known hooks. Contributed by @msdlisper
-
- Fix [#609](https://github.com/biomejs/biome/issues/609) `useExhaustiveDependencies`, by removing `useContext`, `useId` and `useSyncExternalStore` from the known hooks. Contributed by @msdlisper
- Fix [#607](https://github.com/biomejs/biome/issues/609) `useExhaustiveDependencies`, ignore optional chaining, Contributed by @msdlisper

### Parser

#### Enhancements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,14 @@ impl Rule for UseExhaustiveDependencies {
let mut suggested_fix = None;
let mut is_captured_covered = false;
for (dependency_text, dependency_range) in deps.iter() {
let capture_deeper_than_dependency = capture_text.starts_with(dependency_text);
let dependency_deeper_than_capture = dependency_text.starts_with(capture_text);
// capture_text and dependency_text should filter the "?" inside
// in order to ignore optional chaining
let filter_capture_text = capture_text.replace('?', "");
let filter_dependency_text = dependency_text.replace('?', "");
let capture_deeper_than_dependency =
filter_capture_text.starts_with(&filter_dependency_text);
let dependency_deeper_than_capture =
filter_dependency_text.starts_with(&filter_capture_text);
match (
capture_deeper_than_dependency,
dependency_deeper_than_capture,
Expand Down Expand Up @@ -586,8 +592,14 @@ impl Rule for UseExhaustiveDependencies {
for (dependency_text, dep_range) in deps {
let mut covers_any_capture = false;
for (capture_text, _, _) in captures.iter() {
let capture_deeper_dependency = capture_text.starts_with(&dependency_text);
let dependency_deeper_capture = dependency_text.starts_with(capture_text);
// capture_text and dependency_text should filter the "?" inside
// in order to ignore optional chaining
let filter_capture_text = capture_text.replace('?', "");
let filter_dependency_text = dependency_text.replace('?', "");
let capture_deeper_dependency =
filter_capture_text.starts_with(&filter_dependency_text);
let dependency_deeper_capture =
filter_dependency_text.starts_with(&filter_capture_text);
if capture_deeper_dependency || dependency_deeper_capture {
covers_any_capture = true;
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* should not generate diagnostics */

import React from "react";
import { useEffect, useSyncExternalStore } from "react";
import { useEffect, useSyncExternalStore, useMemo } from "react";
import doSomething from 'a';

// No captures
Expand Down Expand Up @@ -176,3 +176,12 @@ function MyComponent17() {
console.log(data);
}, [data]);
}

// https://github.com/biomejs/biome/issues/607
function MyComponent18() {
const obj = Math.random() > 0.5 ? { a: 1, b: 2 } : undefined;

return useMemo(() => {
return obj?.a === 1 && obj.b === 2;
}, [obj?.a, obj?.b]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ expression: valid.js
/* should not generate diagnostics */

import React from "react";
import { useEffect, useSyncExternalStore } from "react";
import { useEffect, useSyncExternalStore, useMemo } from "react";
import doSomething from 'a';

// No captures
Expand Down Expand Up @@ -184,6 +184,15 @@ function MyComponent17() {
}, [data]);
}

// https://github.com/biomejs/biome/issues/607
function MyComponent18() {
const obj = Math.random() > 0.5 ? { a: 1, b: 2 } : undefined;

return useMemo(() => {
return obj?.a === 1 && obj.b === 2;
}, [obj?.a, obj?.b]);
}

```


5 changes: 3 additions & 2 deletions website/src/content/docs/internals/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom

- Fix [#591](https://github.com/biomejs/biome/issues/591) which made [noRedeclare](https://biomejs.dev/linter/rules/no-redeclare) report type parameters with identical names but in different method signatures. Contributed by @Conaclos
- Support more a11y roles and fix some methods for a11y lint rules Contributed @nissy-dev
- Fix `useExhaustiveDependencies`, by removing `useContext`, `useId` and `useSyncExternalStore` from the known hooks. Contributed by @msdlisper
-
- Fix [#609](https://github.com/biomejs/biome/issues/609) `useExhaustiveDependencies`, by removing `useContext`, `useId` and `useSyncExternalStore` from the known hooks. Contributed by @msdlisper
- Fix [#607](https://github.com/biomejs/biome/issues/609) `useExhaustiveDependencies`, ignore optional chaining, Contributed by @msdlisper

### Parser

#### Enhancements
Expand Down