Skip to content

Commit

Permalink
fix(eslint-plugin-react-hooks): accepting as expressions as deps ar…
Browse files Browse the repository at this point in the history
…ray (#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

DiffTrain build for [a1433ca](a1433ca)
  • Loading branch information
eps1lon committed Feb 1, 2024
1 parent a96ef80 commit 9628430
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion compiled/facebook-www/REVISION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
53b12e46a17549ec7644e13c126440ed2f3629fd
a1433ca0bacff76f720ffec9a0020f56e8c9ffed
7 changes: 5 additions & 2 deletions compiled/facebook-www/eslint-plugin-react-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -1390,8 +1390,10 @@ var ExhaustiveDeps = {

var declaredDependencies = [];
var externalDependencies = new Set();
var isArrayExpression = declaredDependenciesNode.type === 'ArrayExpression';
var isTSAsArrayExpression = declaredDependenciesNode.type === 'TSAsExpression' && declaredDependenciesNode.expression.type === 'ArrayExpression';

if (declaredDependenciesNode.type !== 'ArrayExpression') {
if (!isArrayExpression && !isTSAsArrayExpression) {
// If the declared dependencies are not an array expression then we
// can't verify that the user provided the correct dependencies. Tell
// the user this in an error.
Expand All @@ -1400,7 +1402,8 @@ var ExhaustiveDeps = {
message: "React Hook " + context.getSource(reactiveHook) + " was passed a " + 'dependency list that is not an array literal. This means we ' + "can't statically verify whether you've passed the correct " + 'dependencies.'
});
} else {
declaredDependenciesNode.elements.forEach(function (declaredDependencyNode) {
var arrayExpression = isTSAsArrayExpression ? declaredDependenciesNode.expression : declaredDependenciesNode;
arrayExpression.elements.forEach(function (declaredDependencyNode) {
// Skip elided elements.
if (declaredDependencyNode === null) {
return;
Expand Down

0 comments on commit 9628430

Please sign in to comment.