Skip to content

Commit

Permalink
util: eliminate unnecessary exports
Browse files Browse the repository at this point in the history
The getHiddenValue and setHiddenValue functions
are exported from internalUtil for no really good
reason

PR-URL: #11451
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
jasnell authored and addaleax committed Feb 22, 2017
1 parent 1d86a9f commit 95bee8f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 25 deletions.
9 changes: 3 additions & 6 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ const prefix = `(${process.release.name}:${process.pid}) `;
const kArrowMessagePrivateSymbolIndex = binding['arrow_message_private_symbol'];
const kDecoratedPrivateSymbolIndex = binding['decorated_private_symbol'];

exports.getHiddenValue = binding.getHiddenValue;
exports.setHiddenValue = binding.setHiddenValue;

// The `buffer` module uses this. Defining it here instead of in the public
// `util` module makes it accessible without having to `require('util')` there.
exports.customInspectSymbol = Symbol('util.inspect.custom');
Expand Down Expand Up @@ -77,14 +74,14 @@ exports._deprecate = function(fn, msg) {

exports.decorateErrorStack = function decorateErrorStack(err) {
if (!(exports.isError(err) && err.stack) ||
exports.getHiddenValue(err, kDecoratedPrivateSymbolIndex) === true)
binding.getHiddenValue(err, kDecoratedPrivateSymbolIndex) === true)
return;

const arrow = exports.getHiddenValue(err, kArrowMessagePrivateSymbolIndex);
const arrow = binding.getHiddenValue(err, kArrowMessagePrivateSymbolIndex);

if (arrow) {
err.stack = arrow + err.stack;
exports.setHiddenValue(err, kDecoratedPrivateSymbolIndex, true);
binding.setHiddenValue(err, kDecoratedPrivateSymbolIndex, true);
}
};

Expand Down
26 changes: 14 additions & 12 deletions test/parallel/test-internal-util-decorate-error-stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ const path = require('path');
const kArrowMessagePrivateSymbolIndex = binding['arrow_message_private_symbol'];
const kDecoratedPrivateSymbolIndex = binding['decorated_private_symbol'];

const decorateErrorStack = internalUtil.decorateErrorStack;

assert.doesNotThrow(function() {
internalUtil.decorateErrorStack();
internalUtil.decorateErrorStack(null);
internalUtil.decorateErrorStack(1);
internalUtil.decorateErrorStack(true);
decorateErrorStack();
decorateErrorStack(null);
decorateErrorStack(1);
decorateErrorStack(true);
});

// Verify that a stack property is not added to non-Errors
const obj = {};
internalUtil.decorateErrorStack(obj);
decorateErrorStack(obj);
assert.strictEqual(obj.stack, undefined);

// Verify that the stack is decorated when possible
Expand All @@ -43,8 +45,8 @@ assert(typeof err, 'object');
checkStack(err.stack);

// Verify that the stack is only decorated once
internalUtil.decorateErrorStack(err);
internalUtil.decorateErrorStack(err);
decorateErrorStack(err);
decorateErrorStack(err);
checkStack(err.stack);

// Verify that the stack is only decorated once for uncaught exceptions
Expand All @@ -58,7 +60,7 @@ checkStack(result.stderr);
// Verify that the stack is unchanged when there is no arrow message
err = new Error('foo');
let originalStack = err.stack;
internalUtil.decorateErrorStack(err);
decorateErrorStack(err);
assert.strictEqual(originalStack, err.stack);

// Verify that the arrow message is added to the start of the stack when it
Expand All @@ -67,9 +69,9 @@ const arrowMessage = 'arrow_message';
err = new Error('foo');
originalStack = err.stack;

internalUtil.setHiddenValue(err, kArrowMessagePrivateSymbolIndex, arrowMessage);
internalUtil.decorateErrorStack(err);
binding.setHiddenValue(err, kArrowMessagePrivateSymbolIndex, arrowMessage);
decorateErrorStack(err);

assert.strictEqual(err.stack, `${arrowMessage}${originalStack}`);
assert.strictEqual(internalUtil
.getHiddenValue(err, kDecoratedPrivateSymbolIndex), true);
assert.strictEqual(
binding.getHiddenValue(err, kDecoratedPrivateSymbolIndex), true);
13 changes: 6 additions & 7 deletions test/parallel/test-util-internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@
const common = require('../common');
const path = require('path');
const assert = require('assert');
const internalUtil = require('internal/util');
const spawnSync = require('child_process').spawnSync;

const binding = process.binding('util');
const kArrowMessagePrivateSymbolIndex = binding['arrow_message_private_symbol'];

function getHiddenValue(obj, index) {
return function() {
internalUtil.getHiddenValue(obj, index);
binding.getHiddenValue(obj, index);
};
}

function setHiddenValue(obj, index, val) {
return function() {
internalUtil.setHiddenValue(obj, index, val);
binding.setHiddenValue(obj, index, val);
};
}

Expand All @@ -31,7 +30,7 @@ assert.throws(getHiddenValue({}), /index must be an uint32/);
assert.throws(getHiddenValue({}, null), /index must be an uint32/);
assert.throws(getHiddenValue({}, []), /index must be an uint32/);
assert.deepStrictEqual(
internalUtil.getHiddenValue({}, kArrowMessagePrivateSymbolIndex),
binding.getHiddenValue({}, kArrowMessagePrivateSymbolIndex),
undefined);

assert.throws(setHiddenValue(), /obj must be an object/);
Expand All @@ -44,10 +43,10 @@ assert.throws(setHiddenValue({}, null), /index must be an uint32/);
assert.throws(setHiddenValue({}, []), /index must be an uint32/);
const obj = {};
assert.strictEqual(
internalUtil.setHiddenValue(obj, kArrowMessagePrivateSymbolIndex, 'bar'),
binding.setHiddenValue(obj, kArrowMessagePrivateSymbolIndex, 'bar'),
true);
assert.strictEqual(
internalUtil.getHiddenValue(obj, kArrowMessagePrivateSymbolIndex),
binding.getHiddenValue(obj, kArrowMessagePrivateSymbolIndex),
'bar');

let arrowMessage;
Expand All @@ -56,7 +55,7 @@ try {
require(path.join(common.fixturesDir, 'syntax', 'bad_syntax'));
} catch (err) {
arrowMessage =
internalUtil.getHiddenValue(err, kArrowMessagePrivateSymbolIndex);
binding.getHiddenValue(err, kArrowMessagePrivateSymbolIndex);
}

assert(/bad_syntax\.js:1/.test(arrowMessage));
Expand Down

0 comments on commit 95bee8f

Please sign in to comment.