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

Remove Node's custom inspect function #2914

Merged
merged 1 commit into from Feb 14, 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
45 changes: 15 additions & 30 deletions src/jsutils/__tests__/inspect-test.js
Expand Up @@ -3,7 +3,6 @@ import { describe, it } from 'mocha';

import inspect from '../inspect';
import invariant from '../invariant';
import nodejsCustomInspectSymbol from '../nodejsCustomInspectSymbol';

describe('inspect', () => {
it('undefined', () => {
Expand Down Expand Up @@ -84,54 +83,40 @@ describe('inspect', () => {
expect(inspect(map)).to.equal('{ a: true, b: null }');
});

it('custom inspect', () => {
it('use toJSON if provided', () => {
const object = {
inspect() {
return '<custom inspect>';
toJSON() {
return '<json value>';
},
};

expect(inspect(object)).to.equal('<custom inspect>');
expect(inspect(object)).to.equal('<json value>');
});

it('custom inspect that return `this` should work', () => {
it('handles toJSON that return `this` should work', () => {
const object = {
inspect() {
toJSON() {
return this;
},
};

expect(inspect(object)).to.equal('{ inspect: [function inspect] }');
expect(inspect(object)).to.equal('{ toJSON: [function toJSON] }');
});

it('custom symbol inspect is take precedence', () => {
it('handles toJSON returning object values', () => {
const object = {
// istanbul ignore next (Never called and use just as a placeholder)
inspect() {
invariant(false);
},
[String(nodejsCustomInspectSymbol)]() {
return '<custom symbol inspect>';
},
};

expect(inspect(object)).to.equal('<custom symbol inspect>');
});

it('custom inspect returning object values', () => {
const object = {
inspect() {
return { custom: 'inspect' };
toJSON() {
return { json: 'value' };
},
};

expect(inspect(object)).to.equal('{ custom: "inspect" }');
expect(inspect(object)).to.equal('{ json: "value" }');
});

it('custom inspect function that uses this', () => {
it('handles toJSON function that uses this', () => {
const object = {
str: 'Hello World!',
inspect() {
toJSON() {
return this.str;
},
};
Expand Down Expand Up @@ -160,11 +145,11 @@ describe('inspect', () => {
expect(inspect(mixed)).to.equal('{ array: [[Circular]] }');

const customA = {
inspect: () => customB,
toJSON: () => customB,
};

const customB = {
inspect: () => customA,
toJSON: () => customA,
};

expect(inspect(customA)).to.equal('[Circular]');
Expand Down
19 changes: 0 additions & 19 deletions src/jsutils/defineInspect.js

This file was deleted.

26 changes: 6 additions & 20 deletions src/jsutils/inspect.js
@@ -1,5 +1,4 @@
/* eslint-disable flowtype/no-weak-types */
import nodejsCustomInspectSymbol from './nodejsCustomInspectSymbol';

const MAX_ARRAY_LENGTH = 10;
const MAX_RECURSIVE_DEPTH = 2;
Expand Down Expand Up @@ -36,16 +35,15 @@ function formatObjectValue(
}

const seenValues = [...previouslySeenValues, value];
const customInspectFn = getCustomFn(value);

if (customInspectFn !== undefined) {
const customValue = customInspectFn.call(value);
if (typeof value.toJSON === 'function') {
const jsonValue = value.toJSON(value);

// check for infinite recursion
if (customValue !== value) {
return typeof customValue === 'string'
? customValue
: formatValue(customValue, seenValues);
if (jsonValue !== value) {
return typeof jsonValue === 'string'
? jsonValue
: formatValue(jsonValue, seenValues);
}
} else if (Array.isArray(value)) {
return formatArray(value, seenValues);
Expand Down Expand Up @@ -98,18 +96,6 @@ function formatArray(array: Array<mixed>, seenValues: Array<mixed>): string {
return '[' + items.join(', ') + ']';
}

function getCustomFn(object: Object) {
const customInspectFn = object[String(nodejsCustomInspectSymbol)];

if (typeof customInspectFn === 'function') {
return customInspectFn;
}

if (typeof object.inspect === 'function') {
return object.inspect;
}
}

function getObjectTag(object: Object): string {
const tag = Object.prototype.toString
.call(object)
Expand Down
7 changes: 0 additions & 7 deletions src/jsutils/nodejsCustomInspectSymbol.js

This file was deleted.

6 changes: 0 additions & 6 deletions src/language/__tests__/lexer-test.js
@@ -1,6 +1,3 @@
// eslint-disable-next-line import/no-nodejs-modules
import { inspect as nodeInspect } from 'util';

import { expect } from 'chai';
import { describe, it } from 'mocha';

Expand Down Expand Up @@ -121,9 +118,6 @@ describe('Lexer', () => {
expect(JSON.stringify(token)).to.equal(
'{"kind":"Name","value":"foo","line":1,"column":1}',
);
expect(nodeInspect(token)).to.equal(
"{ kind: 'Name', value: 'foo', line: 1, column: 1 }",
);
expect(inspect(token)).to.equal(
'{ kind: "Name", value: "foo", line: 1, column: 1 }',
);
Expand Down
4 changes: 0 additions & 4 deletions src/language/__tests__/parser-test.js
@@ -1,6 +1,3 @@
// eslint-disable-next-line import/no-nodejs-modules
import { inspect as nodeInspect } from 'util';

import { expect } from 'chai';
import { describe, it } from 'mocha';

Expand Down Expand Up @@ -379,7 +376,6 @@ describe('Parser', () => {
const result = parse('{ id }');

expect(JSON.stringify(result.loc)).to.equal('{"start":0,"end":6}');
expect(nodeInspect(result.loc)).to.equal('{ start: 0, end: 6 }');
expect(inspect(result.loc)).to.equal('{ start: 0, end: 6 }');
});

Expand Down
8 changes: 0 additions & 8 deletions src/language/ast.js
@@ -1,5 +1,3 @@
import defineInspect from '../jsutils/defineInspect';

import type { Source } from './source';
import type { TokenKindEnum } from './tokenKind';

Expand Down Expand Up @@ -46,9 +44,6 @@ export class Location {
}
}

// Print a simplified form when appearing in `inspect` and `util.inspect`.
defineInspect(Location);

/**
* Represents a range of characters represented by a lexical token
* within a Source.
Expand Down Expand Up @@ -126,9 +121,6 @@ export class Token {
}
}

// Print a simplified form when appearing in `inspect` and `util.inspect`.
defineInspect(Token);

/**
* @internal
*/
Expand Down
25 changes: 0 additions & 25 deletions src/type/definition.js
Expand Up @@ -17,7 +17,6 @@ import instanceOf from '../jsutils/instanceOf';
import didYouMean from '../jsutils/didYouMean';
import isObjectLike from '../jsutils/isObjectLike';
import identityFunc from '../jsutils/identityFunc';
import defineInspect from '../jsutils/defineInspect';
import suggestionList from '../jsutils/suggestionList';

import { GraphQLError } from '../error/GraphQLError';
Expand Down Expand Up @@ -370,9 +369,6 @@ export class GraphQLList<+T: GraphQLType> {
}
}

// Print a simplified form when appearing in `inspect` and `util.inspect`.
defineInspect(GraphQLList);

/**
* Non-Null Type Wrapper
*
Expand Down Expand Up @@ -419,9 +415,6 @@ export class GraphQLNonNull<+T: GraphQLNullableType> {
}
}

// Print a simplified form when appearing in `inspect` and `util.inspect`.
defineInspect(GraphQLNonNull);

/**
* These types wrap and modify other types
*/
Expand Down Expand Up @@ -631,9 +624,6 @@ export class GraphQLScalarType {
}
}

// Print a simplified form when appearing in `inspect` and `util.inspect`.
defineInspect(GraphQLScalarType);

export type GraphQLScalarSerializer<TExternal> = (
outputValue: mixed,
) => ?TExternal;
Expand Down Expand Up @@ -778,9 +768,6 @@ export class GraphQLObjectType {
}
}

// Print a simplified form when appearing in `inspect` and `util.inspect`.
defineInspect(GraphQLObjectType);

function defineInterfaces(
config: $ReadOnly<
| GraphQLObjectTypeConfig<mixed, mixed>
Expand Down Expand Up @@ -1098,9 +1085,6 @@ export class GraphQLInterfaceType {
}
}

// Print a simplified form when appearing in `inspect` and `util.inspect`.
defineInspect(GraphQLInterfaceType);

export type GraphQLInterfaceTypeConfig<TSource, TContext> = {|
name: string,
description?: ?string,
Expand Down Expand Up @@ -1208,9 +1192,6 @@ export class GraphQLUnionType {
}
}

// Print a simplified form when appearing in `inspect` and `util.inspect`.
defineInspect(GraphQLUnionType);

function defineTypes(
config: $ReadOnly<GraphQLUnionTypeConfig<mixed, mixed>>,
): Array<GraphQLObjectType> {
Expand Down Expand Up @@ -1389,9 +1370,6 @@ export class GraphQLEnumType /* <T> */ {
}
}

// Print a simplified form when appearing in `inspect` and `util.inspect`.
defineInspect(GraphQLEnumType);

function didYouMeanEnumValue(
enumType: GraphQLEnumType,
unknownValueStr: string,
Expand Down Expand Up @@ -1541,9 +1519,6 @@ export class GraphQLInputObjectType {
}
}

// Print a simplified form when appearing in `inspect` and `util.inspect`.
defineInspect(GraphQLInputObjectType);

function defineInputFieldMap(
config: $ReadOnly<GraphQLInputObjectTypeConfig>,
): GraphQLInputFieldMap {
Expand Down
4 changes: 0 additions & 4 deletions src/type/directives.js
Expand Up @@ -6,7 +6,6 @@ import toObjMap from '../jsutils/toObjMap';
import devAssert from '../jsutils/devAssert';
import instanceOf from '../jsutils/instanceOf';
import isObjectLike from '../jsutils/isObjectLike';
import defineInspect from '../jsutils/defineInspect';

import type { DirectiveDefinitionNode } from '../language/ast';
import type { DirectiveLocationEnum } from '../language/directiveLocation';
Expand Down Expand Up @@ -109,9 +108,6 @@ export class GraphQLDirective {
}
}

// Print a simplified form when appearing in `inspect` and `util.inspect`.
defineInspect(GraphQLDirective);

export type GraphQLDirectiveConfig = {|
name: string,
description?: ?string,
Expand Down