Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch instanceOf to be named export #2990

Merged
merged 1 commit into from Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/jsutils/__tests__/instanceOf-test.js
@@ -1,7 +1,7 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';

import instanceOf from '../instanceOf';
import { instanceOf } from '../instanceOf';

describe('instanceOf', () => {
it('fails with descriptive error message', () => {
Expand Down
49 changes: 21 additions & 28 deletions src/jsutils/instanceOf.js
@@ -1,32 +1,25 @@
/**
* A replacement for instanceof which includes an error warning when multi-realm
* constructors are detected.
* See: https://expressjs.com/en/advanced/best-practice-performance.html#set-node_env-to-production
* See: https://webpack.js.org/guides/production/
*/
declare function instanceOf(
value: mixed,
constructor: mixed,
): boolean %checks(value instanceof constructor);

// See: https://expressjs.com/en/advanced/best-practice-performance.html#set-node_env-to-production
// See: https://webpack.js.org/guides/production/
// eslint-disable-next-line import/no-default-export
export default process.env.NODE_ENV === 'production'
? // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2317')
// eslint-disable-next-line no-shadow
function instanceOf(value: mixed, constructor: mixed): boolean {
return value instanceof constructor;
}
: // eslint-disable-next-line no-shadow
function instanceOf(value: any, constructor: any): boolean {
if (value instanceof constructor) {
return true;
export const instanceOf: (mixed, mixed) => boolean =
process.env.NODE_ENV === 'production'
? // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2317')
function instanceOf(value: mixed, constructor: mixed): boolean {
return value instanceof constructor;
}
if (value) {
const valueClass = value.constructor;
const className = constructor.name;
if (className && valueClass && valueClass.name === className) {
throw new Error(
`Cannot use ${className} "${value}" from another module or realm.
: function instanceOf(value: any, constructor: any): boolean {
if (value instanceof constructor) {
return true;
}
if (value) {
const valueClass = value.constructor;
const className = constructor.name;
if (className && valueClass && valueClass.name === className) {
throw new Error(
`Cannot use ${className} "${value}" from another module or realm.

Ensure that there is only one instance of "graphql" in the node_modules
directory. If different versions of "graphql" are the dependencies of other
Expand All @@ -38,8 +31,8 @@ Duplicate "graphql" modules cannot be used at the same time since different
versions may have different capabilities and behavior. The data from one
version used in the function from another could produce confusing and
spurious results.`,
);
);
}
}
}
return false;
};
return false;
};
2 changes: 1 addition & 1 deletion src/language/source.js
@@ -1,6 +1,6 @@
import { inspect } from '../jsutils/inspect';
import { devAssert } from '../jsutils/devAssert';
import instanceOf from '../jsutils/instanceOf';
import { instanceOf } from '../jsutils/instanceOf';

type Location = {|
line: number,
Expand Down
2 changes: 1 addition & 1 deletion src/type/definition.js
Expand Up @@ -11,7 +11,7 @@ import { mapValue } from '../jsutils/mapValue';
import { toObjMap } from '../jsutils/toObjMap';
import { devAssert } from '../jsutils/devAssert';
import { keyValMap } from '../jsutils/keyValMap';
import instanceOf from '../jsutils/instanceOf';
import { instanceOf } from '../jsutils/instanceOf';
import { didYouMean } from '../jsutils/didYouMean';
import { isObjectLike } from '../jsutils/isObjectLike';
import { identityFunc } from '../jsutils/identityFunc';
Expand Down
2 changes: 1 addition & 1 deletion src/type/directives.js
Expand Up @@ -2,7 +2,7 @@ import type { ReadOnlyObjMap, ReadOnlyObjMapLike } from '../jsutils/ObjMap';
import { inspect } from '../jsutils/inspect';
import { toObjMap } from '../jsutils/toObjMap';
import { devAssert } from '../jsutils/devAssert';
import instanceOf from '../jsutils/instanceOf';
import { instanceOf } from '../jsutils/instanceOf';
import { isObjectLike } from '../jsutils/isObjectLike';

import type { DirectiveDefinitionNode } from '../language/ast';
Expand Down
2 changes: 1 addition & 1 deletion src/type/schema.js
Expand Up @@ -6,7 +6,7 @@ import type {
import { inspect } from '../jsutils/inspect';
import { toObjMap } from '../jsutils/toObjMap';
import { devAssert } from '../jsutils/devAssert';
import instanceOf from '../jsutils/instanceOf';
import { instanceOf } from '../jsutils/instanceOf';
import { isObjectLike } from '../jsutils/isObjectLike';

import type { GraphQLError } from '../error/GraphQLError';
Expand Down