From 6cc7d06b0f5ec01ce73f964b919904c73d7aaa40 Mon Sep 17 00:00:00 2001 From: Dimitri POSTOLOV Date: Fri, 23 Jun 2023 12:40:16 +0200 Subject: [PATCH 1/4] avoid using `globalThis` with `process` --- .eslintrc.cjs | 13 ++++++++++++- .gitignore | 1 + src/jsutils/instanceOf.ts | 3 +-- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 22a8050c4d..c20c37bf4d 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -1,3 +1,4 @@ +/** @type {import('eslint').Linter.Config} */ module.exports = { parserOptions: { sourceType: 'script', @@ -347,7 +348,15 @@ module.exports = { 'no-restricted-globals': 'off', 'no-restricted-imports': 'off', 'no-restricted-properties': 'off', - 'no-restricted-syntax': 'off', + 'no-restricted-syntax': [ + 'error', + { + selector: + 'MemberExpression[object.name="globalThis"][property.name="process"]', + message: + "Never use `process` with `globalThis` because bundlers incorrectly replace it and doesn't tree shake unused code", + }, + ], 'no-return-assign': 'error', 'no-return-await': 'error', 'no-script-url': 'error', @@ -494,6 +503,8 @@ module.exports = { plugins: ['@typescript-eslint', 'eslint-plugin-tsdoc'], extends: ['plugin:import/typescript'], rules: { + // https://typescript-eslint.io/linting/troubleshooting/#i-get-errors-from-the-no-undef-rule-about-global-variables-not-being-defined-even-though-there-are-no-typescript-errors + 'no-undef': 'off', ////////////////////////////////////////////////////////////////////////// // `eslint-plugin-tsdoc` rule list based on `v0.2.x` // https://github.com/microsoft/tsdoc/tree/master/eslint-plugin diff --git a/.gitignore b/.gitignore index b3a652de18..d42f24fa55 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ /npmEsmDist /denoDist /websiteDist +.idea/ diff --git a/src/jsutils/instanceOf.ts b/src/jsutils/instanceOf.ts index 489d63f42d..64cf3cb235 100644 --- a/src/jsutils/instanceOf.ts +++ b/src/jsutils/instanceOf.ts @@ -8,8 +8,7 @@ import { inspect } from './inspect.js'; */ export const instanceOf: (value: unknown, constructor: Constructor) => boolean = /* c8 ignore next 6 */ - // FIXME: https://github.com/graphql/graphql-js/issues/2317 - globalThis.process?.env.NODE_ENV === 'production' + process.env.NODE_ENV === 'production' ? function instanceOf(value: unknown, constructor: Constructor): boolean { return value instanceof constructor; } From e0f522a2968f735407fbb6ea774466260bf3f66c Mon Sep 17 00:00:00 2001 From: Dimitri POSTOLOV Date: Sat, 24 Jun 2023 13:52:02 +0200 Subject: [PATCH 2/4] add `typeof process === 'undefined'` to make everybody happy --- src/jsutils/instanceOf.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/jsutils/instanceOf.ts b/src/jsutils/instanceOf.ts index 64cf3cb235..d5c7b8d67d 100644 --- a/src/jsutils/instanceOf.ts +++ b/src/jsutils/instanceOf.ts @@ -8,7 +8,7 @@ import { inspect } from './inspect.js'; */ export const instanceOf: (value: unknown, constructor: Constructor) => boolean = /* c8 ignore next 6 */ - process.env.NODE_ENV === 'production' + typeof process === 'undefined' || process.env.NODE_ENV === 'production' ? function instanceOf(value: unknown, constructor: Constructor): boolean { return value instanceof constructor; } From ef2405f9b624f5b719b9785df30c33216ccbdf65 Mon Sep 17 00:00:00 2001 From: Dimitri POSTOLOV Date: Mon, 26 Jun 2023 11:51:56 +0200 Subject: [PATCH 3/4] Update src/jsutils/instanceOf.ts Co-authored-by: Lenz Weber-Tronic --- src/jsutils/instanceOf.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/jsutils/instanceOf.ts b/src/jsutils/instanceOf.ts index d5c7b8d67d..08bb76ab3c 100644 --- a/src/jsutils/instanceOf.ts +++ b/src/jsutils/instanceOf.ts @@ -8,7 +8,7 @@ import { inspect } from './inspect.js'; */ export const instanceOf: (value: unknown, constructor: Constructor) => boolean = /* c8 ignore next 6 */ - typeof process === 'undefined' || process.env.NODE_ENV === 'production' + typeof process !== 'undefined' && process.env.NODE_ENV === 'production' ? function instanceOf(value: unknown, constructor: Constructor): boolean { return value instanceof constructor; } From 06e2d8a0fb9da493bd1a39ba42f604702f2c33a7 Mon Sep 17 00:00:00 2001 From: Dimitri POSTOLOV Date: Tue, 27 Jun 2023 14:42:08 +0200 Subject: [PATCH 4/4] fix review --- .eslintrc.cjs | 4 ++-- src/jsutils/instanceOf.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index c20c37bf4d..1ee989b4ea 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -352,9 +352,9 @@ module.exports = { 'error', { selector: - 'MemberExpression[object.name="globalThis"][property.name="process"]', + 'MemberExpression[property.name="env"] > .object[object.name="globalThis"][property.name="process"]', message: - "Never use `process` with `globalThis` because bundlers incorrectly replace it and doesn't tree shake unused code", + "Never use `process.env` with `globalThis` because bundlers incorrectly replace it and doesn't tree shake unused code", }, ], 'no-return-assign': 'error', diff --git a/src/jsutils/instanceOf.ts b/src/jsutils/instanceOf.ts index 08bb76ab3c..4307b310a6 100644 --- a/src/jsutils/instanceOf.ts +++ b/src/jsutils/instanceOf.ts @@ -8,7 +8,7 @@ import { inspect } from './inspect.js'; */ export const instanceOf: (value: unknown, constructor: Constructor) => boolean = /* c8 ignore next 6 */ - typeof process !== 'undefined' && process.env.NODE_ENV === 'production' + globalThis.process !== undefined && process.env.NODE_ENV === 'production' ? function instanceOf(value: unknown, constructor: Constructor): boolean { return value instanceof constructor; }