Skip to content

Commit

Permalink
feat: replace instanceOf with unique Symbol checks
Browse files Browse the repository at this point in the history
  • Loading branch information
n1ru4l committed Jun 20, 2022
1 parent cfbc023 commit 3ec488e
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 155 deletions.
9 changes: 9 additions & 0 deletions src/error/GraphQLError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,22 @@ export interface GraphQLErrorOptions {
extensions?: Maybe<GraphQLErrorExtensions>;
}

const isGraphQLErrorSymbol = Symbol.for('GraphQLError');

export function isGraphQLError(error: unknown): error is GraphQLError {
return (
typeof error === 'object' && error != null && isGraphQLErrorSymbol in error
);
}

/**
* A GraphQLError describes an Error found during the parse, validate, or
* execute phases of performing a GraphQL operation. In addition to a message
* and stack trace, it also includes information about the locations in a
* GraphQL document and/or execution result that correspond to the Error.
*/
export class GraphQLError extends Error {
[isGraphQLErrorSymbol]: true = true;
/**
* An array of `{ line, column }` locations within the source GraphQL document
* which correspond to this error.
Expand Down
79 changes: 0 additions & 79 deletions src/jsutils/__tests__/instanceOf-test.ts

This file was deleted.

54 changes: 0 additions & 54 deletions src/jsutils/instanceOf.ts

This file was deleted.

8 changes: 6 additions & 2 deletions src/language/source.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { devAssert } from '../jsutils/devAssert';
import { instanceOf } from '../jsutils/instanceOf';

interface Location {
line: number;
column: number;
}

const isSourceSymbol = Symbol.for('Source');

/**
* A representation of source input to GraphQL. The `name` and `locationOffset` parameters are
* optional, but they are useful for clients who store GraphQL documents in source files.
Expand All @@ -14,6 +15,7 @@ interface Location {
* The `line` and `column` properties in `locationOffset` are 1-indexed.
*/
export class Source {
[isSourceSymbol]: true = true;
body: string;
name: string;
locationOffset: Location;
Expand Down Expand Up @@ -47,5 +49,7 @@ export class Source {
* @internal
*/
export function isSource(source: unknown): source is Source {
return instanceOf(source, Source);
return (
typeof source === 'object' && source != null && isSourceSymbol in source
);
}
21 changes: 21 additions & 0 deletions src/type/__tests__/definition-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,27 @@ describe('Type System: Non-Null', () => {
expectNonNull(ListOfScalarsType).to.not.throw();
expectNonNull(ListOfNonNullScalarsType).to.not.throw();
});

it('rejects a non-type as nullable type of non-null', () => {
// @ts-expect-error
expectNonNull(NonNullScalarType).to.throw(
'Expected Scalar! to be a GraphQL nullable type.',
);
// @ts-expect-error
expectNonNull({}).to.throw('Expected {} to be a GraphQL nullable type.');
// @ts-expect-error
expectNonNull(String).to.throw(
'Expected [function String] to be a GraphQL nullable type.',
);
// @ts-expect-error (must provide type)
expectNonNull(null).to.throw(
'Expected null to be a GraphQL nullable type.',
);
// @ts-expect-error (must provide type)
expectNonNull(undefined).to.throw(
'Expected undefined to be a GraphQL nullable type.',
);
});
});

describe('Type System: test utility methods', () => {
Expand Down
Loading

0 comments on commit 3ec488e

Please sign in to comment.