Skip to content

Commit 8f2c383

Browse files
committed
inspect: Correctly handle custom inspect returning this
1 parent 5eb3bb6 commit 8f2c383

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/jsutils/__tests__/inspect-test.js

+10
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ describe('inspect', () => {
9696
expect(inspect(object)).to.equal('<custom inspect>');
9797
});
9898

99+
it('custom inspect that return `this` should work', () => {
100+
const object = {
101+
inspect() {
102+
return this;
103+
},
104+
};
105+
106+
expect(inspect(object)).to.equal('{ inspect: [function inspect] }');
107+
});
108+
99109
it('custom symbol inspect is take precedence', () => {
100110
invariant(nodejsCustomInspectSymbol);
101111

src/jsutils/inspect.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,13 @@ function formatValue(value, recurseTimes) {
3232
if (customInspectFn) {
3333
// $FlowFixMe(>=0.90.0)
3434
const customValue = customInspectFn.call(value);
35-
return typeof customValue === 'string'
36-
? customValue
37-
: formatValue(customValue, recurseTimes);
35+
36+
// check for infinite recursion
37+
if (customValue !== value) {
38+
return typeof customValue === 'string'
39+
? customValue
40+
: formatValue(customValue, recurseTimes);
41+
}
3842
} else if (Array.isArray(value)) {
3943
return formatArray(value, recurseTimes);
4044
}

0 commit comments

Comments
 (0)