Skip to content

Commit a6a74ae

Browse files
BridgeARcodebytere
authored andcommitted
console: name console functions appropriately
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]>
1 parent 9d24f71 commit a6a74ae

File tree

5 files changed

+67
-2
lines changed

5 files changed

+67
-2
lines changed

lib/internal/console/constructor.js

+3
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
136136
// the prototype so that users extending the Console can override them
137137
// from the prototype chain of the subclass.
138138
this[key] = this[key].bind(this);
139+
ObjectDefineProperty(this[key], 'name', {
140+
value: key
141+
});
139142
}
140143

141144
this[kBindStreamsEager](stdout, stderr);

lib/internal/console/global.js

+2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ for (const prop of ReflectOwnKeys(Console.prototype)) {
3636
if (prop === 'constructor') { continue; }
3737
const desc = ReflectGetOwnPropertyDescriptor(Console.prototype, prop);
3838
if (typeof desc.value === 'function') { // fix the receiver
39+
const name = desc.value.name;
3940
desc.value = desc.value.bind(globalConsole);
41+
ReflectDefineProperty(desc.value, 'name', { value: name });
4042
}
4143
ReflectDefineProperty(globalConsole, prop, desc);
4244
}

lib/internal/util/inspector.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const {
4+
ObjectDefineProperty,
45
ObjectKeys,
56
} = primordials;
67

@@ -42,6 +43,9 @@ function wrapConsole(consoleFromNode, consoleFromVM) {
4243
consoleFromNode[key] = consoleCall.bind(consoleFromNode,
4344
consoleFromVM[key],
4445
consoleFromNode[key]);
46+
ObjectDefineProperty(consoleFromNode[key], 'name', {
47+
value: key
48+
});
4549
} else {
4650
// Add additional console APIs from the inspector
4751
consoleFromNode[key] = consoleFromVM[key];

test/parallel/test-console-methods.js

+25-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22
require('../common');
33

4-
// This test ensures that console methods
5-
// cannot be invoked as constructors
4+
// This test ensures that console methods cannot be invoked as constructors and
5+
// that their name is always correct.
66

77
const assert = require('assert');
88

@@ -32,7 +32,30 @@ const methods = [
3232
'groupCollapsed',
3333
];
3434

35+
const alternateNames = {
36+
debug: 'log',
37+
info: 'log',
38+
dirxml: 'log',
39+
error: 'warn',
40+
groupCollapsed: 'group'
41+
};
42+
43+
function assertEqualName(method) {
44+
try {
45+
assert.strictEqual(console[method].name, method);
46+
} catch {
47+
assert.strictEqual(console[method].name, alternateNames[method]);
48+
}
49+
try {
50+
assert.strictEqual(newInstance[method].name, method);
51+
} catch {
52+
assert.strictEqual(newInstance[method].name, alternateNames[method]);
53+
}
54+
}
55+
3556
for (const method of methods) {
57+
assertEqualName(method);
58+
3659
assert.throws(() => new console[method](), err);
3760
assert.throws(() => new newInstance[method](), err);
3861
assert.throws(() => Reflect.construct({}, [], console[method]), err);

test/parallel/test-repl.js

+33
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,39 @@ const errorTests = [
754754
/^Uncaught SyntaxError: /
755755
]
756756
},
757+
{
758+
send: 'console',
759+
expect: [
760+
'{',
761+
' log: [Function: log],',
762+
' warn: [Function: warn],',
763+
' dir: [Function: dir],',
764+
' time: [Function: time],',
765+
' timeEnd: [Function: timeEnd],',
766+
' timeLog: [Function: timeLog],',
767+
' trace: [Function: trace],',
768+
' assert: [Function: assert],',
769+
' clear: [Function: clear],',
770+
' count: [Function: count],',
771+
' countReset: [Function: countReset],',
772+
' group: [Function: group],',
773+
' groupEnd: [Function: groupEnd],',
774+
' table: [Function: table],',
775+
/ debug: \[Function: (debug|log)],/,
776+
/ info: \[Function: (info|log)],/,
777+
/ dirxml: \[Function: (dirxml|log)],/,
778+
/ error: \[Function: (error|warn)],/,
779+
/ groupCollapsed: \[Function: (groupCollapsed|group)],/,
780+
/ Console: \[Function: Console],?/,
781+
...process.features.inspector ? [
782+
' profile: [Function: profile],',
783+
' profileEnd: [Function: profileEnd],',
784+
' timeStamp: [Function: timeStamp],',
785+
' context: [Function: context]',
786+
] : [],
787+
'}',
788+
]
789+
},
757790
];
758791

759792
const tcpTests = [

0 commit comments

Comments
 (0)