-
-
Notifications
You must be signed in to change notification settings - Fork 463
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
feat(lint/noUnusedImports): add rule #428
Conversation
185f0d8
to
e32caaa
Compare
crates/biome_js_analyze/tests/specs/nursery/noUnusedImports/valid.jsx
Outdated
Show resolved
Hide resolved
crates/biome_js_analyze/tests/specs/nursery/noUnusedImports/valid.js
Outdated
Show resolved
Hide resolved
crates/biome_js_analyze/src/semantic_analyzers/nursery/no_unused_imports.rs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for looking after this. Here's a couple of important notes:
- The logic around React isn't complete, it should use the same utilities we used in the JSX rules. It's a solved problem
- The documentation should state that the code action might remove comments, and the criteria behind it
crates/biome_js_analyze/src/semantic_analyzers/nursery/no_unused_imports.rs
Outdated
Show resolved
Hide resolved
crates/biome_js_analyze/src/semantic_analyzers/nursery/no_unused_imports.rs
Outdated
Show resolved
Hide resolved
e32caaa
to
e97a4ae
Compare
Parser conformance results onjs/262
jsx/babel
symbols/microsoft
ts/babel
ts/microsoft
|
e97a4ae
to
ddfbafa
Compare
ddfbafa
to
4028d8f
Compare
crates/biome_js_analyze/src/semantic_analyzers/correctness/no_unused_variables.rs
Outdated
Show resolved
Hide resolved
crates/biome_js_analyze/src/semantic_analyzers/nursery/no_unused_imports.rs
Outdated
Show resolved
Hide resolved
/// There is one exception to the rule: the `React` import. | ||
/// Importing the `React` variable was a mandatory pattern until some time ago: | ||
/// For the time being this rule will ignore it, | ||
/// but this **might change in the future releases**. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should have an option instead, and users can add "React"
if they need. This is more future-proof, and it doesn't discriminate against some libraries. Who knows if in future we will have other "unused imports".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think we could just get rid of the React exception? Since when the issue is fixed in babel?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think we could just get rid of the React exception?
Yes, we can add an option to the rule, an array of strings:
{
"noUnusedImports": {
"level": "error",
"options": {
"allowedImports": [
{ "bindings": ["React"], "import": "react" }
]
}
}
}
So users can allow-list anything they want from any import.
Since when the issue is fixed in babel?
One of the recent versions. It's now possible writing React components without the React
at the top of the file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we can add an option to the rule, an array of strings
I mean without providing any options?
Have you other known use-cases for such a config?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only react as far as I know.
Uhmmmm, I'm overthinking it. Let's keep the React exception.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can drop the React
exception. If users report this issue, we can re-introduce the exception. Moreover, the rule is not recommended.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will merge this PR without the exception. If you think we really need this exception, I can open a follow-up PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi! I'm integrating Biome linter into our project, and enabling this rule removed all React
imports, thus breaking our build. Option to ignore React
would be nice, thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An option to ignore whatever we want would be preferable. I'm using Biome in the Roblox ecosystem (with Typescript), and we have to import it for our JSX implementation to work. Of course, it keeps trying to remove this too.
crates/biome_js_analyze/src/semantic_analyzers/nursery/no_unused_imports.rs
Outdated
Show resolved
Hide resolved
crates/biome_js_analyze/src/semantic_analyzers/nursery/no_unused_imports.rs
Outdated
Show resolved
Hide resolved
4028d8f
to
78fd73b
Compare
78fd73b
to
91d518c
Compare
91d518c
to
08cd939
Compare
Summary
This PR implements
noUnusedImports
. The rule reports unused imports and provides a safe code fix to remove them.Once stabilized, noUnusedVariables should be modified to ignore unused imports.
Splitting noUnusedVariables enables to have more control around unused imports and to provide a safe code fix. This is what does eslint-plugin-unused-imports. This allows supporting some user workflows.
The rule is not recommended because noUnusedVariables is not. Moreover this could conflict with other user workflows.
Test Plan
Tests added.