From de5b2c8bcd74e1a9cf7af7c911803b5c37d33460 Mon Sep 17 00:00:00 2001 From: Sebastian Markbage Date: Tue, 14 Mar 2023 19:27:29 -0400 Subject: [PATCH 1/3] Avoid meta programming to initialize functions in module scope Function declarations with conditions inside of them is the preferred pattern. --- .../src/client/ReactDOMComponent.js | 66 +- .../src/client/validateDOMNesting.js | 947 +++++++++--------- .../src/shared/ReactDOMUnknownPropertyHook.js | 26 +- .../src/shared/warnValidStyle.js | 178 ++-- .../src/ReactNativeFiberInspector.js | 194 ++-- .../src/legacy-events/EventPluginUtils.js | 7 +- .../src/ReactFiberClassComponent.js | 56 +- .../src/ReactFizzClassComponent.js | 18 +- 8 files changed, 738 insertions(+), 754 deletions(-) diff --git a/packages/react-dom-bindings/src/client/ReactDOMComponent.js b/packages/react-dom-bindings/src/client/ReactDOMComponent.js index 8cb2e95accb9f..037d787197c85 100644 --- a/packages/react-dom-bindings/src/client/ReactDOMComponent.js +++ b/packages/react-dom-bindings/src/client/ReactDOMComponent.js @@ -94,15 +94,8 @@ const HTML = '__html'; let warnedUnknownTags: { [key: string]: boolean, }; - -let validatePropertiesInDevelopment; -let warnForPropDifference; -let warnForExtraAttributes; -let warnForInvalidEventListener; let canDiffStyleForHydrationWarning; -let normalizeHTML; - if (__DEV__) { warnedUnknownTags = { // There are working polyfills for . Let people use it. @@ -115,15 +108,6 @@ if (__DEV__) { webview: true, }; - validatePropertiesInDevelopment = function (type: string, props: any) { - validateARIAProperties(type, props); - validateInputProperties(type, props); - validateUnknownProperties(type, props, { - registrationNameDependencies, - possibleRegistrationNames, - }); - }; - // IE 11 parses & normalizes the style attribute as opposed to other // browsers. It adds spaces and sorts the properties in some // non-alphabetical order. Handling that would require sorting CSS @@ -133,12 +117,25 @@ if (__DEV__) { // in that browser completely in favor of doing all that work. // See https://github.com/facebook/react/issues/11807 canDiffStyleForHydrationWarning = canUseDOM && !document.documentMode; +} - warnForPropDifference = function ( - propName: string, - serverValue: mixed, - clientValue: mixed, - ) { +function validatePropertiesInDevelopment(type: string, props: any) { + if (__DEV__) { + validateARIAProperties(type, props); + validateInputProperties(type, props); + validateUnknownProperties(type, props, { + registrationNameDependencies, + possibleRegistrationNames, + }); + } +} + +function warnForPropDifference( + propName: string, + serverValue: mixed, + clientValue: mixed, +) { + if (__DEV__) { if (didWarnInvalidHydration) { return; } @@ -156,9 +153,11 @@ if (__DEV__) { JSON.stringify(normalizedServerValue), JSON.stringify(normalizedClientValue), ); - }; + } +} - warnForExtraAttributes = function (attributeNames: Set) { +function warnForExtraAttributes(attributeNames: Set) { + if (__DEV__) { if (didWarnInvalidHydration) { return; } @@ -168,12 +167,11 @@ if (__DEV__) { names.push(name); }); console.error('Extra attributes from the server: %s', names); - }; + } +} - warnForInvalidEventListener = function ( - registrationName: string, - listener: any, - ) { +function warnForInvalidEventListener(registrationName: string, listener: any) { + if (__DEV__) { if (listener === false) { console.error( 'Expected `%s` listener to be a function, instead got `false`.\n\n' + @@ -190,11 +188,13 @@ if (__DEV__) { typeof listener, ); } - }; + } +} - // Parse the HTML and read it back to normalize the HTML string so that it - // can be used for comparison. - normalizeHTML = function (parent: Element, html: string) { +// Parse the HTML and read it back to normalize the HTML string so that it +// can be used for comparison. +function normalizeHTML(parent: Element, html: string) { + if (__DEV__) { // We could have created a separate document here to avoid // re-initializing custom elements if they exist. But this breaks // how