Skip to content

Commit

Permalink
[string-refs] make disableStringRefs a dynamic www flag (#31175)
Browse files Browse the repository at this point in the history
DiffTrain build for [75dd053](75dd053)
  • Loading branch information
kassens committed Oct 14, 2024
1 parent 0ccec15 commit c1210a1
Show file tree
Hide file tree
Showing 37 changed files with 905 additions and 714 deletions.
93 changes: 52 additions & 41 deletions compiled/facebook-www/JSXDEVRuntime-dev.classic.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,8 @@ __DEV__ &&
}
function warnIfStringRefCannotBeAutoConverted(config, self) {
var owner;
"string" === typeof config.ref &&
!disableStringRefs &&
"string" === typeof config.ref &&
(owner = getOwner()) &&
self &&
owner.stateNode !== self &&
Expand Down Expand Up @@ -639,18 +640,23 @@ __DEV__ &&
(checkKeyStringCoercion(maybeKey), (children = "" + maybeKey));
hasValidKey(config) &&
(checkKeyStringCoercion(config.key), (children = "" + config.key));
hasValidRef(config) && warnIfStringRefCannotBeAutoConverted(config, self);
if ("ref" in config || "key" in config) {
hasValidRef(config) &&
(disableStringRefs ||
warnIfStringRefCannotBeAutoConverted(config, self));
if (
(!enableFastJSXWithoutStringRefs && "ref" in config) ||
"key" in config
) {
maybeKey = {};
for (var propName in config)
"key" !== propName &&
("ref" === propName
? (maybeKey.ref = coerceStringRef(
(disableStringRefs || "ref" !== propName
? (maybeKey[propName] = config[propName])
: (maybeKey.ref = coerceStringRef(
config[propName],
getOwner(),
type
))
: (maybeKey[propName] = config[propName]));
)));
} else maybeKey = config;
if (!disableDefaultPropsExceptForClasses && type && type.defaultProps) {
config = type.defaultProps;
Expand Down Expand Up @@ -756,6 +762,7 @@ __DEV__ &&
return info;
}
function coerceStringRef(mixedRef, owner, type) {
if (disableStringRefs) return mixedRef;
if ("string" !== typeof mixedRef)
if ("number" === typeof mixedRef || "boolean" === typeof mixedRef)
willCoercionThrow(mixedRef) &&
Expand All @@ -774,44 +781,47 @@ __DEV__ &&
return callback;
}
function stringRefAsCallbackRef(stringRef, type, owner, value) {
if (!owner)
throw Error(
"Element ref was specified as a string (" +
stringRef +
") but no owner was set. This could happen for one of the following reasons:\n1. You may be adding a ref to a function component\n2. You may be adding a ref to a component that was not created inside a component's render method\n3. You have multiple copies of React loaded\nSee https://react.dev/link/refs-must-have-owner for more information."
);
if (1 !== owner.tag)
throw Error(
"Function components cannot have string refs. We recommend using useRef() instead. Learn more about using refs safely here: https://react.dev/link/strict-mode-string-ref"
);
if (
"function" !== typeof type ||
(type.prototype && type.prototype.isReactComponent)
)
(type = getComponentNameFromFiber(owner) || "Component"),
didWarnAboutStringRefs[type] ||
(enableLogStringRefsProd &&
enableLogStringRefsProd(type, stringRef),
error(
'Component "%s" contains the string ref "%s". Support for string refs will be removed in a future major release. We recommend using useRef() or createRef() instead. Learn more about using refs safely here: https://react.dev/link/strict-mode-string-ref',
type,
stringRef
),
(didWarnAboutStringRefs[type] = !0));
owner = owner.stateNode;
if (!owner)
throw Error(
"Missing owner for string ref " +
stringRef +
". This error is likely caused by a bug in React. Please file an issue."
);
owner = owner.refs;
null === value ? delete owner[stringRef] : (owner[stringRef] = value);
if (!disableStringRefs) {
if (!owner)
throw Error(
"Element ref was specified as a string (" +
stringRef +
") but no owner was set. This could happen for one of the following reasons:\n1. You may be adding a ref to a function component\n2. You may be adding a ref to a component that was not created inside a component's render method\n3. You have multiple copies of React loaded\nSee https://react.dev/link/refs-must-have-owner for more information."
);
if (1 !== owner.tag)
throw Error(
"Function components cannot have string refs. We recommend using useRef() instead. Learn more about using refs safely here: https://react.dev/link/strict-mode-string-ref"
);
if (
"function" !== typeof type ||
(type.prototype && type.prototype.isReactComponent)
)
(type = getComponentNameFromFiber(owner) || "Component"),
didWarnAboutStringRefs[type] ||
(enableLogStringRefsProd &&
enableLogStringRefsProd(type, stringRef),
error(
'Component "%s" contains the string ref "%s". Support for string refs will be removed in a future major release. We recommend using useRef() or createRef() instead. Learn more about using refs safely here: https://react.dev/link/strict-mode-string-ref',
type,
stringRef
),
(didWarnAboutStringRefs[type] = !0));
owner = owner.stateNode;
if (!owner)
throw Error(
"Missing owner for string ref " +
stringRef +
". This error is likely caused by a bug in React. Please file an issue."
);
owner = owner.refs;
null === value ? delete owner[stringRef] : (owner[stringRef] = value);
}
}
var React = require("react"),
dynamicFeatureFlags = require("ReactFeatureFlags"),
disableDefaultPropsExceptForClasses =
dynamicFeatureFlags.disableDefaultPropsExceptForClasses,
disableStringRefs = dynamicFeatureFlags.disableStringRefs,
enableDebugTracing = dynamicFeatureFlags.enableDebugTracing,
enableLogStringRefsProd = dynamicFeatureFlags.enableLogStringRefsProd,
enableRenderableContext = dynamicFeatureFlags.enableRenderableContext,
Expand Down Expand Up @@ -867,7 +877,8 @@ __DEV__ &&
specialPropKeyWarningShown;
var didWarnAboutStringRefs = {};
var didWarnAboutElementRef = {};
var didWarnAboutKeySpread = {},
var enableFastJSXWithoutStringRefs = disableStringRefs,
didWarnAboutKeySpread = {},
ownerHasKeyUseWarning = {};
exports.Fragment = REACT_FRAGMENT_TYPE;
exports.jsxDEV = function (
Expand Down
93 changes: 52 additions & 41 deletions compiled/facebook-www/JSXDEVRuntime-dev.modern.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,8 @@ __DEV__ &&
}
function warnIfStringRefCannotBeAutoConverted(config, self) {
var owner;
"string" === typeof config.ref &&
!disableStringRefs &&
"string" === typeof config.ref &&
(owner = getOwner()) &&
self &&
owner.stateNode !== self &&
Expand Down Expand Up @@ -636,18 +637,23 @@ __DEV__ &&
(checkKeyStringCoercion(maybeKey), (children = "" + maybeKey));
hasValidKey(config) &&
(checkKeyStringCoercion(config.key), (children = "" + config.key));
hasValidRef(config) && warnIfStringRefCannotBeAutoConverted(config, self);
if ("ref" in config || "key" in config) {
hasValidRef(config) &&
(disableStringRefs ||
warnIfStringRefCannotBeAutoConverted(config, self));
if (
(!enableFastJSXWithoutStringRefs && "ref" in config) ||
"key" in config
) {
maybeKey = {};
for (var propName in config)
"key" !== propName &&
("ref" === propName
? (maybeKey.ref = coerceStringRef(
(disableStringRefs || "ref" !== propName
? (maybeKey[propName] = config[propName])
: (maybeKey.ref = coerceStringRef(
config[propName],
getOwner(),
type
))
: (maybeKey[propName] = config[propName]));
)));
} else maybeKey = config;
if (!disableDefaultPropsExceptForClasses && type && type.defaultProps) {
config = type.defaultProps;
Expand Down Expand Up @@ -753,6 +759,7 @@ __DEV__ &&
return info;
}
function coerceStringRef(mixedRef, owner, type) {
if (disableStringRefs) return mixedRef;
if ("string" !== typeof mixedRef)
if ("number" === typeof mixedRef || "boolean" === typeof mixedRef)
willCoercionThrow(mixedRef) &&
Expand All @@ -771,44 +778,47 @@ __DEV__ &&
return callback;
}
function stringRefAsCallbackRef(stringRef, type, owner, value) {
if (!owner)
throw Error(
"Element ref was specified as a string (" +
stringRef +
") but no owner was set. This could happen for one of the following reasons:\n1. You may be adding a ref to a function component\n2. You may be adding a ref to a component that was not created inside a component's render method\n3. You have multiple copies of React loaded\nSee https://react.dev/link/refs-must-have-owner for more information."
);
if (1 !== owner.tag)
throw Error(
"Function components cannot have string refs. We recommend using useRef() instead. Learn more about using refs safely here: https://react.dev/link/strict-mode-string-ref"
);
if (
"function" !== typeof type ||
(type.prototype && type.prototype.isReactComponent)
)
(type = getComponentNameFromFiber(owner) || "Component"),
didWarnAboutStringRefs[type] ||
(enableLogStringRefsProd &&
enableLogStringRefsProd(type, stringRef),
error(
'Component "%s" contains the string ref "%s". Support for string refs will be removed in a future major release. We recommend using useRef() or createRef() instead. Learn more about using refs safely here: https://react.dev/link/strict-mode-string-ref',
type,
stringRef
),
(didWarnAboutStringRefs[type] = !0));
owner = owner.stateNode;
if (!owner)
throw Error(
"Missing owner for string ref " +
stringRef +
". This error is likely caused by a bug in React. Please file an issue."
);
owner = owner.refs;
null === value ? delete owner[stringRef] : (owner[stringRef] = value);
if (!disableStringRefs) {
if (!owner)
throw Error(
"Element ref was specified as a string (" +
stringRef +
") but no owner was set. This could happen for one of the following reasons:\n1. You may be adding a ref to a function component\n2. You may be adding a ref to a component that was not created inside a component's render method\n3. You have multiple copies of React loaded\nSee https://react.dev/link/refs-must-have-owner for more information."
);
if (1 !== owner.tag)
throw Error(
"Function components cannot have string refs. We recommend using useRef() instead. Learn more about using refs safely here: https://react.dev/link/strict-mode-string-ref"
);
if (
"function" !== typeof type ||
(type.prototype && type.prototype.isReactComponent)
)
(type = getComponentNameFromFiber(owner) || "Component"),
didWarnAboutStringRefs[type] ||
(enableLogStringRefsProd &&
enableLogStringRefsProd(type, stringRef),
error(
'Component "%s" contains the string ref "%s". Support for string refs will be removed in a future major release. We recommend using useRef() or createRef() instead. Learn more about using refs safely here: https://react.dev/link/strict-mode-string-ref',
type,
stringRef
),
(didWarnAboutStringRefs[type] = !0));
owner = owner.stateNode;
if (!owner)
throw Error(
"Missing owner for string ref " +
stringRef +
". This error is likely caused by a bug in React. Please file an issue."
);
owner = owner.refs;
null === value ? delete owner[stringRef] : (owner[stringRef] = value);
}
}
var React = require("react"),
dynamicFeatureFlags = require("ReactFeatureFlags"),
disableDefaultPropsExceptForClasses =
dynamicFeatureFlags.disableDefaultPropsExceptForClasses,
disableStringRefs = dynamicFeatureFlags.disableStringRefs,
enableDebugTracing = dynamicFeatureFlags.enableDebugTracing,
enableLogStringRefsProd = dynamicFeatureFlags.enableLogStringRefsProd,
enableRenderableContext = dynamicFeatureFlags.enableRenderableContext,
Expand Down Expand Up @@ -863,7 +873,8 @@ __DEV__ &&
specialPropKeyWarningShown;
var didWarnAboutStringRefs = {};
var didWarnAboutElementRef = {};
var didWarnAboutKeySpread = {},
var enableFastJSXWithoutStringRefs = disableStringRefs,
didWarnAboutKeySpread = {},
ownerHasKeyUseWarning = {};
exports.Fragment = REACT_FRAGMENT_TYPE;
exports.jsxDEV = function (
Expand Down
2 changes: 1 addition & 1 deletion compiled/facebook-www/REVISION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5636fad840942cfea80301d91e931a50c6370d19
75dd053b5e83e8ae20e9f771bca7b95dba4ff881
2 changes: 1 addition & 1 deletion compiled/facebook-www/REVISION_TRANSFORMS
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5636fad840942cfea80301d91e931a50c6370d19
75dd053b5e83e8ae20e9f771bca7b95dba4ff881
Loading

0 comments on commit c1210a1

Please sign in to comment.