Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/funny-phones-read.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@biomejs/biome": patch
---

Fixed [#9020](https://github.com/biomejs/biome/issues/9020): When `javascript.jsxRuntime` is set to `reactClassic`, `noUnusedImports` and `useImportType` rules now allow importing the `React` identifier from a package other than `react`. This aligns the behavior with `tsc` (`--jsx=react`), which also allows importing `React` from any package.
34 changes: 14 additions & 20 deletions crates/biome_js_analyze/src/react.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,28 +309,22 @@ pub(crate) fn is_global_react_import(binding: &JsIdentifierBinding, lib: ReactLi
{
return false;
};

let Some(decl) = binding.declaration() else {
return false;
};

// This must be a default import or a namespace import
let syntax = match decl {
AnyJsBindingDeclaration::JsNamedImportSpecifier(specifier) => {
if !specifier.name().is_ok_and(|name| name.is_default()) {
return false;
}
specifier.into_syntax()
}
AnyJsBindingDeclaration::JsDefaultImportSpecifier(specifier) => specifier.into_syntax(),
AnyJsBindingDeclaration::JsNamespaceImportSpecifier(specifier) => specifier.into_syntax(),
_ => {
return false;
}
};
// Check import source
syntax
.ancestors()
.skip(1)
.find_map(JsImport::cast)
.and_then(|import| import.source_text().ok())
.is_some_and(|source| lib.import_names().contains(&source.text()))
matches!(
decl,
AnyJsBindingDeclaration::JsDefaultImportSpecifier(_)
| AnyJsBindingDeclaration::JsNamespaceImportSpecifier(_)
) || matches!(
decl,
AnyJsBindingDeclaration::JsNamedImportSpecifier(spec)
if spec.name().is_ok_and(|name| name.is_default())
)

// We don't check the import source here because tsc doesn't require that `React` is imported
// from the `react` package either.
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
/* should generate diagnostics */
import X from "react"
import * as X from "react"
import { default as X } from "react"

import React from "x"
import * as React from "x"
import { default as React } from "x"
import React, { useEffect } from "x"
Original file line number Diff line number Diff line change
Expand Up @@ -4,181 +4,82 @@ expression: invalid-unused-react.jsx
---
# Input
```jsx
/* should generate diagnostics */
import X from "react"
import * as X from "react"
import { default as X } from "react"

import React from "x"
import * as React from "x"
import { default as React } from "x"
import React, { useEffect } from "x"

```

# Diagnostics
```
invalid-unused-react.jsx:1:8 lint/correctness/noUnusedImports FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━
invalid-unused-react.jsx:2:8 lint/correctness/noUnusedImports FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! This import is unused.

> 1 │ import X from "react"
1 │ /* should generate diagnostics */
> 2 │ import X from "react"
│ ^
2 │ import * as X from "react"
3 │ import { default as X } from "react"
3 │ import * as X from "react"
4 │ import { default as X } from "react"

i Unused imports might be the result of an incomplete refactoring.

i Unsafe fix: Remove the unused imports.

1 │ import·X·from·"react"
│ ---------------------
1 │ - /*·should·generate·diagnostics·*/
2 │ - import·X·from·"react"
1 │ +
3 2 │ import * as X from "react"
4 3 │ import { default as X } from "react"


```

```
invalid-unused-react.jsx:2:13 lint/correctness/noUnusedImports FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━
invalid-unused-react.jsx:3:13 lint/correctness/noUnusedImports FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━

! This import is unused.

1 │ import X from "react"
> 2 │ import * as X from "react"
1 │ /* should generate diagnostics */
2 │ import X from "react"
> 3 │ import * as X from "react"
│ ^
3 │ import { default as X } from "react"
4
4 │ import { default as X } from "react"
5

i Unused imports might be the result of an incomplete refactoring.

i Unsafe fix: Remove the unused imports.

1 1 │ import X from "react"
2 │ - import·*·as·X·from·"react"
3 2 │ import { default as X } from "react"
4 3 │
1 1 │ /* should generate diagnostics */
2 2 │ import X from "react"
3 │ - import·*·as·X·from·"react"
4 3 │ import { default as X } from "react"
5 4 │


```

```
invalid-unused-react.jsx:3:8 lint/correctness/noUnusedImports FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━
invalid-unused-react.jsx:4:8 lint/correctness/noUnusedImports FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! This import is unused.

1 │ import X from "react"
2 │ import * as X from "react"
> 3 │ import { default as X } from "react"
2 │ import X from "react"
3 │ import * as X from "react"
> 4 │ import { default as X } from "react"
│ ^^^^^^^^^^^^^^^^
4 │
5 │ import React from "x"

i Unused imports might be the result of an incomplete refactoring.

i Unsafe fix: Remove the unused imports.

1 1 │ import X from "react"
2 2 │ import * as X from "react"
3 │ - import·{·default·as·X·}·from·"react"
4 3 │
5 4 │ import React from "x"


```

```
invalid-unused-react.jsx:5:8 lint/correctness/noUnusedImports FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! This import is unused.

3 │ import { default as X } from "react"
4 │
> 5 │ import React from "x"
│ ^^^^^
6 │ import * as React from "x"
7 │ import { default as React } from "x"

i Unused imports might be the result of an incomplete refactoring.

i Unsafe fix: Remove the unused imports.

1 1 │ import X from "react"
2 2 │ import * as X from "react"
3 │ - import·{·default·as·X·}·from·"react"
3 │ + import·{·default·as·X·}·from·"react"
4 4 │
5 │ - import·React·from·"x"
6 5 │ import * as React from "x"
7 6 │ import { default as React } from "x"


```

```
invalid-unused-react.jsx:6:13 lint/correctness/noUnusedImports FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━

! This import is unused.

5 │ import React from "x"
> 6 │ import * as React from "x"
│ ^^^^^
7 │ import { default as React } from "x"
8 │ import React, { useEffect } from "x"

i Unused imports might be the result of an incomplete refactoring.

i Unsafe fix: Remove the unused imports.

4 4 │
5 5 │ import React from "x"
6 │ - import·*·as·React·from·"x"
7 6 │ import { default as React } from "x"
8 7 │ import React, { useEffect } from "x"


```

```
invalid-unused-react.jsx:7:8 lint/correctness/noUnusedImports FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! This import is unused.

5 │ import React from "x"
6 │ import * as React from "x"
> 7 │ import { default as React } from "x"
│ ^^^^^^^^^^^^^^^^^^^^
8 │ import React, { useEffect } from "x"
9 │

i Unused imports might be the result of an incomplete refactoring.

i Unsafe fix: Remove the unused imports.

5 5 │ import React from "x"
6 6 │ import * as React from "x"
7 │ - import·{·default·as·React·}·from·"x"
8 7 │ import React, { useEffect } from "x"
9 8 │


```

```
invalid-unused-react.jsx:8:8 lint/correctness/noUnusedImports FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! These imports are unused.

6 │ import * as React from "x"
7 │ import { default as React } from "x"
> 8 │ import React, { useEffect } from "x"
│ ^^^^^^^^^^^^^^^^^^^^
9 │
5 │

i Unused imports might be the result of an incomplete refactoring.

i Unsafe fix: Remove the unused imports.

6 6 │ import * as React from "x"
7 7 │ import { default as React } from "x"
8 │ - import·React,·{·useEffect·}·from·"x"
9 8
2 2 │ import X from "react"
3 3 │ import * as X from "react"
4 │ - import·{·default·as·X·}·from·"react"
5 4


```
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
import React from "react"
import { default as React } from "react"
import * as React from "react"

// https://github.com/biomejs/biome/issues/9020
import React from "./not-actually-react"
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ import React from "react"
import { default as React } from "react"
import * as React from "react"

// https://github.com/biomejs/biome/issues/9020
import React from "./not-actually-react"

```
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/* should not generate diagnostics */
import React from "react";

// https://github.com/biomejs/biome/issues/9020
import React from "./not-actually-react";

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ expression: valid-unused-react.tsx
/* should not generate diagnostics */
import React from "react";

// https://github.com/biomejs/biome/issues/9020
import React from "./not-actually-react";

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

Expand Down