Skip to content

Commit a21e9d3

Browse files
committed
feat(compiler): implement constant folding for unary minus (#33140)
## Summary `-constant` is represented as a `UnaryExpression` node that is currently not part of constant folding. If the operand is a constant number, the node is folded to `constant * -1`. This also coerces `-0` to `0`, resulting in `0 === -0` being folded to `true`. ## How did you test this change? See attached tests DiffTrain build for [946da51](946da51)
1 parent 3eca8ac commit a21e9d3

35 files changed

+101
-86
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40272,6 +40272,21 @@ function evaluateInstruction(constants, instr) {
4027240272
}
4027340273
return null;
4027440274
}
40275+
case '-': {
40276+
const operand = read(constants, value.value);
40277+
if (operand !== null &&
40278+
operand.kind === 'Primitive' &&
40279+
typeof operand.value === 'number') {
40280+
const result = {
40281+
kind: 'Primitive',
40282+
value: operand.value * -1,
40283+
loc: value.loc,
40284+
};
40285+
instr.value = result;
40286+
return result;
40287+
}
40288+
return null;
40289+
}
4027540290
default:
4027640291
return null;
4027740292
}

compiled/facebook-www/REVISION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4206fe49825787eda57a5d142640a63772ccbf2b
1+
946da518eb2d64d808f9204a72e05892d3005f3f
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4206fe49825787eda57a5d142640a63772ccbf2b
1+
946da518eb2d64d808f9204a72e05892d3005f3f

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1538,7 +1538,7 @@ __DEV__ &&
15381538
exports.useTransition = function () {
15391539
return resolveDispatcher().useTransition();
15401540
};
1541-
exports.version = "19.2.0-www-classic-4206fe49-20250507";
1541+
exports.version = "19.2.0-www-classic-946da518-20250507";
15421542
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
15431543
"function" ===
15441544
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
@@ -1538,7 +1538,7 @@ __DEV__ &&
15381538
exports.useTransition = function () {
15391539
return resolveDispatcher().useTransition();
15401540
};
1541-
exports.version = "19.2.0-www-modern-4206fe49-20250507";
1541+
exports.version = "19.2.0-www-modern-946da518-20250507";
15421542
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
15431543
"function" ===
15441544
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
@@ -636,4 +636,4 @@ exports.useSyncExternalStore = function (
636636
exports.useTransition = function () {
637637
return ReactSharedInternals.H.useTransition();
638638
};
639-
exports.version = "19.2.0-www-classic-4206fe49-20250507";
639+
exports.version = "19.2.0-www-classic-946da518-20250507";

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,4 +636,4 @@ exports.useSyncExternalStore = function (
636636
exports.useTransition = function () {
637637
return ReactSharedInternals.H.useTransition();
638638
};
639-
exports.version = "19.2.0-www-modern-4206fe49-20250507";
639+
exports.version = "19.2.0-www-modern-946da518-20250507";

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ exports.useSyncExternalStore = function (
640640
exports.useTransition = function () {
641641
return ReactSharedInternals.H.useTransition();
642642
};
643-
exports.version = "19.2.0-www-classic-4206fe49-20250507";
643+
exports.version = "19.2.0-www-classic-946da518-20250507";
644644
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
645645
"function" ===
646646
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
@@ -640,7 +640,7 @@ exports.useSyncExternalStore = function (
640640
exports.useTransition = function () {
641641
return ReactSharedInternals.H.useTransition();
642642
};
643-
exports.version = "19.2.0-www-modern-4206fe49-20250507";
643+
exports.version = "19.2.0-www-modern-946da518-20250507";
644644
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
645645
"function" ===
646646
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
@@ -19032,10 +19032,10 @@ __DEV__ &&
1903219032
(function () {
1903319033
var internals = {
1903419034
bundleType: 1,
19035-
version: "19.2.0-www-classic-4206fe49-20250507",
19035+
version: "19.2.0-www-classic-946da518-20250507",
1903619036
rendererPackageName: "react-art",
1903719037
currentDispatcherRef: ReactSharedInternals,
19038-
reconcilerVersion: "19.2.0-www-classic-4206fe49-20250507"
19038+
reconcilerVersion: "19.2.0-www-classic-946da518-20250507"
1903919039
};
1904019040
internals.overrideHookState = overrideHookState;
1904119041
internals.overrideHookStateDeletePath = overrideHookStateDeletePath;
@@ -19069,7 +19069,7 @@ __DEV__ &&
1906919069
exports.Shape = Shape;
1907019070
exports.Surface = Surface;
1907119071
exports.Text = Text;
19072-
exports.version = "19.2.0-www-classic-4206fe49-20250507";
19072+
exports.version = "19.2.0-www-classic-946da518-20250507";
1907319073
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
1907419074
"function" ===
1907519075
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

0 commit comments

Comments
 (0)