Skip to content

Commit

Permalink
fix(lint/useImportType): jsxRuntime wasn't respected in combined im…
Browse files Browse the repository at this point in the history
…port statement (biomejs#2483)
  • Loading branch information
arendjr authored Apr 16, 2024
1 parent 8b2ef89 commit 895e4a9
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 1 deletion.
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

0 comments on commit 895e4a9

Please sign in to comment.