Skip to content

Commit

Permalink
Fix: update custom inspection to use a symbol
Browse files Browse the repository at this point in the history
Node.js 11 removed support for the `inspect` property on objects as
custom inspect functions. Instead, this can be done by using the
util.inspect.custom symbol that is used here instead. To keep it
backwarts compatible the old function stays in place.
  • Loading branch information
BridgeAR committed May 28, 2018
1 parent a1c2e82 commit d57267a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var path = require('path');
var util = require('util');
var isBuffer = require('buffer').Buffer.isBuffer;

var clone = require('clone');
Expand Down Expand Up @@ -157,6 +158,11 @@ File.prototype.inspect = function() {
return '<File ' + inspect.join(' ') + '>';
};

// Newer Node.js versions use this symbol for custom inspection.
if (util.inspect.custom) {
File.prototype[util.inspect.custom] = File.prototype.inspect;
}

File.isCustomProp = function(key) {
return builtInFields.indexOf(key) === -1;
};
Expand Down
6 changes: 5 additions & 1 deletion test/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var fs = require('fs');
var path = require('path');
var util = require('util');
var expect = require('expect');
var miss = require('mississippi');
var cloneable = require('cloneable-readable');
Expand Down Expand Up @@ -746,7 +747,10 @@ describe('File', function() {

it('returns correct format when no contents and no path', function(done) {
var file = new File();
expect(file.inspect()).toEqual('<File >');
var expectation = '<File >';
expect(file.inspect()).toEqual(expectation);
expect(file[util.inspect.custom]()).toEqual(expectation);
expect(util.inspect(file)).toEqual(expectation);
done();
});

Expand Down

0 comments on commit d57267a

Please sign in to comment.