Skip to content

Commit a209345

Browse files
committed
[compiler] Fix error message for custom hooks (facebook#33310)
We were printing "Custom" instead of "hook". DiffTrain build for [c6c2a52](facebook@c6c2a52)
1 parent 26de3cd commit a209345

37 files changed

+2744
-1117
lines changed

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

Lines changed: 38 additions & 14 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;
@@ -52847,7 +52871,7 @@ function visitFunctionExpression(errors, fn) {
5284752871
severity: ErrorSeverity.InvalidReact,
5284852872
reason: 'Hooks must be called at the top level in the body of a function component or custom hook, and may not be called within function expressions. See the Rules of Hooks (https://react.dev/warnings/invalid-hook-call-warning)',
5284952873
loc: callee.loc,
52850-
description: `Cannot call ${hookKind} within a function component`,
52874+
description: `Cannot call ${hookKind === 'Custom' ? 'hook' : hookKind} within a function expression`,
5285152875
suggestions: null,
5285252876
}));
5285352877
}

compiled/facebook-www/REVISION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
462d08f9ba41d48ab36bf405235c1c22023603dc
1+
c6c2a52ad8fb1894b03a3bb618eb57e5deca5aa0
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
462d08f9ba41d48ab36bf405235c1c22023603dc
1+
c6c2a52ad8fb1894b03a3bb618eb57e5deca5aa0

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-462d08f9-20250517";
1540+
exports.version = "19.2.0-www-classic-c6c2a52a-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-462d08f9-20250517";
1540+
exports.version = "19.2.0-www-modern-c6c2a52a-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-462d08f9-20250517";
638+
exports.version = "19.2.0-www-classic-c6c2a52a-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-462d08f9-20250517";
638+
exports.version = "19.2.0-www-modern-c6c2a52a-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-462d08f9-20250517";
642+
exports.version = "19.2.0-www-classic-c6c2a52a-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-462d08f9-20250517";
642+
exports.version = "19.2.0-www-modern-c6c2a52a-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-462d08f9-20250517",
19066+
version: "19.2.0-www-classic-c6c2a52a-20250519",
1906719067
rendererPackageName: "react-art",
1906819068
currentDispatcherRef: ReactSharedInternals,
19069-
reconcilerVersion: "19.2.0-www-classic-462d08f9-20250517"
19069+
reconcilerVersion: "19.2.0-www-classic-c6c2a52a-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-462d08f9-20250517";
19103+
exports.version = "19.2.0-www-classic-c6c2a52a-20250519";
1910419104
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
1910519105
"function" ===
1910619106
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18835,10 +18835,10 @@ __DEV__ &&
1883518835
(function () {
1883618836
var internals = {
1883718837
bundleType: 1,
18838-
version: "19.2.0-www-modern-462d08f9-20250517",
18838+
version: "19.2.0-www-modern-c6c2a52a-20250519",
1883918839
rendererPackageName: "react-art",
1884018840
currentDispatcherRef: ReactSharedInternals,
18841-
reconcilerVersion: "19.2.0-www-modern-462d08f9-20250517"
18841+
reconcilerVersion: "19.2.0-www-modern-c6c2a52a-20250519"
1884218842
};
1884318843
internals.overrideHookState = overrideHookState;
1884418844
internals.overrideHookStateDeletePath = overrideHookStateDeletePath;
@@ -18872,7 +18872,7 @@ __DEV__ &&
1887218872
exports.Shape = Shape;
1887318873
exports.Surface = Surface;
1887418874
exports.Text = Text;
18875-
exports.version = "19.2.0-www-modern-462d08f9-20250517";
18875+
exports.version = "19.2.0-www-modern-c6c2a52a-20250519";
1887618876
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
1887718877
"function" ===
1887818878
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11391,10 +11391,10 @@ var slice = Array.prototype.slice,
1139111391
})(React.Component);
1139211392
var internals$jscomp$inline_1624 = {
1139311393
bundleType: 0,
11394-
version: "19.2.0-www-classic-462d08f9-20250517",
11394+
version: "19.2.0-www-classic-c6c2a52a-20250519",
1139511395
rendererPackageName: "react-art",
1139611396
currentDispatcherRef: ReactSharedInternals,
11397-
reconcilerVersion: "19.2.0-www-classic-462d08f9-20250517"
11397+
reconcilerVersion: "19.2.0-www-classic-c6c2a52a-20250519"
1139811398
};
1139911399
if ("undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__) {
1140011400
var hook$jscomp$inline_1625 = __REACT_DEVTOOLS_GLOBAL_HOOK__;
@@ -11420,4 +11420,4 @@ exports.RadialGradient = RadialGradient;
1142011420
exports.Shape = TYPES.SHAPE;
1142111421
exports.Surface = Surface;
1142211422
exports.Text = Text;
11423-
exports.version = "19.2.0-www-classic-462d08f9-20250517";
11423+
exports.version = "19.2.0-www-classic-c6c2a52a-20250519";

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11103,10 +11103,10 @@ var slice = Array.prototype.slice,
1110311103
})(React.Component);
1110411104
var internals$jscomp$inline_1597 = {
1110511105
bundleType: 0,
11106-
version: "19.2.0-www-modern-462d08f9-20250517",
11106+
version: "19.2.0-www-modern-c6c2a52a-20250519",
1110711107
rendererPackageName: "react-art",
1110811108
currentDispatcherRef: ReactSharedInternals,
11109-
reconcilerVersion: "19.2.0-www-modern-462d08f9-20250517"
11109+
reconcilerVersion: "19.2.0-www-modern-c6c2a52a-20250519"
1111011110
};
1111111111
if ("undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__) {
1111211112
var hook$jscomp$inline_1598 = __REACT_DEVTOOLS_GLOBAL_HOOK__;
@@ -11132,4 +11132,4 @@ exports.RadialGradient = RadialGradient;
1113211132
exports.Shape = TYPES.SHAPE;
1113311133
exports.Surface = Surface;
1113411134
exports.Text = Text;
11135-
exports.version = "19.2.0-www-modern-462d08f9-20250517";
11135+
exports.version = "19.2.0-www-modern-c6c2a52a-20250519";

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31430,11 +31430,11 @@ __DEV__ &&
3143031430
return_targetInst = null;
3143131431
(function () {
3143231432
var isomorphicReactPackageVersion = React.version;
31433-
if ("19.2.0-www-classic-462d08f9-20250517" !== isomorphicReactPackageVersion)
31433+
if ("19.2.0-www-classic-c6c2a52a-20250519" !== isomorphicReactPackageVersion)
3143431434
throw Error(
3143531435
'Incompatible React versions: The "react" and "react-dom" packages must have the exact same version. Instead got:\n - react: ' +
3143631436
(isomorphicReactPackageVersion +
31437-
"\n - react-dom: 19.2.0-www-classic-462d08f9-20250517\nLearn more: https://react.dev/warnings/version-mismatch")
31437+
"\n - react-dom: 19.2.0-www-classic-c6c2a52a-20250519\nLearn more: https://react.dev/warnings/version-mismatch")
3143831438
);
3143931439
})();
3144031440
("function" === typeof Map &&
@@ -31477,10 +31477,10 @@ __DEV__ &&
3147731477
!(function () {
3147831478
var internals = {
3147931479
bundleType: 1,
31480-
version: "19.2.0-www-classic-462d08f9-20250517",
31480+
version: "19.2.0-www-classic-c6c2a52a-20250519",
3148131481
rendererPackageName: "react-dom",
3148231482
currentDispatcherRef: ReactSharedInternals,
31483-
reconcilerVersion: "19.2.0-www-classic-462d08f9-20250517"
31483+
reconcilerVersion: "19.2.0-www-classic-c6c2a52a-20250519"
3148431484
};
3148531485
internals.overrideHookState = overrideHookState;
3148631486
internals.overrideHookStateDeletePath = overrideHookStateDeletePath;
@@ -32080,7 +32080,7 @@ __DEV__ &&
3208032080
exports.useFormStatus = function () {
3208132081
return resolveDispatcher().useHostTransitionStatus();
3208232082
};
32083-
exports.version = "19.2.0-www-classic-462d08f9-20250517";
32083+
exports.version = "19.2.0-www-classic-c6c2a52a-20250519";
3208432084
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
3208532085
"function" ===
3208632086
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31216,11 +31216,11 @@ __DEV__ &&
3121631216
return_targetInst = null;
3121731217
(function () {
3121831218
var isomorphicReactPackageVersion = React.version;
31219-
if ("19.2.0-www-modern-462d08f9-20250517" !== isomorphicReactPackageVersion)
31219+
if ("19.2.0-www-modern-c6c2a52a-20250519" !== isomorphicReactPackageVersion)
3122031220
throw Error(
3122131221
'Incompatible React versions: The "react" and "react-dom" packages must have the exact same version. Instead got:\n - react: ' +
3122231222
(isomorphicReactPackageVersion +
31223-
"\n - react-dom: 19.2.0-www-modern-462d08f9-20250517\nLearn more: https://react.dev/warnings/version-mismatch")
31223+
"\n - react-dom: 19.2.0-www-modern-c6c2a52a-20250519\nLearn more: https://react.dev/warnings/version-mismatch")
3122431224
);
3122531225
})();
3122631226
("function" === typeof Map &&
@@ -31263,10 +31263,10 @@ __DEV__ &&
3126331263
!(function () {
3126431264
var internals = {
3126531265
bundleType: 1,
31266-
version: "19.2.0-www-modern-462d08f9-20250517",
31266+
version: "19.2.0-www-modern-c6c2a52a-20250519",
3126731267
rendererPackageName: "react-dom",
3126831268
currentDispatcherRef: ReactSharedInternals,
31269-
reconcilerVersion: "19.2.0-www-modern-462d08f9-20250517"
31269+
reconcilerVersion: "19.2.0-www-modern-c6c2a52a-20250519"
3127031270
};
3127131271
internals.overrideHookState = overrideHookState;
3127231272
internals.overrideHookStateDeletePath = overrideHookStateDeletePath;
@@ -31866,7 +31866,7 @@ __DEV__ &&
3186631866
exports.useFormStatus = function () {
3186731867
return resolveDispatcher().useHostTransitionStatus();
3186831868
};
31869-
exports.version = "19.2.0-www-modern-462d08f9-20250517";
31869+
exports.version = "19.2.0-www-modern-c6c2a52a-20250519";
3187031870
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
3187131871
"function" ===
3187231872
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19563,14 +19563,14 @@ function getCrossOriginStringAs(as, input) {
1956319563
}
1956419564
var isomorphicReactPackageVersion$jscomp$inline_2083 = React.version;
1956519565
if (
19566-
"19.2.0-www-classic-462d08f9-20250517" !==
19566+
"19.2.0-www-classic-c6c2a52a-20250519" !==
1956719567
isomorphicReactPackageVersion$jscomp$inline_2083
1956819568
)
1956919569
throw Error(
1957019570
formatProdErrorMessage(
1957119571
527,
1957219572
isomorphicReactPackageVersion$jscomp$inline_2083,
19573-
"19.2.0-www-classic-462d08f9-20250517"
19573+
"19.2.0-www-classic-c6c2a52a-20250519"
1957419574
)
1957519575
);
1957619576
Internals.findDOMNode = function (componentOrElement) {
@@ -19588,10 +19588,10 @@ Internals.Events = [
1958819588
];
1958919589
var internals$jscomp$inline_2696 = {
1959019590
bundleType: 0,
19591-
version: "19.2.0-www-classic-462d08f9-20250517",
19591+
version: "19.2.0-www-classic-c6c2a52a-20250519",
1959219592
rendererPackageName: "react-dom",
1959319593
currentDispatcherRef: ReactSharedInternals,
19594-
reconcilerVersion: "19.2.0-www-classic-462d08f9-20250517"
19594+
reconcilerVersion: "19.2.0-www-classic-c6c2a52a-20250519"
1959519595
};
1959619596
if ("undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__) {
1959719597
var hook$jscomp$inline_2697 = __REACT_DEVTOOLS_GLOBAL_HOOK__;
@@ -20003,4 +20003,4 @@ exports.useFormState = function (action, initialState, permalink) {
2000320003
exports.useFormStatus = function () {
2000420004
return ReactSharedInternals.H.useHostTransitionStatus();
2000520005
};
20006-
exports.version = "19.2.0-www-classic-462d08f9-20250517";
20006+
exports.version = "19.2.0-www-classic-c6c2a52a-20250519";

0 commit comments

Comments
 (0)