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

useHookAtTopLevel reports errors on code that is not React #3476

Closed
1 task done
ssilve1989 opened this issue Jul 19, 2024 · 16 comments · Fixed by #3999
Closed
1 task done

useHookAtTopLevel reports errors on code that is not React #3476

ssilve1989 opened this issue Jul 19, 2024 · 16 comments · Fixed by #3999
Assignees
Labels
S-Needs discussion Status: needs a discussion to understand criteria

Comments

@ssilve1989
Copy link

Environment information

CLI:
  Version:                      1.8.3
  Color support:                true

Platform:
  CPU Architecture:             aarch64
  OS:                           macos

Environment:
  BIOME_LOG_DIR:                unset
  NO_COLOR:                     unset
  TERM:                         "xterm-256color"
  JS_RUNTIME_VERSION:           "v20.11.1"
  JS_RUNTIME_NAME:              "node"
  NODE_PACKAGE_MANAGER:         "pnpm/8.10.2"

Biome Configuration:
  Status:                       Loaded successfully
  Formatter disabled:           false
  Linter disabled:              false
  Organize imports disabled:    false
  VCS disabled:                 false

Workspace:
  Open Documents:               0

What happened?

The following sample NestJS snippet triggers the useHookAtTopLevel thinking useGlobalInterceptors is a React hook.

async function bootstrap() {
  const app = await NestFactory.create(AppModule)
  app.useGlobalInterceptors(new RandomInterceptor());
}

bootstrap();

Expected result

It should not raise any warning/error

Code of Conduct

  • I agree to follow Biome's Code of Conduct
@yossydev
Copy link

Reproduced here. It is indeed an error.
https://github.com/yossydev/use-hook-at-top-level-nextjs

I'm not familiar with nestjs and don't know what use you are using useHookAtTopLevel for, but I would have thought it would be better not to check lint as this is a React rule in the first place.

@dyc3
Copy link
Contributor

dyc3 commented Jul 28, 2024

That link 404s.

This rule should only be checked in jsx or tsx environments. I don't think we currently have a way to automatically determine if code is react code or not other than that.

@yossydev
Copy link

That link 404s.

Sorry, this was private repository.
you can see it now.

This rule should only be checked in jsx or tsx environments. I don't think we currently have a way to automatically determine if code is react code or not other than that.

Certainly, that sounds better.

@Javimtib92
Copy link
Contributor

Javimtib92 commented Sep 6, 2024

I think we could use a couple of heuristics like checking is_react_component (checks if function starts with Uppercase like react components do) and checking if the file is jsx/tsx.

Could I take this task to try that out?

@dyc3
Copy link
Contributor

dyc3 commented Sep 6, 2024

Sure! I'll go ahead and assign you.

@arendjr
Copy link
Contributor

arendjr commented Sep 7, 2024

Please be aware we already have such a heuristic:

pub(crate) fn is_react_component(name: &str) -> bool {

I suppose this is not used by the rule yet…

@Javimtib92
Copy link
Contributor

Javimtib92 commented Sep 7, 2024

Please be aware we already have such a heuristic:

pub(crate) fn is_react_component(name: &str) -> bool {

I suppose this is not used by the rule yet…

Yep, that's the function I wanted to use. I was thinking now that maybe limiting this to jsx or tsx files is not a good heuristic because a hook could be used inside another hook defined in a js or ts file, right?

For example: https://github.com/ilyalesik/react-media-hook/blob/master/index.js

Edited: Thinking about it a bit more, only checking if it's inside a react component is not correct either because it might be used inside another hook. I'll take that into consideration too.

@Javimtib92
Copy link
Contributor

Investigating this I noticed that eslint approach is not perfect either:

image

@Javimtib92
Copy link
Contributor

A similar case was fixed for some jest functions that start with “use”

if let Some(expr) = call

I tried adding other heuristics, like checking that the root caller is a react component or a hook, but so far all I tried ends up changing the snapshots because some cases get uncovered. We might need to do some comprimises with this.

@dyc3
Copy link
Contributor

dyc3 commented Sep 8, 2024

It might be useful if biome could inspect package.json to guess what environment the file is in.

@Javimtib92
Copy link
Contributor

@dyc3 I think your suggestion is actually a good workaround. I opened a Draft PR with this change. I need to find a way to run the snapshots simulating an environment with the react dependency. I would highly appreciate any suggestions. Thanks!

@Conaclos
Copy link
Member

Conaclos commented Sep 9, 2024

There is something I don't understand: the rule is not enabled by default (it is not recommended). Thus, the rule should only be enabled if the project use React. Why did you enable it in the first place?

@ematipico
Copy link
Member

ematipico commented Sep 15, 2024

Maybe the documentation could be improved, but this rule isn't meant for non-react projects, so I don't think it's a bug on our side.

@ematipico ematipico added the S-Needs discussion Status: needs a discussion to understand criteria label Sep 15, 2024
@dyc3
Copy link
Contributor

dyc3 commented Sep 15, 2024

I agree with @ematipico I'm all for saving user's time configuring projects, and maybe we could come up with some really good heuristics to auto detect what kind of project biome is operating on, but heuristics are not perfect. If anything, it would probably be better to ask the user for that info in biome init when they are setting it up.

Would it make sense to have more collections of rules similar to how we currently have recommended rules? eg. react-recommended, react-all. Or we could give rules tags to do something similar, though tags might be a bit more complicated on the implementation side.

@ematipico
Copy link
Member

Would it make sense to have more collections of rules similar to how we currently have recommended rules?

There's an RFC for that, you can find it from the pinned umbrella issue "Biome developments"

@ssilve1989
Copy link
Author

There is something I don't understand: the rule is not enabled by default (it is not recommended). Thus, the rule should only be enabled if the project use React. Why did you enable it in the first place?

@Conaclos because I am an idiot apparently. I had all rules enabled and then after adding some offending code one day saw this error start appearing and didn't remember I had all instead of recommended

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-Needs discussion Status: needs a discussion to understand criteria
Projects
None yet
7 participants