Commit Graph

251 Commits

Author SHA1 Message Date
michael faith
5ccfcd17ff
feat(eslint-plugin-react-hooks): merge rule from eslint-plugin-react-compiler into react-hooks plugin (#32416)
This change merges the `react-compiler` rule from
`eslint-plugin-react-compiler` into the `eslint-plugin-react-hooks`
plugin. In order to do the move in a way that keeps commit history with
the moved files, but also no remove them from their origin until a
future cleanup change can be done, I did the `git mv` first, and then
recreated the files that were moved in their original places, as a
separate commit. Unfortunately GH shows the moved files as new instead
of the ones that are truly new. But in the IDE and `git blame`, commit
history is intact with the moved files.

Since this change adds new dependencies, and one of those dependencies
has a higher `engines` declaration for `node` than what the plugin
currently has, this is technically a breaking change and will have to go
out as part of a major release.

### Related Changes
- https://github.com/facebook/react/pull/32458

---------

Co-authored-by: Lauren Tan <poteto@users.noreply.github.com>
2025-03-12 21:43:06 -04:00
Mathias Stang
562f17efab
docs(eslint-plugin-react-hooks): add changelog for 5.1.0 & 5.2.0 (#32536)
<!--
  Thanks for submitting a pull request!
We appreciate you spending the time to work on these changes. Please
provide enough information so that others can review your pull request.
The three fields below are mandatory.

Before submitting a pull request, please make sure the following is
done:

1. Fork [the repository](https://github.com/facebook/react) and create
your branch from `main`.
  2. Run `yarn` in the repository root.
3. If you've fixed a bug or added code that should be tested, add tests!
4. Ensure the test suite passes (`yarn test`). Tip: `yarn test --watch
TestName` is helpful in development.
5. Run `yarn test --prod` to test in the production environment. It
supports the same options as `yarn test`.
6. If you need a debugger, run `yarn test --debug --watch TestName`,
open `chrome://inspect`, and press "Inspect".
7. Format your code with
[prettier](https://github.com/prettier/prettier) (`yarn prettier`).
8. Make sure your code lints (`yarn lint`). Tip: `yarn linc` to only
check changed files.
  9. Run the [Flow](https://flowtype.org/) type checks (`yarn flow`).
  10. If you haven't already, complete the CLA.

Learn more about contributing:
https://reactjs.org/docs/how-to-contribute.html
-->

## Summary

<!--
Explain the **motivation** for making this change. What existing problem
does the pull request solve?
-->

Adds changelog entries for the last two minor releases of
`eslint-plugin-react-hooks`. Fixes #31717.

I chose to not include #31208 (8382581446)
and #32115 (fd2d279984) in the changelog
as they only changed internals that do not affect consumers of the
plugin, and it doesn't seem like the changelog previously included such
changes.

Changes are sorted by importance (rather than by commit date), with the
most important changes first.

## How did you test this change?

<!--
Demonstrate the code is solid. Example: The exact commands you ran and
their output, screenshots / videos if the pull request changes the user
interface.
How exactly did you verify that your PR solves the issue you wanted to
solve?
  If you leave this empty, your PR will very likely be closed.
-->

Docs only, nothing to test.
2025-03-06 13:58:39 -05:00
michael faith
443b7ff2a8
docs(eslint-plugin-react-hooks): clarify config details for prior versions (#32498)
This change adds more details about prior versions of the plugin's
config, to help people as they migrate from legacy to flat configs
across multiple versions of this plugin. At some point in the 6.0 or 7.0
cycle, it would probably make sense to re-consolidate this into a single
version.

Closes #32494
2025-03-03 20:57:05 -05:00
michael faith
ca12911d1f
feat(eslint-plugin-react-hooks): make flat config the recommended config (#32457)
This change swaps which config `recommended` is aliasing. In 5.2.0, the
new flat config was introduced as `recommended-latest`, while
`recommended` still pointed at the legacy rc-based config, with a note
that in the next major version `recommended` would be updated to point
at `recommend-latest`. This change makes that swap, and make the default
`recommended` experience the flat config. To continue using the legacy
rc recommended config, please make the following change in your config

```diff
- extends: ['plugin:react-hooks/recommended']
+ extends: ['plugin:react-hooks/recommended-legacy']
```

This change also deprecates `recommended-latest` in favor of
`recommended`. `recommended-latest` will be removed in a future major
version.

The README has been updated to reflect the new usage, and to put the
flat config sections before the legacy config sections.

I also took the opportunity to change the v9 fixture to use a typescript
config, serving as a demonstration for usage as well as a way to
validate the types are correct.

BREAKING CHANGE

---------

Co-authored-by: lauren <poteto@users.noreply.github.com>
2025-02-28 11:22:08 -05:00
michael faith
d55cc79bcf
refactor(eslint-plugin-react-hooks): move rules to rules folder (#32411)
Since the compiler plugin is going to be merged into the hooks plugin,
and ultimately decomposed into several more rules, it would be good to
start creating a more traditional folder structure for the plugin. This
change just moves the rules into a `rules` folder.

Co-authored-by: lauren <poteto@users.noreply.github.com>
2025-02-28 11:12:10 -05:00
michael faith
11ca4f6b69
feat(eslint-plugin-react-hooks): update engines declaration (#32458)
In preparation for the merging of the compiler plugin into this one
(#32416), this change proactively updates the plugin's `engines`
declaration to require Node versions greater than or equal to 18

BREAKING CHANGE

Co-authored-by: lauren <poteto@users.noreply.github.com>
2025-02-28 11:07:13 -05:00
lauren
a84862dbdc
[eslint] Target ES5 (#32420)
Update eslint-plugin-react-hooks to be built targetting ES5 instead. For
various reasons our internal infra relies on these files being built
already downleveled.
2025-02-18 16:56:02 -05:00
michael faith
4632e36a4e
refactor(eslint-plugin-react-hooks): change array type and improve conditionals (#32400)
Some checks failed
(Runtime) Build and Test / yarn test-build (1/3, --project=devtools -r=experimental) (push) Has been cancelled
(Runtime) Build and Test / yarn test-build (1/3, -r=experimental --env=development) (push) Has been cancelled
(Runtime) Build and Test / yarn test-build (1/3, -r=experimental --env=production) (push) Has been cancelled
(Runtime) Build and Test / yarn test-build (1/3, -r=stable --env=development) (push) Has been cancelled
(Runtime) Build and Test / yarn test-build (1/3, -r=stable --env=production) (push) Has been cancelled
(Runtime) Build and Test / yarn test-build (2/3, --project=devtools -r=experimental) (push) Has been cancelled
(Runtime) Build and Test / yarn test-build (2/3, -r=experimental --env=development) (push) Has been cancelled
(Runtime) Build and Test / yarn test-build (2/3, -r=experimental --env=production) (push) Has been cancelled
(Runtime) Build and Test / yarn test-build (2/3, -r=stable --env=development) (push) Has been cancelled
(Runtime) Build and Test / yarn test-build (2/3, -r=stable --env=production) (push) Has been cancelled
(Runtime) Build and Test / yarn test-build (3/3, --project=devtools -r=experimental) (push) Has been cancelled
(Runtime) Build and Test / yarn test-build (3/3, -r=experimental --env=development) (push) Has been cancelled
(Runtime) Build and Test / yarn test-build (3/3, -r=experimental --env=production) (push) Has been cancelled
(Runtime) Build and Test / yarn test-build (3/3, -r=stable --env=development) (push) Has been cancelled
(Runtime) Build and Test / yarn test-build (3/3, -r=stable --env=production) (push) Has been cancelled
(Runtime) Build and Test / Process artifacts combined (push) Has been cancelled
(Runtime) Build and Test / Search build artifacts for unminified errors (push) Has been cancelled
(Runtime) Build and Test / Check release dependencies (push) Has been cancelled
(Runtime) Build and Test / Check fixtures DOM (stable) (push) Has been cancelled
(Runtime) Build and Test / Run fixtures Flight tests (push) Has been cancelled
(Runtime) Build and Test / Build DevTools and process artifacts (chrome) (push) Has been cancelled
(Runtime) Build and Test / Build DevTools and process artifacts (edge) (push) Has been cancelled
(Runtime) Build and Test / Build DevTools and process artifacts (firefox) (push) Has been cancelled
(Runtime) Build and Test / Merge DevTools artifacts (push) Has been cancelled
(Runtime) Build and Test / Run DevTools e2e tests (push) Has been cancelled
(Runtime) Build and Test / Run sizebot (push) Has been cancelled
(Runtime) Publish Prereleases Nightly / Publish to Canary channel (push) Has been cancelled
(Compiler) Publish Prereleases Nightly / Publish to Experimental channel (push) Has been cancelled
(Runtime) Publish Prereleases Nightly / Publish to Experimental channel (push) Has been cancelled
(Compiler) Publish Prereleases Weekly / Publish to beta channel (push) Has been cancelled
- [build(eslint-plugin-react-hooks): add
ts-linting](4c0fbe73d9)
This change adds configuration to the eslint config governing
`eslint-plugin-react-hooks` to use the typescript-eslint plugin and
parser. It adds the typescript-recommended config, and configures the
team's preferred `array-type` convention.

- [refactor(eslint-plugin-react-hooks): improve
conditionals](540d0d95bc)
This change addresses several feedback items from
https://github.com/facebook/react/pull/32240

- [ci (eslint-e2e): exclude nested node_modules from
cache](a3279f46a8)
This change removes the nested fixture `node_modules` from being cached,
so that the symbolic link can be made after the build happens.
2025-02-16 20:28:12 -05:00
michael faith
5adf40208f
feat(eslint-plugin-react-hooks): convert to typescript and package type declarations (#32240)
<!--
  Thanks for submitting a pull request!
We appreciate you spending the time to work on these changes. Please
provide enough information so that others can review your pull request.
The three fields below are mandatory.

Before submitting a pull request, please make sure the following is
done:

1. Fork [the repository](https://github.com/facebook/react) and create
your branch from `main`.
  2. Run `yarn` in the repository root.
3. If you've fixed a bug or added code that should be tested, add tests!
4. Ensure the test suite passes (`yarn test`). Tip: `yarn test --watch
TestName` is helpful in development.
5. Run `yarn test --prod` to test in the production environment. It
supports the same options as `yarn test`.
6. If you need a debugger, run `yarn test --debug --watch TestName`,
open `chrome://inspect`, and press "Inspect".
7. Format your code with
[prettier](https://github.com/prettier/prettier) (`yarn prettier`).
8. Make sure your code lints (`yarn lint`). Tip: `yarn linc` to only
check changed files.
  9. Run the [Flow](https://flowtype.org/) type checks (`yarn flow`).
  10. If you haven't already, complete the CLA.

Learn more about contributing:
https://reactjs.org/docs/how-to-contribute.html
-->

## Summary

This change converts the eslint hooks plugin to typescript, which also
allows us to include type declarations in the package, for those using
[typescript eslint
configs](https://eslint.org/blog/2025/01/eslint-v9.18.0-released/#stable-typescript-configuration-file-support).

### Constituent changes that should land before this one

- [x] ~https://github.com/facebook/react/pull/32276~
- [x] https://github.com/facebook/react/pull/32279
- [x] https://github.com/facebook/react/pull/32283
- [x] https://github.com/facebook/react/pull/32393
- [x] https://github.com/facebook/react/pull/32396

Closes #30119

---------

Co-authored-by: Lauren Tan <poteto@users.noreply.github.com>
2025-02-16 14:10:54 -05:00
michael faith
442150e0e2
build(eslint-plugin-react-hooks): tsconfig and global types (#32283)
<!--
  Thanks for submitting a pull request!
We appreciate you spending the time to work on these changes. Please
provide enough information so that others can review your pull request.
The three fields below are mandatory.

Before submitting a pull request, please make sure the following is
done:

1. Fork [the repository](https://github.com/facebook/react) and create
your branch from `main`.
  2. Run `yarn` in the repository root.
3. If you've fixed a bug or added code that should be tested, add tests!
4. Ensure the test suite passes (`yarn test`). Tip: `yarn test --watch
TestName` is helpful in development.
5. Run `yarn test --prod` to test in the production environment. It
supports the same options as `yarn test`.
6. If you need a debugger, run `yarn test --debug --watch TestName`,
open `chrome://inspect`, and press "Inspect".
7. Format your code with
[prettier](https://github.com/prettier/prettier) (`yarn prettier`).
8. Make sure your code lints (`yarn lint`). Tip: `yarn linc` to only
check changed files.
  9. Run the [Flow](https://flowtype.org/) type checks (`yarn flow`).
  10. If you haven't already, complete the CLA.

Learn more about contributing:
https://reactjs.org/docs/how-to-contribute.html
-->

## Summary

Contributing to https://github.com/facebook/react/pull/32240, this
change adds the tsconfig, tsup config, and estree type declarations that
will be needed for that plugin's typescript migration.
2025-02-03 12:07:30 -05:00
michael faith
a657bc5dee
build(eslint-plugin-react-hooks): add dev dependencies for typescript migration (#32279)
<!--
  Thanks for submitting a pull request!
We appreciate you spending the time to work on these changes. Please
provide enough information so that others can review your pull request.
The three fields below are mandatory.

Before submitting a pull request, please make sure the following is
done:

1. Fork [the repository](https://github.com/facebook/react) and create
your branch from `main`.
  2. Run `yarn` in the repository root.
3. If you've fixed a bug or added code that should be tested, add tests!
4. Ensure the test suite passes (`yarn test`). Tip: `yarn test --watch
TestName` is helpful in development.
5. Run `yarn test --prod` to test in the production environment. It
supports the same options as `yarn test`.
6. If you need a debugger, run `yarn test --debug --watch TestName`,
open `chrome://inspect`, and press "Inspect".
7. Format your code with
[prettier](https://github.com/prettier/prettier) (`yarn prettier`).
8. Make sure your code lints (`yarn lint`). Tip: `yarn linc` to only
check changed files.
  9. Run the [Flow](https://flowtype.org/) type checks (`yarn flow`).
  10. If you haven't already, complete the CLA.

Learn more about contributing:
https://reactjs.org/docs/how-to-contribute.html
-->

## Summary

Contributing to https://github.com/facebook/react/pull/32240, this
change adds the dev dependencies needed to support the migration of the
plugin to typescript.
2025-01-31 14:32:23 -05:00
lauren
fd2d279984
[eslint-plugin-react-hooks] Inline meta fields (#32115)
rollup doesn't inline cjs requires (although it can with an external
plugin), so requiring package.json was causing issues internally at Meta
since that file doesn't exist there.

We could teach our build scripts to do so but given that the eslint meta
field is optional anyways I opted to just hardcode the name and omit the
version.
2025-01-17 15:16:36 -05:00
michael faith
61e713c1d3
feat(eslint-plugin-react-hooks): support flat config (#30774) 2025-01-16 18:39:11 -05:00
Miguel Jiménez Esún
301a18a6af
react-hooks/rules-of-hooks: detect issues in class properties (#31823)
Co-authored-by: Elizabeth Craig <elcraig@microsoft.com>
2025-01-06 12:12:09 +01:00
Andrew Clark
c86542b240
Bump next prerelease version numbers (#31676)
Updates the version numbers in the prerelease (canary and experimental)
channels.

---------

Co-authored-by: Jack Pope <jackpope1@gmail.com>
2024-12-12 14:10:46 -05:00
Marin Atanasov
7c4a7c9ddf
react-hooks/rules-of-hooks: Improve support for do/while loops (#31720) 2024-12-10 22:46:33 +01:00
Mark Skelton
eaf2d5c670
fix[eslint-plugin-react-hooks]: Fix error when callback argument is an identifier with an as expression (#31119) 2024-11-19 10:36:30 +01:00
Marin Atanasov
9daabc0bf9
react-hooks/rules-of-hooks: Add support for do/while loops (#28714)
<!--
  Thanks for submitting a pull request!
We appreciate you spending the time to work on these changes. Please
provide enough information so that others can review your pull request.
The three fields below are mandatory.

Before submitting a pull request, please make sure the following is
done:

1. Fork [the repository](https://github.com/facebook/react) and create
your branch from `main`.
  2. Run `yarn` in the repository root.
3. If you've fixed a bug or added code that should be tested, add tests!
4. Ensure the test suite passes (`yarn test`). Tip: `yarn test --watch
TestName` is helpful in development.
5. Run `yarn test --prod` to test in the production environment. It
supports the same options as `yarn test`.
6. If you need a debugger, run `yarn test --debug --watch TestName`,
open `chrome://inspect`, and press "Inspect".
7. Format your code with
[prettier](https://github.com/prettier/prettier) (`yarn prettier`).
8. Make sure your code lints (`yarn lint`). Tip: `yarn linc` to only
check changed files.
  9. Run the [Flow](https://flowtype.org/) type checks (`yarn flow`).
  10. If you haven't already, complete the CLA.

Learn more about contributing:
https://reactjs.org/docs/how-to-contribute.html
-->

## Summary

<!--
Explain the **motivation** for making this change. What existing problem
does the pull request solve?
-->

Currently, `react-hooks/rules-of-hooks` does not support `do/while`
loops - I've also reported this in
https://github.com/facebook/react/issues/28713.

This PR takes a stab at adding support for `do/while` by following the
same logic we already have for detecting `while` loops.

After this PR, any hooks called inside a `do/while` loop will be
considered invalid.

We're also adding some unit tests to confirm that the behavior is
working as expected.

Fixes #28713.

## How did you test this change?

<!--
Demonstrate the code is solid. Example: The exact commands you ran and
their output, screenshots / videos if the pull request changes the user
interface.
How exactly did you verify that your PR solves the issue you wanted to
solve?
  If you leave this empty, your PR will very likely be closed.
-->

I've added unit tests that cover the case and verified that they pass by
running:

```
yarn test packages/eslint-plugin-react-hooks/__tests__/ESLintRulesOfHooks-test.js --watch
```

I've also verified that the rest of the tests continue to pass by
running:

```
yarn test
```

and

```
yarn test --prod
```
2024-10-22 13:07:10 -07:00
Sebastian "Sebbie" Silbermann
8382581446
[ESLint] Add test for rejected useId in async Components (#31208) 2024-10-15 17:17:41 +02:00
Sebastian "Sebbie" Silbermann
09111202d6
eslint-plugin-react-hooks: Release 5.0.0 (#31176)
Co-authored-by: lauren <poteto@users.noreply.github.com>
2024-10-11 14:51:59 +02:00
Sebastian Silbermann
1b0132c05a
Consider dispatch from useActionState stable (#29665) 2024-07-06 08:52:20 +02:00
Sebastian Silbermann
6f18664b82
eslint-plugin-react-hooks: Add support for ESLint v9 (#28773) 2024-04-23 23:29:01 +02:00
Andrew Clark
857ee8cdf9
Don't minify symbols in production builds (#28881)
This disables symbol renaming in production builds. The original
variable and function names are preserved. All other forms of
compression applied by Closure (dead code elimination, inlining, etc)
are unchanged — the final program is identical to what we were producing
before, just in a more readable form.

The motivation is to make it easier to debug React issues that only
occur in production — the same reason we decided to start shipping
sourcemaps in #28827 and #28827.

However, because most apps run their own minification step on their npm
dependencies, it's not necessary for us to minify the symbols before
publishing — it'll be handled the app, if desired.

This is the same strategy Meta has used to ship React for years. The
React build itself has unminified symbols, but they get minified as part
of Meta's regular build pipeline.

Even if an app does not minify their npm dependencies, gzip covers most
of the cost of symbol renaming anyway.

This saves us from having to ship sourcemaps, which means even apps that
don't have sourcemaps configured will be able to debug the React build
as easily as they would any other npm dependency.
2024-04-20 11:23:46 -04:00
Jean-François Greffier
496fd3da5a
[ESLint] Update README links to react.dev (#28692)
Update ESLint plugin README to redirect to https://react.dev for more
details (instead of legacy docs)
2024-04-01 10:52:34 -04:00
Sebastian Silbermann
a540f53616
Add passing exhaustive-deps test for type parameters in TypeScript (#20432) 2024-03-14 15:41:37 +01:00
Ricky
1940cb27b2
Update /link URLs to react.dev (#28477)
Depends on https://github.com/reactjs/react.dev/pull/6670 [merged]
2024-03-03 17:34:33 -05:00
Ricky
5f2c6b74db
Update homepage URLs to react.dev (#28478)
Updates the package.json "homepage" entry to react.dev
2024-03-01 14:35:18 -05:00
StyleShit
2efa38332a
fix(eslint-plugin-react-hooks): accepting as expression as a callback (#28202)
## Summary

Closes #20750

## How did you test this change?

Added a test case
2024-02-01 21:08:21 +01:00
StyleShit
a1433ca0ba
fix(eslint-plugin-react-hooks): accepting as expressions as deps array (#28189)
## Summary

This PR closes #25844
The original issue talks about `as const`, but seems like it fails for
any `as X` expressions since it adds another nesting level to the AST.

EDIT: Also closes #20162

## How did you test this change?

Added unit tests
2024-02-01 20:30:17 +01:00
Ricky
cc7d421629
Add lint tests that should fail (#28147)
These though fail.

Anon default export: https://github.com/facebook/react/issues/21181
Promise callbacks: https://github.com/facebook/react/issues/26186
Returning anon functions: https://github.com/facebook/react/issues/22520
2024-01-29 12:54:32 -05:00
Jan Kassens
640ccebb7d
[lint] treat React.use() the same as use() (#27769)
We should probably treat `React.use()` the same as `use()` to allow it
within loops and conditionals.

Ideally this would implement a test that `React` is imported or required
from `'react'`, but we don't otherwise implement such a test.
2023-12-01 15:02:11 -05:00
Jan Kassens
b8be034f07
[lint] move use lint to non-experimental (#27768)
`use` is being stabilized, so let's make sure the lint is updated for
the next release.
2023-11-30 17:39:00 -05:00
George Zahariev
6bfc0e032a
Support Flow as expressions in ESLint rules (#27590)
Support Flow `as` expressions in ESLint rules, e.g. `<expr> as <type>`.
This is the same syntax as TypeScript as expressions. I just looked for
any place referencing `TSAsExpression` (the TS node) or
`TypeCastExpression` (the previous Flow syntax) and added a case for
`AsExpression` as well.
2023-11-01 15:24:06 -04:00
Christoph Nakazawa
d947c2f110
Allow useEffect(fn, undefined) in react-hooks/exhaustive-deps. (#27525)
## Summary

There is a bug in the `react-hooks/exhaustive-deps` rule that forbids
the dependencies argument from being `undefined`. It triggers the error
that the dependency list is not an array literal. This makes sense in
pre ES5 strict-mode environments as undefined could be redefined, but
should not be a concern in today's JS environments.

**Justification:**
* The deps argument being undefined (for `useEffect` calls etc.) is a
valid use case for hooks that should re-run on every render.
* The deps argument being omitted is considered a valid use case by the
`exhaustive-deps` rule already.
* The TypeScript type definitions support passing `undefined` because
hooks are typed as `useEffect(effect: EffectCallback, deps?:
DependencyList): void;`.
* Since omitting an argument and passing `undefined` are considered
equivalent, this eslint rule should consider them as equivalent too.

Further, I accidentally forgot passing a dependency array to `useEffect`
in code that I shared on Twitter, and people started abusing me about
it. I'd like to create an eslint rule for my projects that requires me
to provide a dep argument in all cases (`undefined`, `[]` or the list of
dependencies) so that I can avoid such problems in the future. This
would also force me to always think about the dependencies instead of
accidentally forgetting them and my hook running on each render. In an
audit of my own codebase I had about 3% of hooks that I want to run on
each render, and adding an explicit `undefined` seems reasonable in
those situations.

It could be argued this could be an option or part of the
`exhaustive-deps` rule, but it's probably better to merge this PR, make
a release and see if my custom eslint rule gains traction in the future.

## How did you test this change?

* Added a test.
* `yarn test ESLintRuleExhaustiveDeps-test`
* Careful code inspection.
2023-11-01 15:05:55 -04:00
Andrew Clark
7118f5dd7b
[ESLint] Disallow hooks in async functions (#27045)
Hooks cannot be called in async functions, on either the client or the
server. This mistake sometimes happens when using Server Components,
especially when refactoring a Server Component to a Client Component.

React logs a warning at runtime, but it's even better to catch this with
a lint rule since it will show immediate inline feedback in the editor.

I added this to the existing "Rules of Hooks" ESLint rule.
2023-07-05 11:44:28 -04:00
Sophie Alpert
767f52237c
Use .slice() for all substring-ing (#26677)
- substr is Annex B
- substring silently flips its arguments if they're in the "wrong order", which is confusing
- slice is better than sliced bread (no pun intended) and also it works the same way on Arrays so there's less to remember

---

> I'd be down to just lint and enforce a single form just for the potential compression savings by using a repeated string.

_Originally posted by @sebmarkbage in https://github.com/facebook/react/pull/26663#discussion_r1170455401_
2023-04-19 14:26:01 -07:00
Jan Kassens
6b30832666
Upgrade prettier (#26081)
The old version of prettier we were using didn't support the Flow syntax
to access properties in a type using `SomeType['prop']`. This updates
`prettier` and `rollup-plugin-prettier` to the latest versions.

I added the prettier config `arrowParens: "avoid"` to reduce the diff
size as the default has changed in Prettier 2.0. The largest amount of
changes comes from function expressions now having a space. This doesn't
have an option to preserve the old behavior, so we have to update this.
2023-01-31 08:25:05 -05:00
Jan Kassens
2b1fb91a55
ESLint upgrade to use hermes-eslint (#25915)
Hermes parser is the preferred parser for Flow code going forward. We
need to upgrade to this parser to support new Flow syntax like function
`this` context type annotations or `ObjectType['prop']` syntax.

Unfortunately, there's quite a few upgrades here to make it work somehow
(dependencies between the changes)

- ~Upgrade `eslint` to `8.*`~ reverted this as the React eslint plugin
tests depend on the older version and there's a [yarn
bug](https://github.com/yarnpkg/yarn/issues/6285) that prevents
`devDependencies` and `peerDependencies` to different versions.
- Remove `eslint-config-fbjs` preset dependency and inline the rules,
imho this makes it a lot clearer what the rules are.
- Remove the turned off `jsx-a11y/*` rules and it's dependency instead
of inlining those from the `fbjs` config.
- Update parser and dependency from `babel-eslint` to `hermes-eslint`.
- `ft-flow/no-unused-expressions` rule replaces `no-unused-expressions`
which now allows standalone type asserts, e.g. `(foo: number);`
- Bunch of globals added to the eslint config
- Disabled `no-redeclare`, seems like the eslint upgrade started making
this more precise and warn against re-defined globals like
`__EXPERIMENTAL__` (in rollup scripts) or `fetch` (when importing fetch
from node-fetch).
- Minor lint fixes like duplicate keys in objects.
2022-12-20 14:27:01 -05:00
Sebastian Markbåge
84a0a171ea
Rename experimental useEvent to useEffectEvent (#25881)
We originally had grand plans for using this Event concept for more but
now it's only meant to be used in combination with effects.

It's an Event in the FRP terms, that is triggered from an Effect.
Technically it can also be from another function that itself is
triggered from an existing side-effect but that's kind of an advanced
case.

The canonical case is an effect that triggers an event:

```js
const onHappened = useEffectEvent(() => ...);
useEffect(() => {
  onHappened();
}, []);
```
2022-12-14 15:08:29 -05:00
lauren
9336e29d91
[useEvent] Lint for presence of useEvent functions in dependency lists (#25512)
* [useEvent] Lint for presence of useEvent functions in dependency lists

With #25473, the identity of useEvent's return value is no longer stable
across renders. Previously, the ExhaustiveDeps lint rule would only
allow the omission of the useEvent function, but you could still add it
as a dependency.

This PR updates the ExhaustiveDeps rule to explicitly check for the
presence of useEvent functions in dependency lists, and emits a warning
and suggestion/autofixer for removing the dependency.
2022-10-19 12:03:46 -07:00
Andrew Clark
9cdf8a99ed
[Codemod] Update copyright header to Meta (#25315)
* Facebook -> Meta in copyright

rg --files | xargs sed -i 's#Copyright (c) Facebook, Inc. and its affiliates.#Copyright (c) Meta Platforms, Inc. and affiliates.#g'

* Manual tweaks
2022-10-18 11:19:24 -04:00
Lauren Tan
3fd9bd8e74 Add RulesOfHooks support for use
Usage of the new `use` hook needs to conform to the rules of hooks, with
the one exception that it can be called conditionally.

ghstack-source-id: 7ea5beceaf
Pull Request resolved: https://github.com/facebook/react/pull/25370
2022-10-04 12:41:29 -04:00
Lauren Tan
06066c1a5a Make RulesOfHooks-test more consistent with ExhaustiveDeps-test
Small formatting changes to make the tests consistent.

ghstack-source-id: 7013a37f1f
Pull Request resolved: https://github.com/facebook/react/pull/25369
2022-09-30 15:16:22 -04:00
Lauren Tan
49ae0fad84 Fix RulesOfHooks test case indentation
Just a small formatting fix.

ghstack-source-id: c71ff02ed9
Pull Request resolved: https://github.com/facebook/react/pull/25368
2022-09-30 15:16:22 -04:00
Lauren Tan
cb5084d1c4
[ESLint] Check useEvent references instead (#25319)
* [ESLint] Check useEvent references instead

Previously the useEvent check in RulesOfHooks would collect all
definitions of useEvent functions at the top level, record them as
violations, then clear those violations if the useEvent function was
later called or referened inside of an effect or another event.

The flaw with this approach was in the special case where useEvent
functions could be passed by reference inside of effects or events. The
violation would be cleared here (since it was called at least once)
and subsequent usages of the useEvent function would not be properly
checked.

This PR changes it so we check all identifiers that resolve to a
useEvent function, and if they are not in an effect or event must be
called or a lint error is emitted.

Co-authored-by: Dan Abramov <dan.abramov@gmail.com>

* Add comment

Co-authored-by: Dan Abramov <dan.abramov@gmail.com>
2022-09-24 01:34:17 +01:00
Lauren Tan
c89a83695c
Update RulesOfHooks with useEvent rules (#25285)
This update to the RulesOfHooks rule checks that functions created with
`useEvent` can only be invoked in a `useEffect` callback, in another
event function, or a closure.
They can't be passed down directly as a reference to child components.

This PR also updates the ExhaustiveDeps lint rule to treat useEvent's 
return value as stable, so it can be omitted from dependency lists.

Currently this all gated behind an experimental flag.

Co-authored-by: Dan Abramov <dan.abramov@gmail.com>
2022-09-23 14:55:12 -07:00
Jan Kassens
2c2d9a1df0
[eslint-plugin-react-hooks] only allow capitalized component names (#25162)
- update naming rules to disallow _component
- update eslint-plugin-react-hooks version
2022-09-01 10:07:31 -04:00
Sebastian Silbermann
59bc52a16c
Add 4.5.0 release to eslint rules CHANGELOG (#24853)
Eye-balled from https://app.renovatebot.com/package-diff?name=eslint-plugin-react-hooks&from=4.4.0&to=4.5.0#d2h-042857 which changes were included.
2022-07-21 15:46:10 -04:00
Kerim Büyükakyüz
7cf9f5e03a
Extra space (#24612) 2022-06-17 10:43:17 -07:00
Josh Story
5cc2487e08
bump versions for next release (#24725) 2022-06-14 13:24:00 -07:00
Andrew Clark
72b7462fe7
Bump local package.json versions for 18.1 release (#24447) 2022-04-26 16:58:44 -04:00
Afzal Sayed
069d23bb74
[eslint-plugin-exhaustive-deps] Fix exhaustive deps check for unstable vars (#24343)
* Fix exhaustive deps for unstable vars

* Fix formatting

* Optimise iterations

* Fix linting
2022-04-11 21:43:16 +01:00
Stephen Cyron
1f7a901d7b
Fix false positive lint error with large number of branches (#24287)
* Switched RulesOfHooks.js to use BigInt. Added test and updated .eslintrc.js to use es2020.

* Added BigInt as readonly global in eslintrc.cjs.js and eslintrc.cjs2015.js

* Added comment to RulesOfHooks.js that gets rid of BigInt eslint error

* Got rid of changes in .eslintrc.js and yarn.lock

* Move global down

Co-authored-by: stephen cyron <stephen.cyron@fdmgroup.com>
Co-authored-by: dan <dan.abramov@gmail.com>
2022-04-08 00:22:47 +01:00
Brian Vaughn
aa05e73150
Add 4.4.0 release to eslint rules CHANGELOG (#24234) 2022-03-31 10:43:08 -04:00
Andrew Clark
34aa5cfe0d Update local package.jsons for 18 2022-03-29 12:07:33 -04:00
Sebastian Markbåge
0dedfcc681
Update the exports field (#23257)
* Add .browser and .node explicit entry points

This can be useful when the automatic selection doesn't work properly.

* Remove react/index

I'm not sure why I added this in the first place. Perhaps due to how our
builds work somehow.

* Remove build-info.json from files field
2022-02-08 21:07:26 -05:00
Dan Abramov
c3f34e4beb eslint-plugin-react-hooks@4.3.0 2021-11-09 13:58:55 +00:00
Dan Abramov
827021c4e3
Changelog for eslint-plugin-react-hooks@4.3.0 2021-11-09 13:55:27 +00:00
Michaël De Boey
0c0d1ddae4
feat(eslint-plugin-react-hooks): support ESLint 8.x (#22248) 2021-09-06 20:17:51 +01:00
Sinan Sonmez (Chaush)
8a37b0ef3f
typos fixed (#21955) 2021-08-03 14:05:20 -04:00
Brian Vaughn
d483463bc8
Updated scripts and config to replace "master" with "main" branch (#21768) 2021-06-29 14:26:24 -04:00
Ricky
a632f7de3b
Flip tuple order of useTransition (#20976) 2021-04-20 12:21:44 -04:00
Sebastian Markbåge
172e89b4bf
Reland Remove redundant initial of isArray (#21188)
* Remove redundant initial of isArray (#21163)

* Reapply prettier

* Type the isArray function with refinement support

This ensures that an argument gets refined just like it does if isArray is
used directly.

I'm not sure how to express with just a direct reference so I added a
function wrapper and confirmed that this does get inlined properly by
closure compiler.

* A few more

* Rename unit test to internal

This is not testing a bundle.

Co-authored-by: Behnam Mohammadi <itten@live.com>
2021-04-07 07:57:43 -07:00
Sebastian Markbage
b4f119cdf1 Revert "Remove redundant initial of isArray (#21163)"
This reverts commit b130a0f5cd.
2021-04-01 15:19:00 -04:00
Sebastian Markbage
c03197063d Revert "apply prettier (#21165)"
This reverts commit 94fd1214d2.
2021-04-01 15:18:56 -04:00
Behnam Mohammadi
94fd1214d2
apply prettier (#21165) 2021-04-01 12:09:16 -07:00
Behnam Mohammadi
b130a0f5cd
Remove redundant initial of isArray (#21163) 2021-04-01 10:50:48 -07:00
Ari Perkkiö
eb58c3909a
react-hooks/exhaustive-deps: Handle optional chained methods as dependency (#20204) (#20247) 2021-03-24 16:45:27 +00:00
Anthony Garritano
c62986cfd8
Add additional messaging for RulesOfHooks lint error (#20692)
* Add additional messaging for RulesOfHooks lint error

* Fix tests and spacing issue

* Prettify ESLintRulesOfHooks-test
2021-02-22 19:17:13 -05:00
Christian Ruigrok
e8eff119e0
Fix ESLint crash on empty react effect hook (#20385)
* Fix ESLint crash on empty react effect hook

* Add layout effect to test

* Improve wording in comment

* Improve lint warning wording

* Reword missing effect callback message
2021-01-11 15:18:45 -05:00
Dan Abramov
40cfe1f486
Update CHANGELOG.md 2020-10-21 01:15:28 +01:00
Dan Abramov
f021a983aa
Bump versions for 17 (#20062) 2020-10-20 21:41:18 +01:00
Dan Abramov
cc581065df eslint-plugin-react-hooks@4.1.2 2020-09-11 13:18:44 +01:00
Dan Abramov
0044805c88
Update CHANGELOG.md 2020-09-11 13:17:55 +01:00
Sebastian Silbermann
0f70d4dd66
Consider components in jsx as missing dependencies in @typescript-eslint/parser@4.x (#19815)
* Run JS tests with TS esling parser

* Add failing test

* fix: Mark JSXIdentifier has missing dependency

* Safe isSameIdentifier
2020-09-11 13:13:43 +01:00
Dan Abramov
ad8a0a8cd0 eslint-plugin-react-hooks@4.1.1 2020-09-10 19:54:42 +01:00
Dan Abramov
77544a0d6f
Update CHANGELOG.md 2020-09-10 19:54:10 +01:00
Sebastian Silbermann
ed4fdfc737
test(eslint-plugin-react-hooks): Run with TS parsers >= 2.x (#19792)
* test(eslint-plugin-react-hooks): Run with TS parsers >= 2.x

* name test suites for each parser
2020-09-10 19:08:47 +01:00
Matthias Schiffer
cd75f93c03
eslint-plugin-react-hooks: fix compatibility with @typescript-eslint/parser@4.0.0+ (#19751)
In addition to `TSTypeQuery`, dependency nodes with a `TSTypeReference`
parent need to be ignored as well. Without this fix, generic type
variables will be listed as missing dependencies.

Example:

    export function useFoo<T>(): (foo: T) => boolean {
        return useCallback((foo: T) => false, []);
    }

This will report the following issue:

    React Hook useCallback has a missing dependency: 'T'. Either include
    it or remove the dependency array

Closes: #19742
2020-09-10 11:30:18 +01:00
Bhumij Gupta
53e622ca7f
Fix instances of function declaration after return (#19733)
* Add ESLint plugin to check for any function declare after return
* Refactor code to move function declarations before return and fix failing lint
2020-09-01 08:55:10 -04:00
Pascal Fong Kye
1396e4a8f5
Fixes eslint warning when node type is ChainExpression (#19680)
* Add babel parser which supports ChainExpression

* Add and fix tests for new babel eslint parser

* extract function to mark node

* refactor for compatibility with eslint v7.7.0+

* Update eslint to v7.7.0
Update hook test since eslint now supports nullish coalescing
2020-08-29 21:03:23 +01:00
Andrew Clark
a8500be893
Add startTransition as a known stable method (#19720)
The `startTransition` method returned from `useTransition` is a stable
method, like `dispatch` or `setState`. You should not have to specify
it as a hook dependency.
2020-08-28 16:44:39 -07:00
Dan Abramov
e4afb2fddf eslint-plugin-react-hooks@4.1.0 2020-08-17 13:31:20 +01:00
Dan Abramov
ced05c46c1
Update CHANGELOG.md 2020-08-17 13:30:16 +01:00
CY Lim
702fad4b1b
refactor fb.me redirect link to reactjs.org/link (#19598)
* refactor fb.me url to reactjs.org/link

* Update ESLintRuleExhaustiveDeps-test.js

* Update ReactDOMServerIntegrationUntrustedURL-test.internal.js

* Update createReactClassIntegration-test.js

* Update ReactDOMServerIntegrationUntrustedURL-test.internal.js

Co-authored-by: Dan Abramov <dan.abramov@gmail.com>
2020-08-17 13:25:50 +01:00
Jordan Eldredge
1d5e10f703
[eslint-plugin-react-hooks] Report constant constructions (#19590)
* [eslint-plugin-react-cooks] Report constant constructions

The dependency array passed to a React hook can be thought of as a list of cache keys. On each render, if any dependency is not `===` its previous value, the hook will be rerun. Constructing a new object/array/function/etc directly within your render function means that the value will be referentially unique on each render. If you then use that value as a hook dependency, that hook will get a "cache miss" on every render, making the dependency array useless.

This can be especially dangerous since it can cascade. If a hook such as `useMemo` is rerun on each render, not only are we bypassing the option to avoid potentially expensive work, but the value _returned_ by `useMemo` may end up being referentially unique on each render causing other downstream hooks or memoized components to become deoptimized.

* Fix/remove existing tests

* Don't give an autofix of wrapping object declarations

It may not be safe to just wrap the declaration of an object, since the object may get mutated.

Only offer this autofix for functions which are unlikely to get mutated.

Also, update the message to clarify that the entire construction of the value should get wrapped.

* Handle the long tail of nodes that will be referentially unique

* Catch let/var constant constructions on initial assignment

* Trim trailing whitespace

* Address feedback from @gaearon

* Rename "assignment" to "initialization"

* Add test for a constant construction used in multiple dependency arrays
2020-08-13 20:54:33 +01:00
Dan Abramov
fe19b4247b eslint-plugin-react-hooks@4.0.8 2020-07-13 17:58:49 +01:00
Bianca Del Carretto
84479046f7
reference not add to dependencies if only its type is used as typeof arg (#19316)
* reference not add to dependencies if only its type is used as typeof arg

* Add a few more tests
2020-07-13 17:57:00 +01:00
Dan Abramov
a2b4db05bc eslint-plugin-react-hooks@4.0.7 2020-07-10 19:08:05 +01:00
Dan Abramov
47915fd6e1
[ESLint] Fix a bug causing a too coarse dependency suggestion (#19313)
* Add regression test for ESLint rule

* Fix the issue
2020-07-10 19:02:08 +01:00
Dan Abramov
6fd4321135 eslint-plugin-react-hooks@4.0.6 2020-07-07 22:39:40 +01:00
Dan Abramov
7c35cb20ef
[ESLint] Handle optional member chains (#19275)
* Rename internal variables

This disambiguates "optional"/"required" because that terminology is taken by optional chaining.

* Handle optional member chains

* Update comment

Co-authored-by: Ricky <rickhanlonii@gmail.com>

Co-authored-by: Ricky <rickhanlonii@gmail.com>
2020-07-07 21:34:16 +01:00
Dan Abramov
7ca1d861e8
[ESLint] Consistently treat optional chaining as regular chaining (#19273)
* Revert "Fix ExhaustiveDeps ESLint rule throwing with optional chaining (#19260)"

This reverts commit 0f84b0f02b.

* Re-add a test from #19260

* Remove all code for optional chaining support

* Consistently treat optional chaining as regular chaining

This is not ideal because our suggestions use normal chaining. But it gets rid of all current edge cases.

* Add more tests

* More consistency in treating normal and optional expressions

* Add regression tests for every occurrence of Optional*
2020-07-07 17:38:44 +01:00
Joe Lencioni
0f84b0f02b
Fix ExhaustiveDeps ESLint rule throwing with optional chaining (#19260)
Certain code patterns using optional chaining syntax causes
eslint-plugin-react-hooks to throw an error.

We can avoid the throw by adding some guards. I didn't read through the
code to understand how it works, I just added a guard to every place
where it threw, so maybe there is a better fix closer to the root cause
than what I have here.

In my test case, I noticed that the optional chaining that was used in
the code was not included in the suggestions description or output,
but it seems like it should be. This might make a nice future
improvement on top of this fix, so I left a TODO comment to that effect.

Fixes #19243
2020-07-06 15:52:14 -04:00
Dan Abramov
1887c5d946 eslint-plugin-react-hooks@4.0.5 2020-06-30 12:16:49 +01:00
Fred Vollmer
b0533fe33c
fix(eslint-plugin-react-hooks): Support optional chaining when accessing prototype method inside useCallback and useMemo #19061 (#19062)
* fix(eslint-plugin-react-hooks): Support optional chaining when accessing prototype method #19061

* run prettier

* Add fix for #19043
2020-06-30 12:14:00 +01:00
YeonJuan
090c6ed751
[eslint-plugin-react-hooks]: handling sparse array when no-inline callback (#19145) 2020-06-17 12:29:02 -04:00
Ricky
30b47103d4
Fix spelling errors and typos (#19138) 2020-06-15 19:59:44 -04:00
Aaron Pettengill
9752d31f12
Document additionalHooks option (#19005)
* Document `additionalHooks` option

* Update README.md

Co-authored-by: Aaron Pettengill <aaron.pettengill@echoman.com>
Co-authored-by: Dan Abramov <dan.abramov@gmail.com>
2020-05-27 15:27:43 +01:00
Dan Abramov
b7ff888190 eslint-plugin-react-hooks@4.0.4 2020-05-27 14:38:27 +01:00