Skip to content

Commit

Permalink
fix(lint/useExhaustiveDependencies): ignore optional chaining (#666)
Browse files Browse the repository at this point in the history
Co-authored-by: Emanuele Stoppa <[email protected]>
Co-authored-by: Superchupu <[email protected]>
Co-authored-by: Nicolas Hedger <[email protected]>
  • Loading branch information
4 people authored Nov 6, 2023
1 parent 1a457c7 commit b0cdf48
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 10 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,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 @@ -91,8 +91,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

0 comments on commit b0cdf48

Please sign in to comment.