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/useImportType): jsxRuntime wasn't respected in combined import statement #2483

Merged
merged 2 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
8 changes: 7 additions & 1 deletion crates/biome_js_analyze/src/lint/style/use_import_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"linter": {
"rules": {
"style": {
"useImportType": "error"
}
}
},
"javascript": {
"jsxRuntime": "reactClassic"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React, { MouseEvent } from 'react';

function Component() {
const onClick = (event: MouseEvent) => { };
const onDblClick = (event: React.MouseEvent) => { };

return <div onClick={onClick} onDblClick={onDblClick}></div>;
}
Original file line number Diff line number Diff line change
@@ -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 <div onClick={onClick} onDblClick={onDblClick}></div>;
}

```

# 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';
│ +++++

```
2 changes: 2 additions & 0 deletions website/src/content/docs/internals/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down