Skip to content

Commit f9bb9e9

Browse files
committed
fix: never assume the definition of __DEV__ in the global scope
Especially appropriate for jest tests and react-native-web.
1 parent aa3f45f commit f9bb9e9

14 files changed

+26
-18
lines changed

packages/render-html/src/RenderHTMLConfigProvider.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export default function RenderHTMLConfigProvider(
6161
const engine = useAmbientTRenderEngine();
6262
const profile = useProfiler({ prop: 'remoteErrorView or remoteLoadingView' });
6363
const sourceLoaderConfig = useMemo(() => {
64-
__DEV__ && profile();
64+
typeof __DEV__ === 'boolean' && __DEV__ && profile();
6565
return {
6666
remoteErrorView: remoteErrorView || defaultRenderError,
6767
remoteLoadingView: remoteLoadingView || defaultRenderLoading

packages/render-html/src/RenderHTMLDebug.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { RenderHTMLProps } from './shared-types';
55
const RenderHTMLDebug = function RenderHTMLDebug(
66
props: PropsWithChildren<RenderHTMLProps>
77
) {
8-
if (__DEV__) {
8+
if (typeof __DEV__ === 'boolean' && __DEV__) {
99
if (typeof props.contentWidth !== 'number') {
1010
console.warn(debugMessage.contentWidth);
1111
}

packages/render-html/src/RenderHTMLSource.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function RawSourceLoader({
6363
}: SourceLoaderProps): ReactElement | null {
6464
if (isEmptySource(source)) {
6565
/* istanbul ignore next */
66-
if (__DEV__) {
66+
if (typeof __DEV__ === 'boolean' && __DEV__) {
6767
console.warn(debugMessage.noSource);
6868
}
6969
return null;
@@ -111,13 +111,13 @@ const RenderHTMLSource = memo(
111111
prop: 'onDocumentMetadataLoaded or onTTreeChange'
112112
});
113113
const ttreeEvents: TTreeEvents = useMemo(() => {
114-
__DEV__ && profile();
114+
typeof __DEV__ === 'boolean' && __DEV__ && profile();
115115
return {
116116
onDocumentMetadataLoaded,
117117
onTTreeChange
118118
};
119119
}, [onDocumentMetadataLoaded, onTTreeChange, profile]);
120-
if (__DEV__) {
120+
if (typeof __DEV__ === 'boolean' && __DEV__) {
121121
if (!(typeof contentWidth === 'number')) {
122122
console.warn(debugMessage.contentWidth);
123123
}

packages/render-html/src/TNodeRenderer.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ const TNodeRenderer = memo(function MemoizedTNodeRenderer(
2828
if (tnode.type === 'text') {
2929
return React.createElement(TTextRenderer, tnodeProps);
3030
}
31-
if (tnode.type === 'empty' && __DEV__) {
31+
if (typeof __DEV__ === 'boolean' && __DEV__ && tnode.type === 'empty') {
3232
if (tnode.isUnregistered) {
3333
console.warn(
3434
`There is no custom renderer registered for tag "${tnode.tagName}" which is not part of the HTML5 standard. The tag will not be rendered.` +
35-
' If you don\'t want this tag to be rendered, add it to "ignoredTags" prop array. If you do, register a custom renderer for this tag.'
35+
' If you don\'t want this tag to be rendered, add it to "ignoredTags" prop array. If you do, register an HTMLElementModel for this tag with "customHTMLElementModels" prop.'
3636
);
3737
} else if (tnode.tagName !== 'head') {
3838
console.warn(
39-
`The "${tnode.tagName}" tag is a valid HTML element but is not handled by this library. You must register a custom renderer or plugin and make sure its content model is not set to "none".` +
39+
`The "${tnode.tagName}" tag is a valid HTML element but is not handled by this library. You must extend the default HTMLElementModel for this tag with "customHTMLElementModels" prop and make sure its content model is not set to "none".` +
4040
' If you don\'t want this tag to be rendered, add it to "ignoredTags" prop array.'
4141
);
4242
}

packages/render-html/src/TRenderEngineProvider.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ export const defaultTRenderEngineProviderProps: TRenderEngineConfig = {
7777
*/
7878
export function useAmbientTRenderEngine() {
7979
const engine = React.useContext(TRenderEngineContext);
80-
if (__DEV__ && engine === defaultTRenderEngine) {
80+
if (
81+
typeof __DEV__ === 'boolean' &&
82+
__DEV__ &&
83+
engine === defaultTRenderEngine
84+
) {
8185
console.error('TRenderEngineProvider is missing in the render tree.');
8286
}
8387
return engine;

packages/render-html/src/context/RenderRegistryProvider.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export default function RenderRegistryProvider({
3131
}>) {
3232
const profile = useProfiler({ prop: 'renderers' });
3333
const registry = useMemo(() => {
34-
__DEV__ && profile();
34+
typeof __DEV__ === 'boolean' && __DEV__ && profile();
3535
return new RenderRegistry(renderers, elementModels);
3636
}, [renderers, elementModels, profile]);
3737
return (

packages/render-html/src/context/RenderersPropsProvider.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export default function RenderersPropsProvider(
3333
) {
3434
const profile = useProfiler({ prop: 'renderersProps' });
3535
const mergedRenderersProps = useMemo(() => {
36-
__DEV__ && profile();
36+
typeof __DEV__ === 'boolean' && __DEV__ && profile();
3737
return mergeDeepRight(defaultRendererProps, props.renderersProps || {});
3838
}, [props.renderersProps, profile]);
3939
return React.createElement(

packages/render-html/src/context/defaultSharedProps.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { RenderHTMLAmbiantSharedProps } from '../shared-types';
33

44
function WebViewPlaceholder() {
55
/* istanbul ignore else */
6-
if (__DEV__) {
6+
if (typeof __DEV__ === 'boolean' && __DEV__) {
77
console.warn(
88
'One of your renderers is attempting to use WebView component, which has not been ' +
99
"provided as a prop to the RenderHtml component. As a consequence, the element won't be rendered."

packages/render-html/src/debugMessages.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ let debugMessage: Record<DebugType, string>;
1818

1919
export type DebugMessages = typeof debugMessage;
2020
/* istanbul ignore next */
21-
if (__DEV__) {
21+
if (typeof __DEV__ === 'boolean' && __DEV__) {
2222
debugMessage = {
2323
outdatedComputeImagesMaxWidth:
2424
"You're attempting to use an outdated prop, 'computeImagesMaxWidth'. This prop has been replaced in version 6 with 'computeEmbeddedMaxWidth'.",

packages/render-html/src/elements/ListElement.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,11 @@ export default function ListElement({
110110
ownListType ||
111111
listStyleTypeFallbackRecord[listType];
112112
const listStyleType = ownListType || selectedListType;
113-
if (__DEV__ && !(listStyleType in listStyleSpecs)) {
113+
if (
114+
typeof __DEV__ === 'boolean' &&
115+
__DEV__ &&
116+
!(listStyleType in listStyleSpecs)
117+
) {
114118
if (listStyleType.match(/^("|')/)) {
115119
console.warn(
116120
"This library doesn't support strings for list-style-type CSS properties."

0 commit comments

Comments
 (0)