From 4630f37786401c3d714c3308d3e36575abaa7e24 Mon Sep 17 00:00:00 2001 From: Michel Weststrate Date: Fri, 17 Dec 2021 14:11:20 -0800 Subject: [PATCH] Clean up process globals Summary: Cleaned up some `process` references Reviewed By: passy Differential Revision: D33184882 fbshipit-source-id: 6fc56bcea4e145ba97aa8bf9e689e53e2bf239cc --- desktop/flipper-common/src/index.tsx | 1 + .../flipper-common/src/utils/isProduction.tsx | 18 ++++++++++++ desktop/flipper-common/src/utils/isTest.tsx | 3 +- .../data-source/DataSourceRendererVirtual.tsx | 1 + .../src/utils/shallowSerialization.tsx | 10 ++----- .../src/utils/useAssertStableRef.tsx | 28 ++++++++----------- 6 files changed, 36 insertions(+), 25 deletions(-) create mode 100644 desktop/flipper-common/src/utils/isProduction.tsx diff --git a/desktop/flipper-common/src/index.tsx b/desktop/flipper-common/src/index.tsx index 9759c7aad..7e7214a68 100644 --- a/desktop/flipper-common/src/index.tsx +++ b/desktop/flipper-common/src/index.tsx @@ -20,6 +20,7 @@ export * from './server-types'; export {sleep} from './utils/sleep'; export {timeout} from './utils/timeout'; export {isTest} from './utils/isTest'; +export {isProduction} from './utils/isProduction'; export {assertNever} from './utils/assertNever'; export {fsConstants} from './utils/fsConstants'; export { diff --git a/desktop/flipper-common/src/utils/isProduction.tsx b/desktop/flipper-common/src/utils/isProduction.tsx new file mode 100644 index 000000000..0e4a8d524 --- /dev/null +++ b/desktop/flipper-common/src/utils/isProduction.tsx @@ -0,0 +1,18 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + */ + +declare const process: any; + +// TODO: there are currently two definitions of isProduction active, +// this one, and one provided by the RenderHostConfig. Ideally they should be unified +export function isProduction(): boolean { + return ( + typeof process === 'undefined' || process.env.NODE_ENV === 'production' + ); +} diff --git a/desktop/flipper-common/src/utils/isTest.tsx b/desktop/flipper-common/src/utils/isTest.tsx index fb118662a..523820852 100644 --- a/desktop/flipper-common/src/utils/isTest.tsx +++ b/desktop/flipper-common/src/utils/isTest.tsx @@ -8,8 +8,7 @@ */ declare const process: any; -const _isTest = typeof process !== 'undefined' && !!process.env.JEST_WORKER_ID; export function isTest(): boolean { - return _isTest; + return typeof process !== 'undefined' && !!process.env.JEST_WORKER_ID; } diff --git a/desktop/flipper-plugin/src/data-source/DataSourceRendererVirtual.tsx b/desktop/flipper-plugin/src/data-source/DataSourceRendererVirtual.tsx index 383d0623c..674ec3f57 100644 --- a/desktop/flipper-plugin/src/data-source/DataSourceRendererVirtual.tsx +++ b/desktop/flipper-plugin/src/data-source/DataSourceRendererVirtual.tsx @@ -349,5 +349,6 @@ export function useTableRedraw() { declare const process: any; function useInUnitTest(): boolean { + // N.B. Not reusing flipper-common here, since data-source is published as separate package return typeof process !== 'undefined' && process?.env?.NODE_ENV === 'test'; } diff --git a/desktop/flipper-plugin/src/utils/shallowSerialization.tsx b/desktop/flipper-plugin/src/utils/shallowSerialization.tsx index 30b5ef1d4..a14524a94 100644 --- a/desktop/flipper-plugin/src/utils/shallowSerialization.tsx +++ b/desktop/flipper-plugin/src/utils/shallowSerialization.tsx @@ -7,6 +7,8 @@ * @format */ +import {isProduction} from 'flipper-common'; + /** * makeShallowSerializable will prepare common data structures, like Map and Set, for JSON serialization. * However, this will happen only for the root object and not recursively to keep things efficiently. @@ -66,18 +68,12 @@ export function deserializeShallowObject(obj: any): any { return obj; } -// TODO: introduce a isProduction utility! -declare const process: any; - /** * Asserts a value is JSON serializable. * Will print a warning if a value is JSON serializable, but isn't a pure tree */ export function assertSerializable(obj: any) { - if ( - typeof process === 'undefined' || - (process.env.NODE_ENV !== 'test' && process.env.NODE_ENV !== 'development') - ) { + if (isProduction()) { return; } // path to current object diff --git a/desktop/flipper-plugin/src/utils/useAssertStableRef.tsx b/desktop/flipper-plugin/src/utils/useAssertStableRef.tsx index 9de3e63ab..b3a592278 100644 --- a/desktop/flipper-plugin/src/utils/useAssertStableRef.tsx +++ b/desktop/flipper-plugin/src/utils/useAssertStableRef.tsx @@ -7,27 +7,23 @@ * @format */ +import {isProduction} from 'flipper-common'; import {useRef} from 'react'; -// TODO: create isProduction utility! -declare const process: any; - /** * This hook will throw in development builds if the value passed in is unstable. * Use this if to make sure consumers aren't creating or changing certain props over time * (intentionally or accidentally) */ -export const useAssertStableRef = - typeof process !== 'undefined' && - (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test') - ? function useAssertStableRef(value: any, prop: string) { - const ref = useRef(value); - if (ref.current !== value) { - throw new Error( - `[useAssertStableRef] An unstable reference was passed to this component as property '${prop}'. For optimization purposes we expect that this prop doesn't change over time. You might want to create the value passed to this prop outside the render closure, store it in useCallback / useMemo / useState, or set a key on the parent component`, - ); - } +export const useAssertStableRef = !isProduction() + ? function useAssertStableRef(value: any, prop: string) { + const ref = useRef(value); + if (ref.current !== value) { + throw new Error( + `[useAssertStableRef] An unstable reference was passed to this component as property '${prop}'. For optimization purposes we expect that this prop doesn't change over time. You might want to create the value passed to this prop outside the render closure, store it in useCallback / useMemo / useState, or set a key on the parent component`, + ); } - : function (_value: any, _prop: string) { - // no-op - }; + } + : function (_value: any, _prop: string) { + // no-op + };