Skip to content

Commit

Permalink
console: name console functions appropriately
Browse files Browse the repository at this point in the history
The current name of most of the global console functions is
"bound consoleCall". This is changed to the actual functions name
e.g., "log" or "error".

Signed-off-by: Ruben Bridgewater <[email protected]>

PR-URL: #33524
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anto Aravinth <[email protected]>
  • Loading branch information
BridgeAR authored and codebytere committed Jun 18, 2020
1 parent 9d24f71 commit a6a74ae
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/internal/console/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
// the prototype so that users extending the Console can override them
// from the prototype chain of the subclass.
this[key] = this[key].bind(this);
ObjectDefineProperty(this[key], 'name', {
value: key
});
}

this[kBindStreamsEager](stdout, stderr);
Expand Down
2 changes: 2 additions & 0 deletions lib/internal/console/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ for (const prop of ReflectOwnKeys(Console.prototype)) {
if (prop === 'constructor') { continue; }
const desc = ReflectGetOwnPropertyDescriptor(Console.prototype, prop);
if (typeof desc.value === 'function') { // fix the receiver
const name = desc.value.name;
desc.value = desc.value.bind(globalConsole);
ReflectDefineProperty(desc.value, 'name', { value: name });
}
ReflectDefineProperty(globalConsole, prop, desc);
}
Expand Down
4 changes: 4 additions & 0 deletions lib/internal/util/inspector.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const {
ObjectDefineProperty,
ObjectKeys,
} = primordials;

Expand Down Expand Up @@ -42,6 +43,9 @@ function wrapConsole(consoleFromNode, consoleFromVM) {
consoleFromNode[key] = consoleCall.bind(consoleFromNode,
consoleFromVM[key],
consoleFromNode[key]);
ObjectDefineProperty(consoleFromNode[key], 'name', {
value: key
});
} else {
// Add additional console APIs from the inspector
consoleFromNode[key] = consoleFromVM[key];
Expand Down
27 changes: 25 additions & 2 deletions test/parallel/test-console-methods.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';
require('../common');

// This test ensures that console methods
// cannot be invoked as constructors
// This test ensures that console methods cannot be invoked as constructors and
// that their name is always correct.

const assert = require('assert');

Expand Down Expand Up @@ -32,7 +32,30 @@ const methods = [
'groupCollapsed',
];

const alternateNames = {
debug: 'log',
info: 'log',
dirxml: 'log',
error: 'warn',
groupCollapsed: 'group'
};

function assertEqualName(method) {
try {
assert.strictEqual(console[method].name, method);
} catch {
assert.strictEqual(console[method].name, alternateNames[method]);
}
try {
assert.strictEqual(newInstance[method].name, method);
} catch {
assert.strictEqual(newInstance[method].name, alternateNames[method]);
}
}

for (const method of methods) {
assertEqualName(method);

assert.throws(() => new console[method](), err);
assert.throws(() => new newInstance[method](), err);
assert.throws(() => Reflect.construct({}, [], console[method]), err);
Expand Down
33 changes: 33 additions & 0 deletions test/parallel/test-repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,39 @@ const errorTests = [
/^Uncaught SyntaxError: /
]
},
{
send: 'console',
expect: [
'{',
' log: [Function: log],',
' warn: [Function: warn],',
' dir: [Function: dir],',
' time: [Function: time],',
' timeEnd: [Function: timeEnd],',
' timeLog: [Function: timeLog],',
' trace: [Function: trace],',
' assert: [Function: assert],',
' clear: [Function: clear],',
' count: [Function: count],',
' countReset: [Function: countReset],',
' group: [Function: group],',
' groupEnd: [Function: groupEnd],',
' table: [Function: table],',
/ debug: \[Function: (debug|log)],/,
/ info: \[Function: (info|log)],/,
/ dirxml: \[Function: (dirxml|log)],/,
/ error: \[Function: (error|warn)],/,
/ groupCollapsed: \[Function: (groupCollapsed|group)],/,
/ Console: \[Function: Console],?/,
...process.features.inspector ? [
' profile: [Function: profile],',
' profileEnd: [Function: profileEnd],',
' timeStamp: [Function: timeStamp],',
' context: [Function: context]',
] : [],
'}',
]
},
];

const tcpTests = [
Expand Down

0 comments on commit a6a74ae

Please sign in to comment.