diff --git a/src/jsutils/__tests__/instanceOf-test.ts b/src/jsutils/__tests__/instanceOf-test.ts deleted file mode 100644 index 5a54a641e5..0000000000 --- a/src/jsutils/__tests__/instanceOf-test.ts +++ /dev/null @@ -1,79 +0,0 @@ -import { expect } from 'chai'; -import { describe, it } from 'mocha'; - -import { instanceOf } from '../instanceOf.js'; - -describe('instanceOf', () => { - it('do not throw on values without prototype', () => { - class Foo { - get [Symbol.toStringTag]() { - return 'Foo'; - } - } - - expect(instanceOf(true, Foo)).to.equal(false); - expect(instanceOf(null, Foo)).to.equal(false); - expect(instanceOf(Object.create(null), Foo)).to.equal(false); - }); - - it('detect name clashes with older versions of this lib', () => { - function oldVersion() { - class Foo {} - return Foo; - } - - function newVersion() { - class Foo { - get [Symbol.toStringTag]() { - return 'Foo'; - } - } - return Foo; - } - - const NewClass = newVersion(); - const OldClass = oldVersion(); - expect(instanceOf(new NewClass(), NewClass)).to.equal(true); - expect(() => instanceOf(new OldClass(), NewClass)).to.throw(); - }); - - it('allows instances to have share the same constructor name', () => { - function getMinifiedClass(tag: string) { - class SomeNameAfterMinification { - get [Symbol.toStringTag]() { - return tag; - } - } - return SomeNameAfterMinification; - } - - const Foo = getMinifiedClass('Foo'); - const Bar = getMinifiedClass('Bar'); - expect(instanceOf(new Foo(), Bar)).to.equal(false); - expect(instanceOf(new Bar(), Foo)).to.equal(false); - - const DuplicateOfFoo = getMinifiedClass('Foo'); - expect(() => instanceOf(new DuplicateOfFoo(), Foo)).to.throw(); - expect(() => instanceOf(new Foo(), DuplicateOfFoo)).to.throw(); - }); - - it('fails with descriptive error message', () => { - function getFoo() { - class Foo { - get [Symbol.toStringTag]() { - return 'Foo'; - } - } - return Foo; - } - const Foo1 = getFoo(); - const Foo2 = getFoo(); - - expect(() => instanceOf(new Foo1(), Foo2)).to.throw( - /^Cannot use Foo "{}" from another module or realm./m, - ); - expect(() => instanceOf(new Foo2(), Foo1)).to.throw( - /^Cannot use Foo "{}" from another module or realm./m, - ); - }); -}); diff --git a/src/jsutils/instanceOf.ts b/src/jsutils/instanceOf.ts deleted file mode 100644 index 562aee3e2f..0000000000 --- a/src/jsutils/instanceOf.ts +++ /dev/null @@ -1,59 +0,0 @@ -import { inspect } from './inspect.js'; - -/* c8 ignore next 3 */ -const isProduction = - globalThis.process != null && - // eslint-disable-next-line no-undef - process.env.NODE_ENV === 'production'; - -/** - * 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/ - */ -export const instanceOf: (value: unknown, constructor: Constructor) => boolean = - /* c8 ignore next 6 */ - // FIXME: https://github.com/graphql/graphql-js/issues/2317 - isProduction - ? function instanceOf(value: unknown, constructor: Constructor): boolean { - return value instanceof constructor; - } - : function instanceOf(value: unknown, constructor: Constructor): boolean { - if (value instanceof constructor) { - return true; - } - if (typeof value === 'object' && value !== null) { - // Prefer Symbol.toStringTag since it is immune to minification. - const className = constructor.prototype[Symbol.toStringTag]; - const valueClassName = - // We still need to support constructor's name to detect conflicts with older versions of this library. - Symbol.toStringTag in value - ? value[Symbol.toStringTag] - : value.constructor?.name; - if (className === valueClassName) { - const stringifiedValue = inspect(value); - throw new Error( - `Cannot use ${className} "${stringifiedValue}" 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 -relied on modules, use "resolutions" to ensure only one version is installed. - -https://yarnpkg.com/en/docs/selective-version-resolutions - -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; - }; - -interface Constructor extends Function { - prototype: { - [Symbol.toStringTag]: string; - }; -} diff --git a/src/language/source.ts b/src/language/source.ts index eb21547154..baca97f88b 100644 --- a/src/language/source.ts +++ b/src/language/source.ts @@ -1,5 +1,4 @@ import { devAssert } from '../jsutils/devAssert.js'; -import { instanceOf } from '../jsutils/instanceOf.js'; interface Location { line: number; @@ -47,5 +46,5 @@ export class Source { * @internal */ export function isSource(source: unknown): source is Source { - return instanceOf(source, Source); + return source instanceof Source; } diff --git a/src/type/definition.ts b/src/type/definition.ts index 695d46232a..abba125ab1 100644 --- a/src/type/definition.ts +++ b/src/type/definition.ts @@ -2,7 +2,6 @@ import { devAssert } from '../jsutils/devAssert.js'; import { didYouMean } from '../jsutils/didYouMean.js'; import { identityFunc } from '../jsutils/identityFunc.js'; import { inspect } from '../jsutils/inspect.js'; -import { instanceOf } from '../jsutils/instanceOf.js'; import { keyMap } from '../jsutils/keyMap.js'; import { keyValMap } from '../jsutils/keyValMap.js'; import { mapValue } from '../jsutils/mapValue.js'; @@ -78,7 +77,7 @@ export function assertType(type: unknown): GraphQLType { * There are predicates for each kind of GraphQL type. */ export function isScalarType(type: unknown): type is GraphQLScalarType { - return instanceOf(type, GraphQLScalarType); + return type instanceof GraphQLScalarType; } export function assertScalarType(type: unknown): GraphQLScalarType { @@ -89,7 +88,7 @@ export function assertScalarType(type: unknown): GraphQLScalarType { } export function isObjectType(type: unknown): type is GraphQLObjectType { - return instanceOf(type, GraphQLObjectType); + return type instanceof GraphQLObjectType; } export function assertObjectType(type: unknown): GraphQLObjectType { @@ -100,7 +99,7 @@ export function assertObjectType(type: unknown): GraphQLObjectType { } export function isInterfaceType(type: unknown): type is GraphQLInterfaceType { - return instanceOf(type, GraphQLInterfaceType); + return type instanceof GraphQLInterfaceType; } export function assertInterfaceType(type: unknown): GraphQLInterfaceType { @@ -113,7 +112,7 @@ export function assertInterfaceType(type: unknown): GraphQLInterfaceType { } export function isUnionType(type: unknown): type is GraphQLUnionType { - return instanceOf(type, GraphQLUnionType); + return type instanceof GraphQLUnionType; } export function assertUnionType(type: unknown): GraphQLUnionType { @@ -124,7 +123,7 @@ export function assertUnionType(type: unknown): GraphQLUnionType { } export function isEnumType(type: unknown): type is GraphQLEnumType { - return instanceOf(type, GraphQLEnumType); + return type instanceof GraphQLEnumType; } export function assertEnumType(type: unknown): GraphQLEnumType { @@ -137,7 +136,7 @@ export function assertEnumType(type: unknown): GraphQLEnumType { export function isInputObjectType( type: unknown, ): type is GraphQLInputObjectType { - return instanceOf(type, GraphQLInputObjectType); + return type instanceof GraphQLInputObjectType; } export function assertInputObjectType(type: unknown): GraphQLInputObjectType { @@ -157,7 +156,7 @@ export function isListType( ): type is GraphQLList; export function isListType(type: unknown): type is GraphQLList; export function isListType(type: unknown): type is GraphQLList { - return instanceOf(type, GraphQLList); + return type instanceof GraphQLList; } export function assertListType(type: unknown): GraphQLList { @@ -179,7 +178,7 @@ export function isNonNullType( export function isNonNullType( type: unknown, ): type is GraphQLNonNull { - return instanceOf(type, GraphQLNonNull); + return type instanceof GraphQLNonNull; } export function assertNonNullType( diff --git a/src/type/directives.ts b/src/type/directives.ts index 48e90c5531..9cbcb5aef5 100644 --- a/src/type/directives.ts +++ b/src/type/directives.ts @@ -1,5 +1,4 @@ import { inspect } from '../jsutils/inspect.js'; -import { instanceOf } from '../jsutils/instanceOf.js'; import type { Maybe } from '../jsutils/Maybe.js'; import { toObjMap } from '../jsutils/toObjMap.js'; @@ -22,7 +21,7 @@ import { GraphQLBoolean, GraphQLInt, GraphQLString } from './scalars.js'; * Test if the given value is a GraphQL directive. */ export function isDirective(directive: unknown): directive is GraphQLDirective { - return instanceOf(directive, GraphQLDirective); + return directive instanceof GraphQLDirective; } export function assertDirective(directive: unknown): GraphQLDirective { diff --git a/src/type/schema.ts b/src/type/schema.ts index 694454fae5..80451447de 100644 --- a/src/type/schema.ts +++ b/src/type/schema.ts @@ -1,5 +1,4 @@ import { inspect } from '../jsutils/inspect.js'; -import { instanceOf } from '../jsutils/instanceOf.js'; import type { Maybe } from '../jsutils/Maybe.js'; import type { ObjMap } from '../jsutils/ObjMap.js'; import { toObjMap } from '../jsutils/toObjMap.js'; @@ -41,7 +40,7 @@ import { * Test if the given value is a GraphQL schema. */ export function isSchema(schema: unknown): schema is GraphQLSchema { - return instanceOf(schema, GraphQLSchema); + return schema instanceof GraphQLSchema; } export function assertSchema(schema: unknown): GraphQLSchema {