Skip to content

Commit

Permalink
inspect: Correctly handle custom inspect returning this
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Mar 15, 2019
1 parent ad35c41 commit 97a2e98
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/jsutils/__tests__/inspect-test.js
Expand Up @@ -96,6 +96,16 @@ describe('inspect', () => {
expect(inspect(object)).to.equal('<custom inspect>');
});

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

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

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

Expand Down
10 changes: 7 additions & 3 deletions src/jsutils/inspect.js
Expand Up @@ -32,9 +32,13 @@ function formatValue(value, recurseTimes) {
if (customInspectFn) {
// $FlowFixMe(>=0.90.0)
const customValue = customInspectFn.call(value);
return typeof customValue === 'string'
? customValue
: formatValue(customValue, recurseTimes);

// check for infinite recursion
if (customValue !== value) {
return typeof customValue === 'string'
? customValue
: formatValue(customValue, recurseTimes);
}
} else if (Array.isArray(value)) {
return formatArray(value, recurseTimes);
}
Expand Down

0 comments on commit 97a2e98

Please sign in to comment.