From 883cca57c64829d2fbeabfd8159a3e106d8c59e2 Mon Sep 17 00:00:00 2001 From: Carson McManus Date: Mon, 18 Nov 2024 09:40:49 -0500 Subject: [PATCH] fix(analyze/js): fix some imports not being detected --- CHANGELOG.md | 2 + .../use_exhaustive_dependencies.rs | 8 +--- .../useExhaustiveDependencies/issue4568.js | 6 +++ .../issue4568.js.snap | 37 +++++++++++++++++++ .../src/semantic_model/import.rs | 10 ++++- 5 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 crates/biome_js_analyze/tests/specs/correctness/useExhaustiveDependencies/issue4568.js create mode 100644 crates/biome_js_analyze/tests/specs/correctness/useExhaustiveDependencies/issue4568.js.snap diff --git a/CHANGELOG.md b/CHANGELOG.md index 1fa203aed334..e80a0557909d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -255,6 +255,8 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b Contributed by @Conaclos +- Fixed a panic related to bogus import statements in `useExhaustiveDependencies` ([#4568](https://github.com/biomejs/biome/issues/4568)) Contributed by @dyc3 + ### Parser #### Bug fixes diff --git a/crates/biome_js_analyze/src/lint/correctness/use_exhaustive_dependencies.rs b/crates/biome_js_analyze/src/lint/correctness/use_exhaustive_dependencies.rs index 15da2a2ddf61..4e89988ad457 100644 --- a/crates/biome_js_analyze/src/lint/correctness/use_exhaustive_dependencies.rs +++ b/crates/biome_js_analyze/src/lint/correctness/use_exhaustive_dependencies.rs @@ -597,18 +597,14 @@ fn capture_needs_to_be_in_the_dependency_list( | AnyJsBindingDeclaration::JsArrayBindingPatternRestElement(_) | AnyJsBindingDeclaration::JsObjectBindingPatternProperty(_) | AnyJsBindingDeclaration::JsObjectBindingPatternRest(_) - | AnyJsBindingDeclaration::JsObjectBindingPatternShorthandProperty(_) => { - unreachable!("The declaration should be resolved to its prent declaration") - } + | AnyJsBindingDeclaration::JsObjectBindingPatternShorthandProperty(_) => true, // This should be unreachable because of the test if the capture is imported AnyJsBindingDeclaration::JsShorthandNamedImportSpecifier(_) | AnyJsBindingDeclaration::JsNamedImportSpecifier(_) | AnyJsBindingDeclaration::JsBogusNamedImportSpecifier(_) | AnyJsBindingDeclaration::JsDefaultImportSpecifier(_) - | AnyJsBindingDeclaration::JsNamespaceImportSpecifier(_) => { - unreachable!() - } + | AnyJsBindingDeclaration::JsNamespaceImportSpecifier(_) => false, } } diff --git a/crates/biome_js_analyze/tests/specs/correctness/useExhaustiveDependencies/issue4568.js b/crates/biome_js_analyze/tests/specs/correctness/useExhaustiveDependencies/issue4568.js new file mode 100644 index 000000000000..dc6a145397ae --- /dev/null +++ b/crates/biome_js_analyze/tests/specs/correctness/useExhaustiveDependencies/issue4568.js @@ -0,0 +1,6 @@ +import type {Point} fom '.geomet'; +import {useEffect, seRef} 'react'; + +xpor function ueCanvasn( +) { + useEffect( => {on late Point},[c diff --git a/crates/biome_js_analyze/tests/specs/correctness/useExhaustiveDependencies/issue4568.js.snap b/crates/biome_js_analyze/tests/specs/correctness/useExhaustiveDependencies/issue4568.js.snap new file mode 100644 index 000000000000..b6c48933c524 --- /dev/null +++ b/crates/biome_js_analyze/tests/specs/correctness/useExhaustiveDependencies/issue4568.js.snap @@ -0,0 +1,37 @@ +--- +source: crates/biome_js_analyze/tests/spec_tests.rs +expression: issue4568.js +--- +# Input +```jsx +import type {Point} fom '.geomet'; +import {useEffect, seRef} 'react'; + +xpor function ueCanvasn( +) { + useEffect( => {on late Point},[c + +``` + +# Diagnostics +``` +issue4568.js:6:3 lint/correctness/useExhaustiveDependencies ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + ! This hook specifies more dependencies than necessary: c + + 4 │ xpor function ueCanvasn( + 5 │ ) { + > 6 │ useEffect( => {on late Point},[c + │ ^^^^^^^^^ + 7 │ + + i This dependency can be removed from the list. + + 4 │ xpor function ueCanvasn( + 5 │ ) { + > 6 │ useEffect( => {on late Point},[c + │ ^ + 7 │ + + +``` diff --git a/crates/biome_js_semantic/src/semantic_model/import.rs b/crates/biome_js_semantic/src/semantic_model/import.rs index 81bb95a83487..2ad413a94d8d 100644 --- a/crates/biome_js_semantic/src/semantic_model/import.rs +++ b/crates/biome_js_semantic/src/semantic_model/import.rs @@ -6,8 +6,14 @@ use biome_js_syntax::{ use biome_rowan::AstNode; pub(crate) fn is_imported(node: &JsSyntaxNode) -> bool { - node.ancestors() - .any(|x| matches!(x.kind(), JsSyntaxKind::JS_IMPORT)) + node.ancestors().any(|x| { + matches!( + x.kind(), + JsSyntaxKind::JS_IMPORT + | JsSyntaxKind::JS_NAMED_IMPORT_SPECIFIERS + | JsSyntaxKind::JS_DEFAULT_IMPORT_SPECIFIER + ) + }) } /// Marker trait that groups all "AstNode" that can be imported or