diff --git a/crates/biome_js_analyze/src/lint/correctness/no_unused_imports.rs b/crates/biome_js_analyze/src/lint/correctness/no_unused_imports.rs index d5583b15ce35..c55c1e421b3a 100644 --- a/crates/biome_js_analyze/src/lint/correctness/no_unused_imports.rs +++ b/crates/biome_js_analyze/src/lint/correctness/no_unused_imports.rs @@ -126,7 +126,7 @@ impl DerefMut for JsDocTypeModel { } #[derive(Default)] -struct JsDocTypeCollectorVisitior { +struct JsDocTypeCollectorVisitor { jsdoc_types: JsDocTypeModel, } @@ -134,7 +134,7 @@ declare_node_union! { pub AnyJsWithTypeReferencingJsDoc = AnyJsDeclaration | AnyJsClassMember | AnyTsTypeMember | TsEnumMember | JsExport | JsStaticMemberAssignment } -impl Visitor for JsDocTypeCollectorVisitior { +impl Visitor for JsDocTypeCollectorVisitor { type Language = JsLanguage; fn visit( &mut self, @@ -247,7 +247,7 @@ impl Queryable for NoUnusedImportsQuery { root: &::Root, ) { analyzer.add_visitor(Phases::Syntax, || SemanticModelBuilderVisitor::new(root)); - analyzer.add_visitor(Phases::Syntax, JsDocTypeCollectorVisitior::default); + analyzer.add_visitor(Phases::Syntax, JsDocTypeCollectorVisitor::default); analyzer.add_visitor(Phases::Semantic, SyntaxVisitor::default); } diff --git a/crates/biome_js_analyze/tests/quick_test.rs b/crates/biome_js_analyze/tests/quick_test.rs index 14ea95c70394..adbb7a946590 100644 --- a/crates/biome_js_analyze/tests/quick_test.rs +++ b/crates/biome_js_analyze/tests/quick_test.rs @@ -1,4 +1,7 @@ -use biome_analyze::{AnalysisFilter, AnalyzerOptions, ControlFlow, Never, RuleFilter}; +use biome_analyze::options::JsxRuntime; +use biome_analyze::{ + AnalysisFilter, AnalyzerConfiguration, AnalyzerOptions, ControlFlow, Never, RuleFilter, +}; use biome_deserialize::TextRange; use biome_diagnostics::{Diagnostic, DiagnosticExt, Severity, print_diagnostic_to_string}; use biome_fs::TemporaryFs; @@ -26,8 +29,14 @@ fn project_layout_with_top_level_dependencies(dependencies: Dependencies) -> Arc #[test] fn quick_test() { const FILENAME: &str = "dummyFile.ts"; - const SOURCE: &str = r#"import { sleep as alias } from "./sleep.ts"; -alias(100);"#; + const SOURCE: &str = r#"import * as postcssModules from "postcss-modules" + +type PostcssOptions = Parameters[0] + +export function f(options: PostcssOptions) { + console.log(options) +} +"#; let parsed = parse(SOURCE, JsFileSource::tsx(), JsParserOptions::default()); @@ -38,8 +47,12 @@ alias(100);"#; let file_path = Utf8PathBuf::from(format!("{}/{FILENAME}", fs.cli_path())); let mut error_ranges: Vec = Vec::new(); - let options = AnalyzerOptions::default().with_file_path(file_path.clone()); - let rule_filter = RuleFilter::Rule("nursery", "noFloatingPromises"); + let options = AnalyzerOptions::default() + .with_file_path(file_path.clone()) + .with_configuration( + AnalyzerConfiguration::default().with_jsx_runtime(JsxRuntime::ReactClassic), + ); + let rule_filter = RuleFilter::Rule("correctness", "noUnusedImports"); let dependencies = Dependencies(Box::new([("buffer".into(), "latest".into())])); diff --git a/crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/invalid-import-namespace.ts.snap b/crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/invalid-import-namespace.ts.snap deleted file mode 100644 index 638742aeb243..000000000000 --- a/crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/invalid-import-namespace.ts.snap +++ /dev/null @@ -1,57 +0,0 @@ ---- -source: crates/biome_js_analyze/tests/spec_tests.rs -expression: invalid-import-namespace.ts ---- -# Input -```ts -import * as Ns1 from "" -export type T1 = Ns1; - -import type * as Ns2 from "" -export type T2 = Ns2; -``` - -# Diagnostics -``` -invalid-import-namespace.ts:1:13 lint/correctness/noUnusedImports FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━ - - ! This import is unused. - - > 1 │ import * as Ns1 from "" - │ ^^^ - 2 │ export type T1 = Ns1; - 3 │ - - i Unused imports might be the result of an incomplete refactoring. - - i Unsafe fix: Remove the unused imports. - - 1 │ import·*·as·Ns1·from·"" - │ ----------------------- - -``` - -``` -invalid-import-namespace.ts:4:18 lint/correctness/noUnusedImports FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━ - - ! This import is unused. - - 2 │ export type T1 = Ns1; - 3 │ - > 4 │ import type * as Ns2 from "" - │ ^^^ - 5 │ export type T2 = Ns2; - - i Unused imports might be the result of an incomplete refactoring. - - i Unsafe fix: Remove the unused imports. - - 1 1 │ import * as Ns1 from "" - 2 │ - export·type·T1·=·Ns1; - 2 │ + export·type·T1·=·Ns1; - 3 3 │ - 4 │ - import·type·*·as·Ns2·from·"" - 5 4 │ export type T2 = Ns2; - - -``` diff --git a/crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/issue_6835.tsx b/crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/issue_6835.tsx new file mode 100644 index 000000000000..2ae9f9d4e046 --- /dev/null +++ b/crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/issue_6835.tsx @@ -0,0 +1,7 @@ +import * as postcssModules from "postcss-modules" + +type PostcssOptions = Parameters[0] + +export function f(options: PostcssOptions) { + console.log(options) +} diff --git a/crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/issue_6835.tsx.snap b/crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/issue_6835.tsx.snap new file mode 100644 index 000000000000..8c634fbd9463 --- /dev/null +++ b/crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/issue_6835.tsx.snap @@ -0,0 +1,15 @@ +--- +source: crates/biome_js_analyze/tests/spec_tests.rs +expression: issue_6835.tsx +--- +# Input +```tsx +import * as postcssModules from "postcss-modules" + +type PostcssOptions = Parameters[0] + +export function f(options: PostcssOptions) { + console.log(options) +} + +``` diff --git a/crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/invalid-import-namespace.ts b/crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/valid-import-namespace.ts similarity index 57% rename from crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/invalid-import-namespace.ts rename to crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/valid-import-namespace.ts index 5985b2d0ae20..2666c761dc58 100644 --- a/crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/invalid-import-namespace.ts +++ b/crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/valid-import-namespace.ts @@ -1,5 +1,6 @@ +// should not generate diagnostics import * as Ns1 from "" export type T1 = Ns1; import type * as Ns2 from "" -export type T2 = Ns2; \ No newline at end of file +export type T2 = Ns2; diff --git a/crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/valid-import-namespace.ts.snap b/crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/valid-import-namespace.ts.snap new file mode 100644 index 000000000000..1b5d185ff089 --- /dev/null +++ b/crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/valid-import-namespace.ts.snap @@ -0,0 +1,14 @@ +--- +source: crates/biome_js_analyze/tests/spec_tests.rs +expression: valid-import-namespace.ts +--- +# Input +```ts +// should not generate diagnostics +import * as Ns1 from "" +export type T1 = Ns1; + +import type * as Ns2 from "" +export type T2 = Ns2; + +``` diff --git a/crates/biome_js_semantic/src/events.rs b/crates/biome_js_semantic/src/events.rs index 5daef3437970..0b6e01707531 100644 --- a/crates/biome_js_semantic/src/events.rs +++ b/crates/biome_js_semantic/src/events.rs @@ -648,6 +648,7 @@ impl SemanticEventExtractor { .and_then(|clause| clause.type_token()); if type_token.is_none() { self.push_binding(None, BindingName::Value(name.clone()), info.clone()); + self.push_binding(None, BindingName::Type(name.clone()), info.clone()); } else { self.push_binding(None, BindingName::Type(name), info); } @@ -1012,7 +1013,6 @@ impl SemanticEventExtractor { is_read: !reference.is_write(), range: reference.range(), }); - continue; } // Handle edge case where a parameter has the same name that a parameter type name // For example `(stream: stream.T) => {}`