@@ -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;
0 commit comments