Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/jsutils/__tests__/inspect-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe('inspect', () => {
expect(inspect(object)).to.equal('<custom inspect>');
});

it('custom inspect', () => {
it('custom symbol inspect is take precedence', () => {
invariant(nodejsCustomInspectSymbol);

const object = {
Expand All @@ -92,4 +92,25 @@ describe('inspect', () => {

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

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

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

it('custom inspect function that uses this', () => {
const object = {
str: 'Hello World!',
inspect() {
return this.str;
},
};

expect(inspect(object)).to.equal('Hello World!');
});
});
24 changes: 19 additions & 5 deletions src/jsutils/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ export default function inspect(value: mixed): string {
return value.name ? `[function ${value.name}]` : '[function]';
case 'object':
if (value) {
const customInspectFn = value[String(nodejsCustomInspectSymbol)];
if (typeof customInspectFn === 'function') {
return customInspectFn();
} else if (typeof value.inspect === 'function') {
return value.inspect();
const customInspectFn = getCustomFn(value);

if (customInspectFn) {
const customValue = customInspectFn.call(value);
return typeof customValue === 'string'
? customValue
: inspect(customValue);
} else if (Array.isArray(value)) {
return '[' + value.map(inspect).join(', ') + ']';
}
Expand All @@ -39,3 +41,15 @@ export default function inspect(value: mixed): string {
return String(value);
}
}

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

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

if (typeof object.inspect === 'function') {
return object.inspect;
}
}
10 changes: 7 additions & 3 deletions src/language/__tests__/lexer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
* @flow strict
*/

import { inspect as utilInspect } from 'util';
import { inspect as nodeInspect } from 'util';

import { expect } from 'chai';
import { describe, it } from 'mocha';
import dedent from '../../jsutils/dedent';
import inspect from '../../jsutils/inspect';
import { GraphQLError } from '../../error';
import { Source } from '../source';
import { createLexer, TokenKind } from '../lexer';
Expand Down Expand Up @@ -62,14 +63,17 @@ describe('Lexer', () => {
});
});

it('can be JSON.stringified or util.inspected', () => {
it('can be JSON.stringified, util.inspected or jsutils.inspect', () => {
const token = lexOne('foo');
expect(JSON.stringify(token)).to.equal(
'{"kind":"Name","value":"foo","line":1,"column":1}',
);
expect(utilInspect(token)).to.equal(
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 }',
);
});

it('skips whitespace and comments', () => {
Expand Down
4 changes: 3 additions & 1 deletion src/language/__tests__/parser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @flow strict
*/

import { inspect } from 'util';
import { inspect as nodeInspect } from 'util';
import { readFileSync } from 'fs';
import { join } from 'path';

Expand All @@ -18,6 +18,7 @@ import { TokenKind } from '../lexer';
import { parse, parseValue, parseType } from '../parser';
import { Source } from '../source';
import dedent from '../../jsutils/dedent';
import inspect from '../../jsutils/inspect';
import toJSONDeep from './toJSONDeep';

function expectSyntaxError(text, message, location) {
Expand Down Expand Up @@ -391,6 +392,7 @@ 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