Skip to content

Commit 9628430

Browse files
committed
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 DiffTrain build for [a1433ca](a1433ca)
1 parent a96ef80 commit 9628430

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

compiled/facebook-www/REVISION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
53b12e46a17549ec7644e13c126440ed2f3629fd
1+
a1433ca0bacff76f720ffec9a0020f56e8c9ffed

compiled/facebook-www/eslint-plugin-react-hooks.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,8 +1390,10 @@ var ExhaustiveDeps = {
13901390

13911391
var declaredDependencies = [];
13921392
var externalDependencies = new Set();
1393+
var isArrayExpression = declaredDependenciesNode.type === 'ArrayExpression';
1394+
var isTSAsArrayExpression = declaredDependenciesNode.type === 'TSAsExpression' && declaredDependenciesNode.expression.type === 'ArrayExpression';
13931395

1394-
if (declaredDependenciesNode.type !== 'ArrayExpression') {
1396+
if (!isArrayExpression && !isTSAsArrayExpression) {
13951397
// If the declared dependencies are not an array expression then we
13961398
// can't verify that the user provided the correct dependencies. Tell
13971399
// the user this in an error.
@@ -1400,7 +1402,8 @@ var ExhaustiveDeps = {
14001402
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.'
14011403
});
14021404
} else {
1403-
declaredDependenciesNode.elements.forEach(function (declaredDependencyNode) {
1405+
var arrayExpression = isTSAsArrayExpression ? declaredDependenciesNode.expression : declaredDependenciesNode;
1406+
arrayExpression.elements.forEach(function (declaredDependencyNode) {
14041407
// Skip elided elements.
14051408
if (declaredDependencyNode === null) {
14061409
return;

0 commit comments

Comments
 (0)