Skip to content

Commit 9d3bbd1

Browse files
authored
fix(warning): replace __DEV__ with process.env.NODE_ENV condition (#18978)
* fix(warning): replace __DEV__ with process.env.NODE_ENV condition * chore: update dev docs
1 parent 00f1575 commit 9d3bbd1

File tree

25 files changed

+65
-51
lines changed

25 files changed

+65
-51
lines changed

config/eslint-config-carbon/base.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,4 @@ module.exports = {
3939
jest: true,
4040
jasmine: true,
4141
},
42-
globals: {
43-
__DEV__: true,
44-
},
4542
};

config/eslint-config-carbon/rules/best-practices.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,12 @@ module.exports = {
1111
rules: {
1212
// @see https://eslint.org/docs/rules/curly
1313
curly: 'error',
14+
'no-restricted-globals': [
15+
'error',
16+
{
17+
name: '__DEV__',
18+
message: "Use process.env.NODE_ENV !== 'production' instead",
19+
},
20+
],
1421
},
1522
};

config/jest-config-carbon/setup/setup.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
'use strict';
99

10-
global.__DEV__ = true;
11-
1210
jest.setTimeout(20000);
1311

1412
global.requestAnimationFrame = function requestAnimationFrame(callback) {

docs/developer-handbook.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ import { warning } from '../../internal/warning';
660660
let didWarnAboutDeprecation = false;
661661

662662
function SomeComponent() {
663-
if (__DEV__) {
663+
if (process.env.NODE_ENV !== 'production') {
664664
warning(
665665
didWarnAboutDeprecation,
666666
'The `SomeComponent` component has been deprecated and will be removed ' +
@@ -671,6 +671,10 @@ function SomeComponent() {
671671
}
672672
```
673673

674+
_Note: even though warning() has a process.env.NODE_ENV condition internal to
675+
itself, an additional one is needed above to ensure `didWarnAboutDeprecation` is
676+
not modified in non-development environments._
677+
674678
_Note: if available, you should add a closing sentence specifying what component
675679
to use instead, or share a link for more information. This may look like:_
676680

packages/icon-build-helpers/src/builders/react/builder.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ function formatAttributes(attrs) {
236236
function createIconFlatExport(moduleName, descriptor, isDeprecated = false) {
237237
const deprecatedBlock = isDeprecated
238238
? `
239-
if (__DEV__) {
239+
if (process.env.NODE_ENV !== "production") {
240240
if (!didWarnAboutDeprecation['${moduleName}']) {
241241
didWarnAboutDeprecation['${moduleName}'] = true;
242242
console.warn(
@@ -256,7 +256,7 @@ function createIconEntrypoint(moduleName, descriptor, isDeprecated = false) {
256256
: '';
257257
const deprecatedBlock = isDeprecated
258258
? `
259-
if (__DEV__) {
259+
if (process.env.NODE_ENV !== "production") {
260260
if (!didWarnAboutDeprecation) {
261261
didWarnAboutDeprecation = true;
262262
console.warn(

packages/icon-build-helpers/src/builders/react/next/templates.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const component = template(
4040
}
4141
);
4242
43-
if (__DEV__) {
43+
if (process.env.NODE_ENV !== "production") {
4444
%%moduleName%%.propTypes = iconPropTypes;
4545
}
4646
`,
@@ -51,7 +51,7 @@ const component = template(
5151

5252
const deprecatedBlock = template(
5353
`
54-
if (__DEV__) {
54+
if (process.env.NODE_ENV !== "production") {
5555
if (!%%check%%) {
5656
%%check%% = true;
5757
console.warn(%%warning%%);

packages/react/src/components/ContainedList/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import ContainedListItem from './ContainedListItem';
1111

1212
ContainedList.ContainedListItem = ContainedListItem;
1313

14-
if (__DEV__) {
14+
if (process.env.NODE_ENV !== 'production') {
1515
deprecateFieldOnObject(ContainedList, 'ContainedListItem', ContainedListItem);
1616
}
1717
export { ContainedListItem };

packages/react/src/components/ModalWrapper/ModalWrapper.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,13 @@ interface ModelWrapperState {
4545
}
4646

4747
let didWarnAboutDeprecation = false;
48+
let isDev = process.env.NODE_ENV !== 'production';
4849

4950
export default class ModalWrapper extends React.Component<
5051
ModalWrapperProps,
5152
ModelWrapperState
5253
> {
53-
if(__DEV__) {
54+
if(isDev) {
5455
warning(
5556
didWarnAboutDeprecation,
5657
'`<ModalWrapper>` has been deprecated in favor of `<ComposedModal/>` and will be removed in the next major version, `@carbon/react@v2.x`'

packages/react/src/components/Notification/Notification.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1436,7 +1436,7 @@ let didWarnAboutDeprecation = false;
14361436
export const StaticNotification: React.FC<StaticNotificationProps> = (
14371437
props
14381438
) => {
1439-
if (__DEV__) {
1439+
if (process.env.NODE_ENV !== 'production') {
14401440
warning(
14411441
didWarnAboutDeprecation,
14421442
'`StaticNotification` has been renamed to `Callout`.' +

packages/react/src/components/OverflowMenu/OverflowMenu.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export const getMenuOffset: MenuOffset = (
9494
) => {
9595
const triggerButtonPositionProp = triggerButtonPositionProps[direction];
9696
const triggerButtonPositionFactor = triggerButtonPositionFactors[direction];
97-
if (__DEV__) {
97+
if (process.env.NODE_ENV !== 'production') {
9898
invariant(
9999
triggerButtonPositionProp && triggerButtonPositionFactor,
100100
'[OverflowMenu] wrong floating menu direction: `%s`',

0 commit comments

Comments
 (0)