From 7eeb7948b3a9f29334612a79f349178888df5f7e Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Thu, 12 Jul 2018 16:56:15 +0200 Subject: console: fix timeEnd() not coercing the input MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The input of console.timeEnd() was not coerced to a string. That way labels were not found and the entry was not removed anymore. PR-URL: https://github.com/nodejs/node/pull/21779 Reviewed-By: James M Snell Reviewed-By: Michaƫl Zasso Reviewed-By: Colin Ihrig Reviewed-By: Tiancheng "Timothy" Gu Reviewed-By: Luigi Pinca --- lib/console.js | 6 ++++-- test/parallel/test-console.js | 6 +++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/console.js b/lib/console.js index 92ac64173a..665a2a6a23 100644 --- a/lib/console.js +++ b/lib/console.js @@ -236,6 +236,8 @@ Console.prototype.time = function time(label = 'default') { }; Console.prototype.timeEnd = function timeEnd(label = 'default') { + // Coerces everything other than Symbol to a string + label = `${label}`; const hasWarned = timeLogImpl(this, 'timeEnd', label); if (!hasWarned) { this._times.delete(label); @@ -243,13 +245,13 @@ Console.prototype.timeEnd = function timeEnd(label = 'default') { }; Console.prototype.timeLog = function timeLog(label, ...data) { + // Coerces everything other than Symbol to a string + label = `${label}`; timeLogImpl(this, 'timeLog', label, data); }; // Returns true if label was not found function timeLogImpl(self, name, label = 'default', data) { - // Coerces everything other than Symbol to a string - label = `${label}`; const time = self._times.get(label); if (!time) { process.emitWarning(`No such label '${label}' for console.${name}()`); diff --git a/test/parallel/test-console.js b/test/parallel/test-console.js index 6f06ea0954..d6064016b5 100644 --- a/test/parallel/test-console.js +++ b/test/parallel/test-console.js @@ -135,11 +135,14 @@ console.timeEnd('constructor'); console.time('hasOwnProperty'); console.timeEnd('hasOwnProperty'); -// verify that values are coerced to strings +// Verify that values are coerced to strings. console.time([]); console.timeEnd([]); console.time({}); console.timeEnd({}); +// Repeat the object call to verify that everything really worked. +console.time({}); +console.timeEnd({}); console.time(null); console.timeEnd(null); console.time(undefined); @@ -224,6 +227,7 @@ assert.ok(/^hasOwnProperty: \d+\.\d{3}ms$/.test(strings.shift().trim())); // verify that console.time() coerces label values to strings as expected assert.ok(/^: \d+\.\d{3}ms$/.test(strings.shift().trim())); assert.ok(/^\[object Object\]: \d+\.\d{3}ms$/.test(strings.shift().trim())); +assert.ok(/^\[object Object\]: \d+\.\d{3}ms$/.test(strings.shift().trim())); assert.ok(/^null: \d+\.\d{3}ms$/.test(strings.shift().trim())); assert.ok(/^default: \d+\.\d{3}ms$/.test(strings.shift().trim())); assert.ok(/^default: \d+\.\d{3}ms$/.test(strings.shift().trim())); -- cgit v1.2.3