Skip to content

Commit c157c76

Browse files
committed
[compiler] Fix false positive for useMemo reassigning context vars (#34904)
Within a function expression local variables may use StoreContext for local context variables, so the reassignment check here was firing too often. We should only report an error for variables that are declared outside the function, ie part of its `context`. --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/34904). * #34903 * __->__ #34904 DiffTrain build for [2bcbf25](2bcbf25)
1 parent b0c5930 commit c157c76

35 files changed

+99
-96
lines changed

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50435,21 +50435,24 @@ function validateUseMemo(fn) {
5043550435
return errors.asResult();
5043650436
}
5043750437
function validateNoContextVariableAssignment(fn, errors) {
50438+
const context = new Set(fn.context.map(place => place.identifier.id));
5043850439
for (const block of fn.body.blocks.values()) {
5043950440
for (const instr of block.instructions) {
5044050441
const value = instr.value;
5044150442
switch (value.kind) {
5044250443
case 'StoreContext': {
50443-
errors.pushDiagnostic(CompilerDiagnostic.create({
50444-
category: ErrorCategory.UseMemo,
50445-
reason: 'useMemo() callbacks may not reassign variables declared outside of the callback',
50446-
description: 'useMemo() callbacks must be pure functions and cannot reassign variables defined outside of the callback function',
50447-
suggestions: null,
50448-
}).withDetails({
50449-
kind: 'error',
50450-
loc: value.lvalue.place.loc,
50451-
message: 'Cannot reassign variable',
50452-
}));
50444+
if (context.has(value.lvalue.place.identifier.id)) {
50445+
errors.pushDiagnostic(CompilerDiagnostic.create({
50446+
category: ErrorCategory.UseMemo,
50447+
reason: 'useMemo() callbacks may not reassign variables declared outside of the callback',
50448+
description: 'useMemo() callbacks must be pure functions and cannot reassign variables defined outside of the callback function',
50449+
suggestions: null,
50450+
}).withDetails({
50451+
kind: 'error',
50452+
loc: value.lvalue.place.loc,
50453+
message: 'Cannot reassign variable',
50454+
}));
50455+
}
5045350456
break;
5045450457
}
5045550458
}

compiled/facebook-www/REVISION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
58bdc0bb967098f14562cd76af0668f2056459a0
1+
2bcbf254f168ddec567156f802d19315e64e4aa8
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
58bdc0bb967098f14562cd76af0668f2056459a0
1+
2bcbf254f168ddec567156f802d19315e64e4aa8

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1499,7 +1499,7 @@ __DEV__ &&
14991499
exports.useTransition = function () {
15001500
return resolveDispatcher().useTransition();
15011501
};
1502-
exports.version = "19.3.0-www-classic-58bdc0bb-20251019";
1502+
exports.version = "19.3.0-www-classic-2bcbf254-20251020";
15031503
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
15041504
"function" ===
15051505
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
@@ -1499,7 +1499,7 @@ __DEV__ &&
14991499
exports.useTransition = function () {
15001500
return resolveDispatcher().useTransition();
15011501
};
1502-
exports.version = "19.3.0-www-modern-58bdc0bb-20251019";
1502+
exports.version = "19.3.0-www-modern-2bcbf254-20251020";
15031503
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
15041504
"function" ===
15051505
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
@@ -606,4 +606,4 @@ exports.useSyncExternalStore = function (
606606
exports.useTransition = function () {
607607
return ReactSharedInternals.H.useTransition();
608608
};
609-
exports.version = "19.3.0-www-classic-58bdc0bb-20251019";
609+
exports.version = "19.3.0-www-classic-2bcbf254-20251020";

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,4 +606,4 @@ exports.useSyncExternalStore = function (
606606
exports.useTransition = function () {
607607
return ReactSharedInternals.H.useTransition();
608608
};
609-
exports.version = "19.3.0-www-modern-58bdc0bb-20251019";
609+
exports.version = "19.3.0-www-modern-2bcbf254-20251020";

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ exports.useSyncExternalStore = function (
610610
exports.useTransition = function () {
611611
return ReactSharedInternals.H.useTransition();
612612
};
613-
exports.version = "19.3.0-www-classic-58bdc0bb-20251019";
613+
exports.version = "19.3.0-www-classic-2bcbf254-20251020";
614614
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
615615
"function" ===
616616
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
@@ -610,7 +610,7 @@ exports.useSyncExternalStore = function (
610610
exports.useTransition = function () {
611611
return ReactSharedInternals.H.useTransition();
612612
};
613-
exports.version = "19.3.0-www-modern-58bdc0bb-20251019";
613+
exports.version = "19.3.0-www-modern-2bcbf254-20251020";
614614
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
615615
"function" ===
616616
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
@@ -20371,10 +20371,10 @@ __DEV__ &&
2037120371
(function () {
2037220372
var internals = {
2037320373
bundleType: 1,
20374-
version: "19.3.0-www-classic-58bdc0bb-20251019",
20374+
version: "19.3.0-www-classic-2bcbf254-20251020",
2037520375
rendererPackageName: "react-art",
2037620376
currentDispatcherRef: ReactSharedInternals,
20377-
reconcilerVersion: "19.3.0-www-classic-58bdc0bb-20251019"
20377+
reconcilerVersion: "19.3.0-www-classic-2bcbf254-20251020"
2037820378
};
2037920379
internals.overrideHookState = overrideHookState;
2038020380
internals.overrideHookStateDeletePath = overrideHookStateDeletePath;
@@ -20409,7 +20409,7 @@ __DEV__ &&
2040920409
exports.Shape = Shape;
2041020410
exports.Surface = Surface;
2041120411
exports.Text = Text;
20412-
exports.version = "19.3.0-www-classic-58bdc0bb-20251019";
20412+
exports.version = "19.3.0-www-classic-2bcbf254-20251020";
2041320413
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
2041420414
"function" ===
2041520415
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

0 commit comments

Comments
 (0)