Skip to content

Commit

Permalink
console: console.countReset() should emit warning
Browse files Browse the repository at this point in the history
The Console Standard specifies that console.countReset()
should emit some type of a warning when given a label that
has no previous account associated with it. This PR brings
node's implementation of console.countReset() up-to-spec and
adds a test asserting that a warning is emitted.

Fixes: #20524
  • Loading branch information
domfarolino committed Jul 4, 2018
1 parent b016745 commit ac53ee5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
11 changes: 7 additions & 4 deletions lib/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
if (typeof colorMode !== 'boolean' && colorMode !== 'auto')
throw new ERR_INVALID_ARG_VALUE('colorMode', colorMode);

// Corresponds to https://console.spec.whatwg.org/#count-map
this[kCounts] = new Map();
this[kColorMode] = colorMode;

Expand Down Expand Up @@ -308,12 +309,14 @@ Console.prototype.count = function count(label = 'default') {
this.log(`${label}: ${count}`);
};

// Not yet defined by the https://console.spec.whatwg.org, but
// proposed to be added and currently implemented by Edge. Having
// the ability to reset counters is important to help prevent
// the counter from being a memory leak.
// Defined by: https://console.spec.whatwg.org/#countreset
Console.prototype.countReset = function countReset(label = 'default') {
const counts = this[kCounts];
if (!counts.has(label)) {
process.emitWarning(`Count for '${label}' does not exist`);
return;
}

counts.delete(`${label}`);
};

Expand Down
6 changes: 4 additions & 2 deletions test/parallel/test-console.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ if (common.isMainThread) {
common.expectWarning(
'Warning',
[
['No such label \'nolabel\' for console.timeEnd()', common.noWarnCode],
['Count for \'noLabel\' does not exist', common.noWarnCode],
['No such label \'nolabel\' for console.timeLog()', common.noWarnCode],
['No such label \'nolabel\' for console.timeEnd()', common.noWarnCode],
['Label \'test\' already exists for console.time()', common.noWarnCode]
]
);

console.timeEnd('nolabel');
console.countReset('noLabel');
console.timeLog('nolabel');
console.timeEnd('nolabel');

console.time('label');
console.timeEnd('label');
Expand Down

0 comments on commit ac53ee5

Please sign in to comment.