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(useHookAtTopLevel): avoid diagnostics on non react projects #3826

Conversation

Javimtib92
Copy link
Contributor

@Javimtib92 Javimtib92 commented Sep 8, 2024

Summary

Closes #3476

To avoid reporting diagnostics of this hook on non react projects I added a check against the package.json to find react dependency. This should help on many of the edge cases. Like the methods from NestJS app starting with "use".

Test Plan

I need a way to run the snapshots as if the tests were running with the 'react' dependency. The CI tests should pass once this is done.

Quick Test screenshots

without react dependency with react dependency
Captura de pantalla 2024-09-08 a las 18 58 24 Captura de pantalla 2024-09-08 a las 18 56 37

@github-actions github-actions bot added A-Linter Area: linter L-JavaScript Language: JavaScript and super languages A-Changelog Area: changelog labels Sep 8, 2024
@Javimtib92 Javimtib92 marked this pull request as draft September 8, 2024 17:16
Copy link

codspeed-hq bot commented Sep 8, 2024

CodSpeed Performance Report

Merging #3826 will degrade performances by 6.34%

Comparing Javimtib92:fix/use-hook-at-top-level-on-non-react-components (b5aeafe) with main (713a76e)

Summary

❌ 1 regressions
✅ 106 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark main Javimtib92:fix/use-hook-at-top-level-on-non-react-components Change
react.production.min_3378072959512366797.js[cached] 1.9 ms 2 ms -6.34%

@Javimtib92
Copy link
Contributor Author

Javimtib92 commented Sep 8, 2024

@dyc3 Should I bump the version of the rule? And is there a way to simulate dependencies on the diagnostics ran by declare_lint_rule?

@dyc3
Copy link
Contributor

dyc3 commented Sep 8, 2024

No, the version field is the version of biome in which the rule became available, so leave that as is.

is there a way to simulate dependencies

I have no idea, I've never had to do that before.

@Javimtib92 Javimtib92 marked this pull request as ready for review September 9, 2024 15:21
@peaBerberian
Copy link
Contributor

peaBerberian commented Sep 13, 2024

Sorry if I missed something, but shouldn't we also at least look at the devDependencies key of the package.json?

I work on libraries where we sometimes use react for demo pages, development tools and/or tests. Those are generally not exposed to library users, and thus React is often only declared as a devDependency.
Yet we could profit from this rule.

I'm under the impression that:

package_json.dependencies.contains("react")

Only applies to the dependencies key here, sorry if I'm mistaken.

@Conaclos
Copy link
Member

Please, note that I added a comment in the issue.

@Javimtib92
Copy link
Contributor Author

Sorry if I missed something, but shouldn't we also at least look at the devDependencies key of the package.json?

I work on libraries where we sometimes use react for demo pages, development tools and/or tests. Those are generally not exposed to library users, and thus React is often only declared as a devDependency. Yet we could profit from this rule.

I'm under the impression that:

package_json.dependencies.contains("react")

Only applies to the dependencies key here, sorry if I'm mistaken.

I didn't think about that use case and I agree we should look at devDependencies too. Although I'm also waiting for an answer to the comment in the issue to understand better if what I tried to do here is really needed.

@Javimtib92 Javimtib92 force-pushed the fix/use-hook-at-top-level-on-non-react-components branch from ce26087 to b5aeafe Compare September 14, 2024 09:50
@peaBerberian
Copy link
Contributor

peaBerberian commented Sep 14, 2024

EDIT: In my first version of this comment, I forgot that the absence of a package.json file was properly handled.

Please, note that I added a comment in the issue.

Yes thanks, I read the issue yet still added a comment in this PR just in case it turned out to be merged in the near future, as I'm only checking here sporadically.

I share the same linked interrogation, and I fear edge cases where that heuristic would produce false negatives.

I didn't think about that use case and I agree we should look at devDependencies too

Thank you!

Just to go further through edge cases I'm wondering about: some project could theoretically have it only as an optional dependency or a peer dependency (like react-related libraries for example), or even not relying on a package manager to set-up react.

E.g., I've worked with developers only relying on minified library bundles added as script tags in their HTML - and most browsers now also allows directly importing modules at runtime.

Yet all of them could still be using biome and react, but maybe that's an unsupported usage?

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.

Blocking for now, we haven't identified what's the real bug. Let's first discuss it in the relative issue.

@ematipico
Copy link
Member

I am going to close it for now, as it's up to the developer to enable this rule in this project.

We are going to provide a better system in the future, possibly for Biome v2.0, so users can enable framework-related rules via configuration.

@ematipico ematipico closed this Sep 19, 2024
@Javimtib92 Javimtib92 deleted the fix/use-hook-at-top-level-on-non-react-components branch September 19, 2024 15:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Changelog Area: changelog A-Linter Area: linter L-JavaScript Language: JavaScript and super languages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

useHookAtTopLevel reports errors on code that is not React
5 participants