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

Track JSX presence per-function, fixing some false negatives #830

Conversation

willheslam
Copy link
Contributor

@willheslam willheslam commented Sep 24, 2020

Removing some false negatives of movable functions that were ignored because they were defined after some JSX

This is a follow up to 8a999c0

I noticed this example has a false negative:

function Foo() {
  const Bar = <div />
  function doBaz() {
    return 42
  }
  return <div>{ doBaz() }</div>
}

where doBaz comes after Bar, so it's treated as unmovable because the rule is using a singular boolean flag.
If const Bar = <div /> is moved to after doBaz, it correctly identifies it as movable because the flag hasn't been set yet.
The scope has to reach module-level again before the flag is turned off, meaning any safely-movable functions succeeding some JSX, or succeeding another scope with some JSX in are ignored.

This PR changes the rule to track whether JSX has been identified more finely, so it no longer ignores safe functions like these.

I tried this change out on a large-ish React codebase and whilst it picked up a previous false negative that could be safely moved, it didn't newly identify any false positives, so that's a good sign above and beyond the test examples.

Removing some false negatives of movable functions that were ignored because they were defined after some JSX
@willheslam willheslam changed the title Track JSX presence per-function Track JSX presence per-function, fixing some false negatives Sep 24, 2020
@fisker
Copy link
Collaborator

fisker commented Sep 25, 2020

I think this is what I'm trying to do in #786, but I got myself confused...

Can you add tests from #786 here?

@willheslam
Copy link
Contributor Author

willheslam commented Sep 25, 2020

Hi @fisker, I'm happy to add extra test cases to this PR to make sure we're covering every angle. :)
Looking briefly at your PR, some of those tests look like they overlap with the ones already added here - as you're familiar with that PR, do you mind pointing out to me which ones you think should be added?

I've covered:
Passes lint: Conservatively ignoring functions with JSX in that otherwise could be safely moved
Fails lint: JSX in the same scope, preceding a function that could be safely moved
Fails lint: JSX in a nested scope, preceding a function that could be safely moved

@sindresorhus sindresorhus merged commit 85d424c into sindresorhus:master Sep 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants