Skip to content

Commit 27823f6

Browse files
authored
feat: drop support for non-symbol inspect properties (#114)
1 parent b5b084b commit 27823f6

File tree

2 files changed

+11
-20
lines changed

2 files changed

+11
-20
lines changed

src/index.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,6 @@ const inspectCustom = (value: object, options: Options, type: string, inspectFn:
9696
return ((value as any)[nodeInspect] as Function)(options.depth, options, inspectFn)
9797
}
9898

99-
if ('inspect' in value && typeof value.inspect === 'function') {
100-
return value.inspect(options.depth, options)
101-
}
102-
10399
if ('constructor' in value && constructorMap.has(value.constructor)) {
104100
return constructorMap.get(value.constructor)!(value, options)
105101
}

test/acceptance.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,45 +19,40 @@ describe('objects', () => {
1919
expect(inspect({ foo: 1, [Symbol('foo')]: 1 })).to.equal('{ foo: 1, [Symbol(foo)]: 1 }')
2020
})
2121

22-
it('does not use custom inspect functions if `customInspect` is turned off', () => {
22+
it('does not treat inspect members as inspect functions', () => {
2323
const obj = {
2424
inspect: () => 1,
2525
}
26-
expect(inspect(obj, { customInspect: false })).to.equal('{ inspect: [Function inspect] }')
27-
})
28-
29-
it('uses custom inspect function is `customInspect` is turned on', () => {
30-
const obj = {
31-
inspect: () => 1,
32-
}
33-
expect(inspect(obj, { customInspect: true })).to.equal('1')
26+
expect(inspect(obj)).to.equal('{ inspect: [Function inspect] }')
3427
})
3528

3629
it('does not use custom inspect functions if `customInspect` is turned off', () => {
3730
const obj = {
38-
inspect: () => 1,
31+
[Symbol.for('nodejs.util.inspect.custom')]: () => 1,
3932
}
40-
expect(inspect(obj, { customInspect: false })).to.equal('{ inspect: [Function inspect] }')
33+
expect(inspect(obj, { customInspect: false })).to.equal(
34+
'{ [Symbol(nodejs.util.inspect.custom)]: [Function [nodejs.util.inspect.custom]] }'
35+
)
4136
})
4237

4338
it('uses custom inspect function if `customInspect` is turned on', () => {
4439
const obj = {
45-
inspect: () => 'abc',
40+
[Symbol.for('nodejs.util.inspect.custom')]: () => 1,
4641
}
47-
expect(inspect(obj, { customInspect: true })).to.equal('abc')
42+
expect(inspect(obj, { customInspect: true })).to.equal('1')
4843
})
4944

5045
it('inspects custom inspect function result', () => {
5146
const obj = {
52-
inspect: () => ['foobarbazbing'],
47+
[Symbol.for('nodejs.util.inspect.custom')]: () => ['foobarbazbing'],
5348
}
5449
expect(inspect(obj, { customInspect: true, truncate: 15 })).to.equal("[ 'foobarba…' ]")
5550
})
5651

5752
it('uses a custom deeply nested inspect function if `customInspect` is turned on', () => {
5853
const obj = {
5954
sub: {
60-
inspect: (depth, options) => options.stylize('Object content', 'string'),
55+
[Symbol.for('nodejs.util.inspect.custom')]: (depth, options) => options.stylize('Object content', 'string'),
6156
},
6257
}
6358
expect(inspect(obj, { customInspect: true })).to.equal('{ sub: Object content }')
@@ -66,7 +61,7 @@ describe('objects', () => {
6661
it('inspect with custom object-returning inspect', () => {
6762
const obj = {
6863
sub: {
69-
inspect: () => ({ foo: 'bar' }),
64+
[Symbol.for('nodejs.util.inspect.custom')]: () => ({ foo: 'bar' }),
7065
},
7166
}
7267

0 commit comments

Comments
 (0)