Skip to content

Commit

Permalink
Stop using SYMBOL_TO_STRING_TAG polyfill.
Browse files Browse the repository at this point in the history
After rebasing PR #2894 against origin/main, I noticed that the
src/polyfills/symbols.js module no longer exists, and Symbol.toStringTag
is used directly elsewhere in the codebase.
  • Loading branch information
benjamn committed Mar 25, 2021
1 parent 213b703 commit 717f24e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
13 changes: 6 additions & 7 deletions src/jsutils/__tests__/instanceOf-test.js
Expand Up @@ -2,7 +2,6 @@ import { expect } from 'chai';
import { describe, it } from 'mocha';

import { instanceOf } from '../instanceOf';
import { SYMBOL_TO_STRING_TAG } from '../../polyfills/symbols';
import {
GraphQLScalarType,
GraphQLObjectType,
Expand Down Expand Up @@ -57,13 +56,13 @@ describe('instanceOf', () => {
}

function getTag(from: any): string {
return from[SYMBOL_TO_STRING_TAG];
return from[Symbol.toStringTag];
}

it('does not fail if dynamically-defined tags differ', () => {
checkSameNameClasses((tag) => {
class Foo {}
Object.defineProperty(Foo.prototype, SYMBOL_TO_STRING_TAG, {
Object.defineProperty(Foo.prototype, Symbol.toStringTag, {
value: tag,
});
return Foo;
Expand All @@ -73,7 +72,7 @@ describe('instanceOf', () => {
it('does not fail if dynamically-defined tag getters differ', () => {
checkSameNameClasses((tag) => {
class Foo {}
Object.defineProperty(Foo.prototype, SYMBOL_TO_STRING_TAG, {
Object.defineProperty(Foo.prototype, Symbol.toStringTag, {
get() {
return tag;
},
Expand All @@ -85,7 +84,7 @@ describe('instanceOf', () => {
it('does not fail for anonymous classes', () => {
checkSameNameClasses((tag) => {
const Foo = class {};
Object.defineProperty(Foo.prototype, SYMBOL_TO_STRING_TAG, {
Object.defineProperty(Foo.prototype, Symbol.toStringTag, {
get() {
return tag;
},
Expand All @@ -97,7 +96,7 @@ describe('instanceOf', () => {
it('does not fail if prototype property tags differ', () => {
checkSameNameClasses((tag) => {
class Foo {}
(Foo.prototype: any)[SYMBOL_TO_STRING_TAG] = tag;
(Foo.prototype: any)[Symbol.toStringTag] = tag;
return Foo;
});
});
Expand All @@ -106,7 +105,7 @@ describe('instanceOf', () => {
checkSameNameClasses((tag) => {
class Foo {
// $FlowFixMe[unsupported-syntax] Flow doesn't support computed properties yet
get [SYMBOL_TO_STRING_TAG]() {
get [Symbol.toStringTag]() {
return tag;
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/jsutils/instanceOf.js
@@ -1,5 +1,3 @@
import { SYMBOL_TO_STRING_TAG } from '../polyfills/symbols';

/**
* A replacement for instanceof which includes an error warning when multi-realm
* constructors are detected.
Expand All @@ -18,7 +16,7 @@ export const instanceOf: (mixed, mixed) => boolean =
}
if (value) {
const proto = constructor && constructor.prototype;
const classTag = proto && proto[SYMBOL_TO_STRING_TAG];
const classTag = proto && proto[Symbol.toStringTag];
const className = classTag || constructor.name;
// When the constructor class defines a Symbol.toStringTag
// property, as most classes exported by graphql-js do, use it
Expand All @@ -32,8 +30,8 @@ export const instanceOf: (mixed, mixed) => boolean =
// they could be minified to the same short string, even though
// value is legitimately _not_ instanceof constructor.
const valueName = classTag
? value[SYMBOL_TO_STRING_TAG]
: value.constructor && value.constructor.name;
? value[Symbol.toStringTag]
: value.constructor && value.constructor.name;
if (typeof className === 'string' && valueName === className) {
throw new Error(
`Cannot use ${className} "${value}" from another module or realm.
Expand Down

0 comments on commit 717f24e

Please sign in to comment.