Skip to content

Commit d310d65

Browse files
authored
Avoid meta programming to initialize functions in module scope (#26388)
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__) { ... } } ```
1 parent 21aee59 commit d310d65

24 files changed

+628
-652
lines changed

packages/dom-event-testing-library/domEnvironment.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* Change environment support for PointerEvent.
1414
*/
1515

16-
const emptyFunction = function () {};
16+
function emptyFunction() {}
1717

1818
export function hasPointerEvent() {
1919
return global != null && global.PointerEvent != null;

packages/react-dom-bindings/src/client/ReactDOMComponent.js

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,8 @@ const HTML = '__html';
9494
let warnedUnknownTags: {
9595
[key: string]: boolean,
9696
};
97-
98-
let validatePropertiesInDevelopment;
99-
let warnForPropDifference;
100-
let warnForExtraAttributes;
101-
let warnForInvalidEventListener;
10297
let canDiffStyleForHydrationWarning;
10398

104-
let normalizeHTML;
105-
10699
if (__DEV__) {
107100
warnedUnknownTags = {
108101
// There are working polyfills for <dialog>. Let people use it.
@@ -115,15 +108,6 @@ if (__DEV__) {
115108
webview: true,
116109
};
117110

118-
validatePropertiesInDevelopment = function (type: string, props: any) {
119-
validateARIAProperties(type, props);
120-
validateInputProperties(type, props);
121-
validateUnknownProperties(type, props, {
122-
registrationNameDependencies,
123-
possibleRegistrationNames,
124-
});
125-
};
126-
127111
// IE 11 parses & normalizes the style attribute as opposed to other
128112
// browsers. It adds spaces and sorts the properties in some
129113
// non-alphabetical order. Handling that would require sorting CSS
@@ -133,12 +117,25 @@ if (__DEV__) {
133117
// in that browser completely in favor of doing all that work.
134118
// See https://github.com/facebook/react/issues/11807
135119
canDiffStyleForHydrationWarning = canUseDOM && !document.documentMode;
120+
}
136121

137-
warnForPropDifference = function (
138-
propName: string,
139-
serverValue: mixed,
140-
clientValue: mixed,
141-
) {
122+
function validatePropertiesInDevelopment(type: string, props: any) {
123+
if (__DEV__) {
124+
validateARIAProperties(type, props);
125+
validateInputProperties(type, props);
126+
validateUnknownProperties(type, props, {
127+
registrationNameDependencies,
128+
possibleRegistrationNames,
129+
});
130+
}
131+
}
132+
133+
function warnForPropDifference(
134+
propName: string,
135+
serverValue: mixed,
136+
clientValue: mixed,
137+
) {
138+
if (__DEV__) {
142139
if (didWarnInvalidHydration) {
143140
return;
144141
}
@@ -156,9 +153,11 @@ if (__DEV__) {
156153
JSON.stringify(normalizedServerValue),
157154
JSON.stringify(normalizedClientValue),
158155
);
159-
};
156+
}
157+
}
160158

161-
warnForExtraAttributes = function (attributeNames: Set<string>) {
159+
function warnForExtraAttributes(attributeNames: Set<string>) {
160+
if (__DEV__) {
162161
if (didWarnInvalidHydration) {
163162
return;
164163
}
@@ -168,12 +167,11 @@ if (__DEV__) {
168167
names.push(name);
169168
});
170169
console.error('Extra attributes from the server: %s', names);
171-
};
170+
}
171+
}
172172

173-
warnForInvalidEventListener = function (
174-
registrationName: string,
175-
listener: any,
176-
) {
173+
function warnForInvalidEventListener(registrationName: string, listener: any) {
174+
if (__DEV__) {
177175
if (listener === false) {
178176
console.error(
179177
'Expected `%s` listener to be a function, instead got `false`.\n\n' +
@@ -190,11 +188,13 @@ if (__DEV__) {
190188
typeof listener,
191189
);
192190
}
193-
};
191+
}
192+
}
194193

195-
// Parse the HTML and read it back to normalize the HTML string so that it
196-
// can be used for comparison.
197-
normalizeHTML = function (parent: Element, html: string) {
194+
// Parse the HTML and read it back to normalize the HTML string so that it
195+
// can be used for comparison.
196+
function normalizeHTML(parent: Element, html: string) {
197+
if (__DEV__) {
198198
// We could have created a separate document here to avoid
199199
// re-initializing custom elements if they exist. But this breaks
200200
// how <noscript> is being handled. So we use the same document.
@@ -208,7 +208,7 @@ if (__DEV__) {
208208
);
209209
testElement.innerHTML = html;
210210
return testElement.innerHTML;
211-
};
211+
}
212212
}
213213

214214
// HTML parsing normalizes CR and CRLF to LF.

packages/react-dom-bindings/src/client/createMicrosoftUnsafeLocalFunction.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

packages/react-dom-bindings/src/client/setInnerHTML.js

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,15 @@
77
* @flow
88
*/
99

10+
/* globals MSApp */
11+
1012
import {SVG_NAMESPACE} from './DOMNamespaces';
11-
import createMicrosoftUnsafeLocalFunction from './createMicrosoftUnsafeLocalFunction';
1213
import {enableTrustedTypesIntegration} from 'shared/ReactFeatureFlags';
1314

1415
// SVG temp container for IE lacking innerHTML
1516
let reusableSVGContainer: HTMLElement;
1617

17-
/**
18-
* Set the innerHTML property of a node
19-
*
20-
* @param {DOMElement} node
21-
* @param {string} html
22-
* @internal
23-
*/
24-
const setInnerHTML: (
25-
node: Element,
26-
html: {valueOf(): {toString(): string, ...}, ...},
27-
) => void = createMicrosoftUnsafeLocalFunction(function (
18+
function setInnerHTMLImpl(
2819
node: Element,
2920
html: {valueOf(): {toString(): string, ...}, ...},
3021
): void {
@@ -66,6 +57,26 @@ const setInnerHTML: (
6657
}
6758
}
6859
node.innerHTML = (html: any);
69-
});
60+
}
61+
62+
let setInnerHTML: (
63+
node: Element,
64+
html: {valueOf(): {toString(): string, ...}, ...},
65+
) => void = setInnerHTMLImpl;
66+
// $FlowFixMe[cannot-resolve-name]
67+
if (typeof MSApp !== 'undefined' && MSApp.execUnsafeLocalFunction) {
68+
/**
69+
* Create a function which has 'unsafe' privileges (required by windows8 apps)
70+
*/
71+
setInnerHTML = function (
72+
node: Element,
73+
html: {valueOf(): {toString(): string, ...}, ...},
74+
): void {
75+
// $FlowFixMe[cannot-resolve-name]
76+
return MSApp.execUnsafeLocalFunction(function () {
77+
return setInnerHTMLImpl(node, html);
78+
});
79+
};
80+
}
7081

7182
export default setInnerHTML;

packages/react-dom-bindings/src/client/setTextContent.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {TEXT_NODE} from './HTMLNodeType';
1818
* @param {string} text
1919
* @internal
2020
*/
21-
const setTextContent = function (node: Element, text: string): void {
21+
function setTextContent(node: Element, text: string): void {
2222
if (text) {
2323
const firstChild = node.firstChild;
2424

@@ -32,6 +32,6 @@ const setTextContent = function (node: Element, text: string): void {
3232
}
3333
}
3434
node.textContent = text;
35-
};
35+
}
3636

3737
export default setTextContent;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy