Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

console: coerce label to string in console.time() #14643

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions doc/api/console.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ See [`util.format()`][] for more information.
<!-- YAML
added: v0.1.104
-->
* `label` {string}
* `label` {string} Defaults to `'default'`.

Starts a timer that can be used to compute the duration of an operation. Timers
are identified by a unique `label`. Use the same `label` when calling
Expand All @@ -337,7 +337,7 @@ changes:
description: This method no longer supports multiple calls that don’t map
to individual `console.time()` calls; see below for details.
-->
* `label` {string}
* `label` {string} Defaults to `'default'`.

Stops a timer that was previously started by calling [`console.time()`][] and
prints the result to `stdout`:
Expand Down
6 changes: 4 additions & 2 deletions lib/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,14 @@ Console.prototype.dir = function dir(object, options) {
};


Console.prototype.time = function time(label) {
Console.prototype.time = function time(label = 'default') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is okay because of the method binding in L62?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean? It only binds the this value. And for the strictest compliance method.length === 0 because the IDL specifies label as an optional parameter.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would have changed the length unless it was bound, which some (@jasnell) consider semver-major.
It does change console.__proto__.time.length a.k.a. console.Console.prototype.time.length.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW I do think this is semver-major, but because of the default change not .length.

label = `${label}`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not simply String(label)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@refack I guess it's because String(Symbol('s')) does not throw.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What a weird language 😖

Copy link
Contributor

@refack refack Aug 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found the piece I was missing, the console IDL - https://console.spec.whatwg.org/#console-namespace
@jasnell could you find a suitable place to reference that in the code?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, IDL rules are weird. Before ES6 you could also use label + '' but not anymore due to some @@toPrimitive semantics that wouldn't work with Dates.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can write a line of comment to explain this? Or maybe next time who see it will be confused again.

this._times.set(label, process.hrtime());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I would just use the template literal in this line and spare the assignment above.

};


Console.prototype.timeEnd = function timeEnd(label) {
Console.prototype.timeEnd = function timeEnd(label = 'default') {
label = `${label}`;
const time = this._times.get(label);
if (!time) {
process.emitWarning(`No such label '${label}' for console.timeEnd()`);
Expand Down
20 changes: 20 additions & 0 deletions test/parallel/test-console.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ assert.doesNotThrow(function() {
console.timeEnd('label');
});

assert.throws(() => console.time(Symbol('test')),
/^TypeError: Cannot convert a Symbol value to a string$/);
assert.throws(() => console.timeEnd(Symbol('test')),
/^TypeError: Cannot convert a Symbol value to a string$/);


// an Object with a custom .inspect() function
const custom_inspect = { foo: 'bar', inspect: () => 'inspect' };

Expand Down Expand Up @@ -103,6 +109,20 @@ console.timeEnd('constructor');
console.time('hasOwnProperty');
console.timeEnd('hasOwnProperty');

// verify that values are coerced to strings
console.time([]);
console.timeEnd([]);
console.time({});
console.timeEnd({});
console.time(null);
console.timeEnd(null);
console.time(undefined);
console.timeEnd('default');
console.time('default');
console.timeEnd();
console.time(NaN);
console.timeEnd(NaN);

assert.strictEqual(strings.length, process.stdout.writeTimes);
assert.strictEqual(errStrings.length, process.stderr.writeTimes);
common.restoreStdout();
Expand Down