Skip to content

Commit 1ee6786

Browse files
committed
[eslint-plugin-react-hooks] add experimental_autoDependenciesHooks option (#33294)
DiffTrain build for [a3abf5f](a3abf5f)
1 parent 1a6cc81 commit 1ee6786

35 files changed

+123
-99
lines changed

compiled/eslint-plugin-react-hooks/index.js

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,30 @@ const rule$2 = {
5050
enableDangerousAutofixThisMayCauseInfiniteLoops: {
5151
type: 'boolean',
5252
},
53+
experimental_autoDependenciesHooks: {
54+
type: 'array',
55+
items: {
56+
type: 'string',
57+
},
58+
},
5359
},
5460
},
5561
],
5662
},
5763
create(context) {
58-
const additionalHooks = context.options &&
59-
context.options[0] &&
60-
context.options[0].additionalHooks
61-
? new RegExp(context.options[0].additionalHooks)
64+
const rawOptions = context.options && context.options[0];
65+
const additionalHooks = rawOptions && rawOptions.additionalHooks
66+
? new RegExp(rawOptions.additionalHooks)
6267
: undefined;
63-
const enableDangerousAutofixThisMayCauseInfiniteLoops = (context.options &&
64-
context.options[0] &&
65-
context.options[0].enableDangerousAutofixThisMayCauseInfiniteLoops) ||
68+
const enableDangerousAutofixThisMayCauseInfiniteLoops = (rawOptions &&
69+
rawOptions.enableDangerousAutofixThisMayCauseInfiniteLoops) ||
6670
false;
71+
const experimental_autoDependenciesHooks = rawOptions && Array.isArray(rawOptions.experimental_autoDependenciesHooks)
72+
? rawOptions.experimental_autoDependenciesHooks
73+
: [];
6774
const options = {
6875
additionalHooks,
76+
experimental_autoDependenciesHooks,
6977
enableDangerousAutofixThisMayCauseInfiniteLoops,
7078
};
7179
function reportProblem(problem) {
@@ -108,7 +116,7 @@ const rule$2 = {
108116
return result;
109117
};
110118
}
111-
function visitFunctionWithDependencies(node, declaredDependenciesNode, reactiveHook, reactiveHookName, isEffect) {
119+
function visitFunctionWithDependencies(node, declaredDependenciesNode, reactiveHook, reactiveHookName, isEffect, isAutoDepsHook) {
112120
if (isEffect && node.async) {
113121
reportProblem({
114122
node: node,
@@ -434,6 +442,9 @@ const rule$2 = {
434442
return;
435443
}
436444
if (!declaredDependenciesNode) {
445+
if (isAutoDepsHook) {
446+
return;
447+
}
437448
let setStateInsideEffectWithoutDeps = null;
438449
dependencies.forEach(({ references }, key) => {
439450
if (setStateInsideEffectWithoutDeps) {
@@ -485,6 +496,11 @@ const rule$2 = {
485496
}
486497
return;
487498
}
499+
if (isAutoDepsHook &&
500+
declaredDependenciesNode.type === 'Literal' &&
501+
declaredDependenciesNode.value === null) {
502+
return;
503+
}
488504
const declaredDependencies = [];
489505
const externalDependencies = new Set();
490506
const isArrayExpression = declaredDependenciesNode.type === 'ArrayExpression';
@@ -918,7 +934,12 @@ const rule$2 = {
918934
});
919935
return;
920936
}
921-
if (!declaredDependenciesNode && !isEffect) {
937+
const isAutoDepsHook = options.experimental_autoDependenciesHooks.includes(reactiveHookName);
938+
if ((!declaredDependenciesNode ||
939+
(isAutoDepsHook &&
940+
declaredDependenciesNode.type === 'Literal' &&
941+
declaredDependenciesNode.value === null)) &&
942+
!isEffect) {
922943
if (reactiveHookName === 'useMemo' ||
923944
reactiveHookName === 'useCallback') {
924945
reportProblem({
@@ -937,10 +958,13 @@ const rule$2 = {
937958
switch (callback.type) {
938959
case 'FunctionExpression':
939960
case 'ArrowFunctionExpression':
940-
visitFunctionWithDependencies(callback, declaredDependenciesNode, reactiveHook, reactiveHookName, isEffect);
961+
visitFunctionWithDependencies(callback, declaredDependenciesNode, reactiveHook, reactiveHookName, isEffect, isAutoDepsHook);
941962
return;
942963
case 'Identifier':
943-
if (!declaredDependenciesNode) {
964+
if (!declaredDependenciesNode ||
965+
(isAutoDepsHook &&
966+
declaredDependenciesNode.type === 'Literal' &&
967+
declaredDependenciesNode.value === null)) {
944968
return;
945969
}
946970
if ('elements' in declaredDependenciesNode &&
@@ -968,7 +992,7 @@ const rule$2 = {
968992
}
969993
switch (def.node.type) {
970994
case 'FunctionDeclaration':
971-
visitFunctionWithDependencies(def.node, declaredDependenciesNode, reactiveHook, reactiveHookName, isEffect);
995+
visitFunctionWithDependencies(def.node, declaredDependenciesNode, reactiveHook, reactiveHookName, isEffect, isAutoDepsHook);
972996
return;
973997
case 'VariableDeclarator':
974998
const init = def.node.init;
@@ -978,7 +1002,7 @@ const rule$2 = {
9781002
switch (init.type) {
9791003
case 'ArrowFunctionExpression':
9801004
case 'FunctionExpression':
981-
visitFunctionWithDependencies(init, declaredDependenciesNode, reactiveHook, reactiveHookName, isEffect);
1005+
visitFunctionWithDependencies(init, declaredDependenciesNode, reactiveHook, reactiveHookName, isEffect, isAutoDepsHook);
9821006
return;
9831007
}
9841008
break;

compiled/facebook-www/REVISION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6060367ef8a7a5bac12e0f830367bb13626db83a
1+
a3abf5f2f835ad0c61e2325f5cbac2d1d9045517
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6060367ef8a7a5bac12e0f830367bb13626db83a
1+
a3abf5f2f835ad0c61e2325f5cbac2d1d9045517

compiled/facebook-www/React-dev.classic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1537,7 +1537,7 @@ __DEV__ &&
15371537
exports.useTransition = function () {
15381538
return resolveDispatcher().useTransition();
15391539
};
1540-
exports.version = "19.2.0-www-classic-6060367e-20250517";
1540+
exports.version = "19.2.0-www-classic-a3abf5f2-20250519";
15411541
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
15421542
"function" ===
15431543
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

compiled/facebook-www/React-dev.modern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1537,7 +1537,7 @@ __DEV__ &&
15371537
exports.useTransition = function () {
15381538
return resolveDispatcher().useTransition();
15391539
};
1540-
exports.version = "19.2.0-www-modern-6060367e-20250517";
1540+
exports.version = "19.2.0-www-modern-a3abf5f2-20250519";
15411541
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
15421542
"function" ===
15431543
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

compiled/facebook-www/React-prod.classic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,4 +635,4 @@ exports.useSyncExternalStore = function (
635635
exports.useTransition = function () {
636636
return ReactSharedInternals.H.useTransition();
637637
};
638-
exports.version = "19.2.0-www-classic-6060367e-20250517";
638+
exports.version = "19.2.0-www-classic-a3abf5f2-20250519";

compiled/facebook-www/React-prod.modern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,4 +635,4 @@ exports.useSyncExternalStore = function (
635635
exports.useTransition = function () {
636636
return ReactSharedInternals.H.useTransition();
637637
};
638-
exports.version = "19.2.0-www-modern-6060367e-20250517";
638+
exports.version = "19.2.0-www-modern-a3abf5f2-20250519";

compiled/facebook-www/React-profiling.classic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ exports.useSyncExternalStore = function (
639639
exports.useTransition = function () {
640640
return ReactSharedInternals.H.useTransition();
641641
};
642-
exports.version = "19.2.0-www-classic-6060367e-20250517";
642+
exports.version = "19.2.0-www-classic-a3abf5f2-20250519";
643643
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
644644
"function" ===
645645
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

compiled/facebook-www/React-profiling.modern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ exports.useSyncExternalStore = function (
639639
exports.useTransition = function () {
640640
return ReactSharedInternals.H.useTransition();
641641
};
642-
exports.version = "19.2.0-www-modern-6060367e-20250517";
642+
exports.version = "19.2.0-www-modern-a3abf5f2-20250519";
643643
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
644644
"function" ===
645645
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

compiled/facebook-www/ReactART-dev.classic.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19063,10 +19063,10 @@ __DEV__ &&
1906319063
(function () {
1906419064
var internals = {
1906519065
bundleType: 1,
19066-
version: "19.2.0-www-classic-6060367e-20250517",
19066+
version: "19.2.0-www-classic-a3abf5f2-20250519",
1906719067
rendererPackageName: "react-art",
1906819068
currentDispatcherRef: ReactSharedInternals,
19069-
reconcilerVersion: "19.2.0-www-classic-6060367e-20250517"
19069+
reconcilerVersion: "19.2.0-www-classic-a3abf5f2-20250519"
1907019070
};
1907119071
internals.overrideHookState = overrideHookState;
1907219072
internals.overrideHookStateDeletePath = overrideHookStateDeletePath;
@@ -19100,7 +19100,7 @@ __DEV__ &&
1910019100
exports.Shape = Shape;
1910119101
exports.Surface = Surface;
1910219102
exports.Text = Text;
19103-
exports.version = "19.2.0-www-classic-6060367e-20250517";
19103+
exports.version = "19.2.0-www-classic-a3abf5f2-20250519";
1910419104
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
1910519105
"function" ===
1910619106
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

0 commit comments

Comments
 (0)