diff --git a/CHANGELOG.md b/CHANGELOG.md index 6cb1bd413ee4..4c62f7a7f25d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,8 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b #### Bug fixes +- Fix case where `jsxRuntime` wasn't being respected by `useImportType` rule ([#2473](https://github.com/biomejs/biome/issues/2473)).Contributed by @arendjr + ### Parser diff --git a/crates/biome_js_analyze/src/lint/style/use_import_type.rs b/crates/biome_js_analyze/src/lint/style/use_import_type.rs index 0491e590b38a..8a93152aa1c1 100644 --- a/crates/biome_js_analyze/src/lint/style/use_import_type.rs +++ b/crates/biome_js_analyze/src/lint/style/use_import_type.rs @@ -126,7 +126,13 @@ impl Rule for UseImportType { AnyJsImportClause::JsImportCombinedClause(clause) => { let default_binding = clause.default_specifier().ok()?.local_name().ok()?; let default_binding = default_binding.as_js_identifier_binding()?; - let is_default_used_as_type = is_only_used_as_type(model, default_binding); + let is_default_used_as_type = if ctx.jsx_runtime() == JsxRuntime::ReactClassic + && is_global_react_import(default_binding, ReactLibrary::React) + { + false + } else { + is_only_used_as_type(model, default_binding) + }; match clause.specifier().ok()? { AnyJsCombinedSpecifier::JsNamedImportSpecifiers(named_specifiers) => { match named_import_type_fix(model, &named_specifiers) { diff --git a/crates/biome_js_analyze/tests/specs/style/useImportType/valid-unused-react-combined.options.json b/crates/biome_js_analyze/tests/specs/style/useImportType/valid-unused-react-combined.options.json new file mode 100644 index 000000000000..6e2a1082a14e --- /dev/null +++ b/crates/biome_js_analyze/tests/specs/style/useImportType/valid-unused-react-combined.options.json @@ -0,0 +1,12 @@ +{ + "linter": { + "rules": { + "style": { + "useImportType": "error" + } + } + }, + "javascript": { + "jsxRuntime": "reactClassic" + } +} diff --git a/crates/biome_js_analyze/tests/specs/style/useImportType/valid-unused-react-combined.tsx b/crates/biome_js_analyze/tests/specs/style/useImportType/valid-unused-react-combined.tsx new file mode 100644 index 000000000000..d65ae382b5cf --- /dev/null +++ b/crates/biome_js_analyze/tests/specs/style/useImportType/valid-unused-react-combined.tsx @@ -0,0 +1,8 @@ +import React, { MouseEvent } from 'react'; + +function Component() { + const onClick = (event: MouseEvent) => { }; + const onDblClick = (event: React.MouseEvent) => { }; + + return
; +} diff --git a/crates/biome_js_analyze/tests/specs/style/useImportType/valid-unused-react-combined.tsx.snap b/crates/biome_js_analyze/tests/specs/style/useImportType/valid-unused-react-combined.tsx.snap new file mode 100644 index 000000000000..ed50131cbf2b --- /dev/null +++ b/crates/biome_js_analyze/tests/specs/style/useImportType/valid-unused-react-combined.tsx.snap @@ -0,0 +1,43 @@ +--- +source: crates/biome_js_analyze/tests/spec_tests.rs +expression: valid-unused-react-combined.tsx +--- +# Input +```tsx +import React, { MouseEvent } from 'react'; + +function Component() { + const onClick = (event: MouseEvent) => { }; + const onDblClick = (event: React.MouseEvent) => { }; + + return
; +} + +``` + +# Diagnostics +``` +valid-unused-react-combined.tsx:1:1 lint/style/useImportType FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + ! Some named imports are only used as types. + + > 1 │ import React, { MouseEvent } from 'react'; + │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + 2 │ + 3 │ function Component() { + + i This import is only used as a type. + + > 1 │ import React, { MouseEvent } from 'react'; + │ ^^^^^^^^^^ + 2 │ + 3 │ function Component() { + + i Importing the types with import type ensures that they are removed by the transpilers and avoids loading unnecessary modules. + + i Safe fix: Use import type. + + 1 │ import·React,·{·type·MouseEvent·}·from·'react'; + │ +++++ + +``` diff --git a/website/src/content/docs/internals/changelog.md b/website/src/content/docs/internals/changelog.md index 0a6d1cb1e5c4..f50a09ba67f2 100644 --- a/website/src/content/docs/internals/changelog.md +++ b/website/src/content/docs/internals/changelog.md @@ -37,6 +37,8 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b #### Bug fixes +- Fix case where `jsxRuntime` wasn't being respected by `useImportType` rule ([#2473](https://github.com/biomejs/biome/issues/2473)).Contributed by @arendjr + ### Parser