From 77e872217c7f4044ae4f96dd99bf999efb601040 Mon Sep 17 00:00:00 2001 From: Behnam Mohammadi Date: Wed, 8 Jul 2020 22:55:24 +0430 Subject: [PATCH] Improve readability of isValidElementType (#19251) * improve readability * replace condition by switch/case * replace condition by switch/case * remove unnecessary braces * replace switch/case to ifs * replace switch/case to ifs * fix by multiline if statements * fix multiple if statements --- packages/shared/isValidElementType.js | 47 +++++++++++++++++---------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/packages/shared/isValidElementType.js b/packages/shared/isValidElementType.js index 850417a2d3..af9182865d 100644 --- a/packages/shared/isValidElementType.js +++ b/packages/shared/isValidElementType.js @@ -28,28 +28,39 @@ import { } from 'shared/ReactSymbols'; export default function isValidElementType(type: mixed) { - return ( - typeof type === 'string' || - typeof type === 'function' || - // Note: its typeof might be other than 'symbol' or 'number' if it's a polyfill. + if (typeof type === 'string' || typeof type === 'function') { + return true; + } + + // Note: typeof might be other than 'symbol' or 'number' (e.g. if it's a polyfill). + if ( type === REACT_FRAGMENT_TYPE || type === REACT_PROFILER_TYPE || type === REACT_DEBUG_TRACING_MODE_TYPE || type === REACT_STRICT_MODE_TYPE || type === REACT_SUSPENSE_TYPE || type === REACT_SUSPENSE_LIST_TYPE || - type === REACT_LEGACY_HIDDEN_TYPE || - (typeof type === 'object' && - type !== null && - (type.$$typeof === REACT_LAZY_TYPE || - type.$$typeof === REACT_MEMO_TYPE || - type.$$typeof === REACT_PROVIDER_TYPE || - type.$$typeof === REACT_CONTEXT_TYPE || - type.$$typeof === REACT_FORWARD_REF_TYPE || - type.$$typeof === REACT_FUNDAMENTAL_TYPE || - type.$$typeof === REACT_RESPONDER_TYPE || - type.$$typeof === REACT_SCOPE_TYPE || - type.$$typeof === REACT_BLOCK_TYPE || - type[(0: any)] === REACT_SERVER_BLOCK_TYPE)) - ); + type === REACT_LEGACY_HIDDEN_TYPE + ) { + return true; + } + + if (typeof type === 'object' && type !== null) { + if ( + type.$$typeof === REACT_LAZY_TYPE || + type.$$typeof === REACT_MEMO_TYPE || + type.$$typeof === REACT_PROVIDER_TYPE || + type.$$typeof === REACT_CONTEXT_TYPE || + type.$$typeof === REACT_FORWARD_REF_TYPE || + type.$$typeof === REACT_FUNDAMENTAL_TYPE || + type.$$typeof === REACT_RESPONDER_TYPE || + type.$$typeof === REACT_SCOPE_TYPE || + type.$$typeof === REACT_BLOCK_TYPE || + type[(0: any)] === REACT_SERVER_BLOCK_TYPE + ) { + return true; + } + } + + return false; }