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

feat(lint/noUnusedImports): add rule #428

Merged
merged 2 commits into from
Sep 29, 2023
Merged

Conversation

Conaclos
Copy link
Member

@Conaclos Conaclos commented Sep 26, 2023

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.

@Conaclos Conaclos temporarily deployed to Website deployment September 26, 2023 20:55 — with GitHub Actions Inactive
@github-actions github-actions bot added A-Project Area: project A-Linter Area: linter A-Website Area: website L-JavaScript Language: JavaScript and super languages A-Diagnostic Area: diagnostocis A-Changelog Area: changelog labels Sep 26, 2023
@Conaclos Conaclos requested a review from a team September 27, 2023 17:29
@Conaclos Conaclos temporarily deployed to Website deployment September 27, 2023 22:28 — with GitHub Actions Inactive
Copy link
Member

@ematipico ematipico left a 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

@github-actions github-actions bot added the A-Parser Area: parser label Sep 28, 2023
@github-actions
Copy link
Contributor

Parser conformance results on

js/262

Test result main count This PR count Difference
Total 48863 48863 0
Passed 47808 47808 0
Failed 1055 1055 0
Panics 0 0 0
Coverage 97.84% 97.84% 0.00%

jsx/babel

Test result main count This PR count Difference
Total 40 40 0
Passed 37 37 0
Failed 3 3 0
Panics 0 0 0
Coverage 92.50% 92.50% 0.00%

symbols/microsoft

Test result main count This PR count Difference
Total 6212 6212 0
Passed 1767 1767 0
Failed 4445 4445 0
Panics 0 0 0
Coverage 28.44% 28.44% 0.00%

ts/babel

Test result main count This PR count Difference
Total 639 639 0
Passed 571 571 0
Failed 68 68 0
Panics 0 0 0
Coverage 89.36% 89.36% 0.00%

ts/microsoft

Test result main count This PR count Difference
Total 17224 17224 0
Passed 13123 13123 0
Failed 4101 4101 0
Panics 0 0 0
Coverage 76.19% 76.19% 0.00%

@Conaclos Conaclos temporarily deployed to Website deployment September 28, 2023 10:47 — with GitHub Actions Inactive
@Conaclos Conaclos temporarily deployed to Website deployment September 28, 2023 11:27 — with GitHub Actions Inactive
Comment on lines 26 to 29
/// 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**.
Copy link
Member

@ematipico ematipico Sep 28, 2023

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".

Copy link
Member Author

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?

Copy link
Member

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.

Copy link
Member Author

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?

Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member Author

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.

Copy link
Contributor

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!

Copy link

@jackTabsCode jackTabsCode Feb 16, 2024

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.

@Conaclos Conaclos temporarily deployed to Website deployment September 28, 2023 12:44 — with GitHub Actions Inactive
@Conaclos Conaclos temporarily deployed to Website deployment September 29, 2023 12:15 — with GitHub Actions Inactive
@Conaclos Conaclos temporarily deployed to Website deployment September 29, 2023 13:02 — with GitHub Actions Inactive
@Conaclos Conaclos merged commit 80fb2fc into main Sep 29, 2023
18 checks passed
@Conaclos Conaclos deleted the conaclos/lint/noUnusedImports branch September 29, 2023 13:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Changelog Area: changelog A-Diagnostic Area: diagnostocis A-Linter Area: linter A-Parser Area: parser A-Project Area: project A-Website Area: website L-JavaScript Language: JavaScript and super languages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants