Skip to content

Commit

Permalink
Avoid meta programming to initialize functions in module scope (#26388)
Browse files Browse the repository at this point in the history
I'm trying to get rid of all meta programming in the module scope so
that closure can do a better job figuring out cyclic dependencies and
ability to reorder.

This is converting a lot of the patterns that assign functions
conditionally to using function declarations instead.

```
let fn;
if (__DEV__) {
  fn = function() {
    ...
  };
}
```
->
```
function fn() {
  if (__DEV__) {
    ...
  }
}
```

DiffTrain build for [d310d65](d310d65)
  • Loading branch information
sebmarkbage committed Mar 15, 2023
1 parent 18d3ea0 commit 9b26db0
Show file tree
Hide file tree
Showing 25 changed files with 2,392 additions and 2,499 deletions.
4 changes: 2 additions & 2 deletions compiled/facebook-www/JSXDEVRuntime-dev.classic.js
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,7 @@ function defineRefPropWarningGetter(props, displayName) {
* @internal
*/

var ReactElement = function (type, key, ref, self, source, owner, props) {
function ReactElement(type, key, ref, self, source, owner, props) {
var element = {
// This tag allows us to uniquely identify this as a React Element
$$typeof: REACT_ELEMENT_TYPE,
Expand Down Expand Up @@ -993,7 +993,7 @@ var ReactElement = function (type, key, ref, self, source, owner, props) {
}

return element;
};
}
/**
* https://github.com/reactjs/rfcs/pull/107
* @param {*} type
Expand Down
4 changes: 2 additions & 2 deletions compiled/facebook-www/JSXDEVRuntime-dev.modern.js
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,7 @@ function defineRefPropWarningGetter(props, displayName) {
* @internal
*/

var ReactElement = function (type, key, ref, self, source, owner, props) {
function ReactElement(type, key, ref, self, source, owner, props) {
var element = {
// This tag allows us to uniquely identify this as a React Element
$$typeof: REACT_ELEMENT_TYPE,
Expand Down Expand Up @@ -993,7 +993,7 @@ var ReactElement = function (type, key, ref, self, source, owner, props) {
}

return element;
};
}
/**
* https://github.com/reactjs/rfcs/pull/107
* @param {*} type
Expand Down
2 changes: 1 addition & 1 deletion compiled/facebook-www/REVISION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6bd53a5bdfe24d9a70819c4eba40b488e62900cd
d310d654a7c7aab6c8213da84ef36dfba82711b0
10 changes: 5 additions & 5 deletions compiled/facebook-www/React-dev.classic.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ if (
}
"use strict";

var ReactVersion = "18.3.0-www-classic-96f33629";
var ReactVersion = "18.3.0-www-classic-e762d1a9";

// ATTENTION
// When adding new symbols to this file,
Expand Down Expand Up @@ -756,7 +756,7 @@ function warnIfStringRefCannotBeAutoConverted$1(config) {
* @internal
*/

var ReactElement$1 = function (type, key, ref, self, source, owner, props) {
function ReactElement$1(type, key, ref, self, source, owner, props) {
var element = {
// This tag allows us to uniquely identify this as a React Element
$$typeof: REACT_ELEMENT_TYPE,
Expand Down Expand Up @@ -808,7 +808,7 @@ var ReactElement$1 = function (type, key, ref, self, source, owner, props) {
}

return element;
};
}
/**
* Create and return a new ReactElement of the given type.
* See https://reactjs.org/docs/react-api.html#createelement
Expand Down Expand Up @@ -3574,7 +3574,7 @@ function defineRefPropWarningGetter(props, displayName) {
* @internal
*/

var ReactElement = function (type, key, ref, self, source, owner, props) {
function ReactElement(type, key, ref, self, source, owner, props) {
var element = {
// This tag allows us to uniquely identify this as a React Element
$$typeof: REACT_ELEMENT_TYPE,
Expand Down Expand Up @@ -3626,7 +3626,7 @@ var ReactElement = function (type, key, ref, self, source, owner, props) {
}

return element;
};
}
/**
* https://github.com/reactjs/rfcs/pull/107
* @param {*} type
Expand Down
10 changes: 5 additions & 5 deletions compiled/facebook-www/React-dev.modern.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ if (
}
"use strict";

var ReactVersion = "18.3.0-www-modern-724bb919";
var ReactVersion = "18.3.0-www-modern-9bafe2f6";

// ATTENTION
// When adding new symbols to this file,
Expand Down Expand Up @@ -756,7 +756,7 @@ function warnIfStringRefCannotBeAutoConverted$1(config) {
* @internal
*/

var ReactElement$1 = function (type, key, ref, self, source, owner, props) {
function ReactElement$1(type, key, ref, self, source, owner, props) {
var element = {
// This tag allows us to uniquely identify this as a React Element
$$typeof: REACT_ELEMENT_TYPE,
Expand Down Expand Up @@ -808,7 +808,7 @@ var ReactElement$1 = function (type, key, ref, self, source, owner, props) {
}

return element;
};
}
/**
* Create and return a new ReactElement of the given type.
* See https://reactjs.org/docs/react-api.html#createelement
Expand Down Expand Up @@ -3539,7 +3539,7 @@ function defineRefPropWarningGetter(props, displayName) {
* @internal
*/

var ReactElement = function (type, key, ref, self, source, owner, props) {
function ReactElement(type, key, ref, self, source, owner, props) {
var element = {
// This tag allows us to uniquely identify this as a React Element
$$typeof: REACT_ELEMENT_TYPE,
Expand Down Expand Up @@ -3591,7 +3591,7 @@ var ReactElement = function (type, key, ref, self, source, owner, props) {
}

return element;
};
}
/**
* https://github.com/reactjs/rfcs/pull/107
* @param {*} type
Expand Down
79 changes: 36 additions & 43 deletions compiled/facebook-www/ReactART-dev.classic.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function _assertThisInitialized(self) {
return self;
}

var ReactVersion = "18.3.0-www-classic-5ea6b0b1";
var ReactVersion = "18.3.0-www-classic-d10ee72f";

var LegacyRoot = 0;
var ConcurrentRoot = 1;
Expand Down Expand Up @@ -10978,11 +10978,10 @@ var didWarnAboutUninitializedState;
var didWarnAboutGetSnapshotBeforeUpdateWithoutDidUpdate;
var didWarnAboutLegacyLifecyclesAndDerivedState;
var didWarnAboutUndefinedDerivedState;
var warnOnUndefinedDerivedState;
var warnOnInvalidCallback;
var didWarnAboutDirectlyAssigningPropsToState;
var didWarnAboutContextTypeAndContextTypes;
var didWarnAboutInvalidateContextType;
var didWarnOnInvalidCallback;

{
didWarnAboutStateAssignmentForComponent = new Set();
Expand All @@ -10993,9 +10992,30 @@ var didWarnAboutInvalidateContextType;
didWarnAboutUndefinedDerivedState = new Set();
didWarnAboutContextTypeAndContextTypes = new Set();
didWarnAboutInvalidateContextType = new Set();
var didWarnOnInvalidCallback = new Set();
didWarnOnInvalidCallback = new Set(); // This is so gross but it's at least non-critical and can be removed if
// it causes problems. This is meant to give a nicer error message for
// ReactDOM15.unstable_renderSubtreeIntoContainer(reactDOM16Component,
// ...)) which otherwise throws a "_processChildContext is not a function"
// exception.

Object.defineProperty(fakeInternalInstance, "_processChildContext", {
enumerable: false,
value: function () {
throw new Error(
"_processChildContext is not available in React 16+. This likely " +
"means you have multiple copies of React and are attempting to nest " +
"a React 15 tree inside a React 16 tree using " +
"unstable_renderSubtreeIntoContainer, which isn't supported. Try " +
"to make sure you have only one copy of React (and ideally, switch " +
"to ReactDOM.createPortal)."
);
}
});
Object.freeze(fakeInternalInstance);
}

warnOnInvalidCallback = function (callback, callerName) {
function warnOnInvalidCallback(callback, callerName) {
{
if (callback === null || typeof callback === "function") {
return;
}
Expand All @@ -11012,9 +11032,11 @@ var didWarnAboutInvalidateContextType;
callback
);
}
};
}
}

warnOnUndefinedDerivedState = function (type, partialState) {
function warnOnUndefinedDerivedState(type, partialState) {
{
if (partialState === undefined) {
var componentName = getComponentNameFromType(type) || "Component";

Expand All @@ -11028,26 +11050,7 @@ var didWarnAboutInvalidateContextType;
);
}
}
}; // This is so gross but it's at least non-critical and can be removed if
// it causes problems. This is meant to give a nicer error message for
// ReactDOM15.unstable_renderSubtreeIntoContainer(reactDOM16Component,
// ...)) which otherwise throws a "_processChildContext is not a function"
// exception.

Object.defineProperty(fakeInternalInstance, "_processChildContext", {
enumerable: false,
value: function () {
throw new Error(
"_processChildContext is not available in React 16+. This likely " +
"means you have multiple copies of React and are attempting to nest " +
"a React 15 tree inside a React 16 tree using " +
"unstable_renderSubtreeIntoContainer, which isn't supported. Try " +
"to make sure you have only one copy of React (and ideally, switch " +
"to ReactDOM.createPortal)."
);
}
});
Object.freeze(fakeInternalInstance);
}
}

function applyDerivedStateFromProps(
Expand Down Expand Up @@ -18854,20 +18857,10 @@ if (typeof ReactFbErrorUtils.invokeGuardedCallback !== "function") {
);
}

var invokeGuardedCallbackImpl = function (
name,
func,
context,
a,
b,
c,
d,
e,
f
) {
function invokeGuardedCallbackImpl(name, func, context, a, b, c, d, e, f) {
// This will call `this.onError(err)` if an error was caught.
ReactFbErrorUtils.invokeGuardedCallback.apply(this, arguments);
};
}

var hasError = false;
var caughtError = null; // Used by event system to capture/rethrow the first error.
Expand Down Expand Up @@ -18949,7 +18942,7 @@ function reportUncaughtErrorInDEV(error) {
}
}

var callComponentWillUnmountWithTimer = function (current, instance) {
function callComponentWillUnmountWithTimer(current, instance) {
instance.props = current.memoizedProps;
instance.state = current.memoizedState;

Expand All @@ -18963,7 +18956,7 @@ var callComponentWillUnmountWithTimer = function (current, instance) {
} else {
instance.componentWillUnmount();
}
}; // Capture errors so they don't interrupt unmounting.
} // Capture errors so they don't interrupt unmounting.

function safelyCallComponentWillUnmount(
current,
Expand Down Expand Up @@ -26923,10 +26916,10 @@ function FiberNode(tag, pendingProps, key, mode) {
// 5) It should be easy to port this to a C struct and keep a C implementation
// compatible.

var createFiber = function (tag, pendingProps, key, mode) {
function createFiber(tag, pendingProps, key, mode) {
// $FlowFixMe: the shapes are exact here but Flow doesn't like constructors
return new FiberNode(tag, pendingProps, key, mode);
};
}

function shouldConstruct(Component) {
var prototype = Component.prototype;
Expand Down
Loading

0 comments on commit 9b26db0

Please sign in to comment.