Skip to content

Commit

Permalink
fix: Remove magic __DEV__ global (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
amannn authored Dec 9, 2022
1 parent eb33176 commit 7d5aa6a
Show file tree
Hide file tree
Showing 13 changed files with 16 additions and 28 deletions.
18 changes: 11 additions & 7 deletions packages/use-intl/src/core/createBaseTranslator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ function resolvePath(
) {
if (!messages) {
throw new Error(
__DEV__ ? `No messages available at \`${namespace}\`.` : undefined
process.env.NODE_ENV !== 'production'
? `No messages available at \`${namespace}\`.`
: undefined
);
}

Expand All @@ -35,7 +37,7 @@ function resolvePath(

if (part == null || next == null) {
throw new Error(
__DEV__
process.env.NODE_ENV !== 'production'
? `Could not resolve \`${key}\` in ${
namespace ? `\`${namespace}\`` : 'messages'
}.`
Expand Down Expand Up @@ -89,7 +91,9 @@ export function getMessagesOrError<Messages extends AbstractIntlMessages>({
try {
if (!messages) {
throw new Error(
__DEV__ ? `No messages were configured on the provider.` : undefined
process.env.NODE_ENV !== 'production'
? `No messages were configured on the provider.`
: undefined
);
}

Expand All @@ -99,7 +103,7 @@ export function getMessagesOrError<Messages extends AbstractIntlMessages>({

if (!retrievedMessages) {
throw new Error(
__DEV__
process.env.NODE_ENV !== 'production'
? `No messages for namespace \`${namespace}\` found.`
: undefined
);
Expand Down Expand Up @@ -187,7 +191,7 @@ export default function createBaseTranslator<
return getFallbackFromErrorAndNotify(
key,
IntlErrorCode.INSUFFICIENT_PATH,
__DEV__
process.env.NODE_ENV !== 'production'
? `Insufficient path specified for \`${key}\` in \`${
namespace ? `\`${namespace}\`` : 'messages'
}\`.`
Expand Down Expand Up @@ -231,7 +235,7 @@ export default function createBaseTranslator<

if (formattedMessage == null) {
throw new Error(
__DEV__
process.env.NODE_ENV !== 'production'
? `Unable to format \`${key}\` in ${
namespace ? `namespace \`${namespace}\`` : 'messages'
}`
Expand Down Expand Up @@ -274,7 +278,7 @@ export default function createBaseTranslator<
return getFallbackFromErrorAndNotify(
key,
IntlErrorCode.INVALID_MESSAGE,
__DEV__
process.env.NODE_ENV !== 'production'
? `The message \`${key}\` in ${
namespace ? `namespace \`${namespace}\`` : 'messages'
} didn't resolve to a string. If you want to format rich text, use \`t.rich\` instead.`
Expand Down
4 changes: 2 additions & 2 deletions packages/use-intl/src/core/createIntl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export default function createIntl({
if (!options) {
const error = new IntlError(
IntlErrorCode.MISSING_FORMAT,
__DEV__
process.env.NODE_ENV !== 'production'
? `Format \`${formatName}\` is not available. You can configure it on the provider or provide custom options.`
: undefined
);
Expand Down Expand Up @@ -153,7 +153,7 @@ export default function createIntl({
now = globalNow;
} else {
throw new Error(
__DEV__
process.env.NODE_ENV !== 'production'
? `The \`now\` parameter wasn't provided to \`formatRelativeTime\` and there was no global fallback configured on the provider.`
: undefined
);
Expand Down
2 changes: 1 addition & 1 deletion packages/use-intl/src/core/createTranslatorImpl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default function createTranslatorImpl<
if (typeof result !== 'string') {
const error = new IntlError(
IntlErrorCode.FORMATTING_ERROR,
__DEV__
process.env.NODE_ENV !== 'production'
? "`createTranslator` only accepts functions for rich text formatting that receive and return strings.\n\nE.g. t.rich('rich', {b: (chunks) => `<b>${chunks}</b>`})"
: undefined
);
Expand Down
2 changes: 1 addition & 1 deletion packages/use-intl/src/react/IntlProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default function IntlProvider({
messages,
...contextValues
}: Props) {
if (__DEV__) {
if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line react-hooks/rules-of-hooks
useEffect(() => {
if (messages) {
Expand Down
2 changes: 1 addition & 1 deletion packages/use-intl/src/react/useIntlContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default function useIntlContext() {

if (!context) {
throw new Error(
__DEV__
process.env.NODE_ENV !== 'production'
? 'No intl context found. Have you configured the provider?'
: undefined
);
Expand Down
2 changes: 0 additions & 2 deletions packages/use-intl/test/core/createIntl.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import {parseISO} from 'date-fns';
import {createIntl} from '../../src';

(global as any).__DEV__ = true;

const intl = createIntl({locale: 'en'});

it('formats a date and time', () => {
Expand Down
2 changes: 0 additions & 2 deletions packages/use-intl/test/core/createTranslator.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import React from 'react';
import {createTranslator, IntlError, IntlErrorCode} from '../../src';

(global as any).__DEV__ = true;

const messages = {
Home: {
title: 'Hello world!',
Expand Down
2 changes: 0 additions & 2 deletions packages/use-intl/test/react/useIntl.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import {
useIntl
} from '../../src';

(global as any).__DEV__ = true;

function MockProvider(
props: Partial<ComponentProps<typeof IntlProvider>> & {children: ReactNode}
) {
Expand Down
2 changes: 0 additions & 2 deletions packages/use-intl/test/react/useLocale.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import {render, screen} from '@testing-library/react';
import React from 'react';
import {IntlProvider, useLocale} from '../../src';

(global as any).__DEV__ = true;

it('returns the current locale', () => {
function Component() {
return <>{useLocale()}</>;
Expand Down
2 changes: 0 additions & 2 deletions packages/use-intl/test/react/useNow.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import {parseISO} from 'date-fns';
import React from 'react';
import {IntlProvider, useNow} from '../../src';

(global as any).__DEV__ = true;

it('returns the current time', () => {
function Component() {
return <p>{useNow().toISOString()}</p>;
Expand Down
2 changes: 0 additions & 2 deletions packages/use-intl/test/react/useTimeZone.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import {render, screen} from '@testing-library/react';
import React from 'react';
import {IntlProvider, useTimeZone} from '../../src';

(global as any).__DEV__ = true;

it('returns the time zone when it is configured', () => {
function Component() {
return <>{useTimeZone()}</>;
Expand Down
2 changes: 0 additions & 2 deletions packages/use-intl/test/react/useTranslations.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import {
useTranslations
} from '../../src';

(global as any).__DEV__ = true;

// Wrap the library to include a counter for parse
// invocations for the cache test below.
jest.mock('intl-messageformat', () => {
Expand Down
2 changes: 0 additions & 2 deletions packages/use-intl/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
declare const __DEV__: boolean;

// Temporarly copied here until the "es2020.intl" lib is published.

declare namespace Intl {
Expand Down

1 comment on commit 7d5aa6a

@vercel
Copy link

@vercel vercel bot commented on 7d5aa6a Dec 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

next-intl – ./

next-intl-docs.vercel.app
next-intl-amann.vercel.app
next-intl-git-main-amann.vercel.app

Please sign in to comment.