Skip to content

Commit 5fb30dc

Browse files
author
Ruben Bridgewater
committed
fixup: add more tests, fix edge case, rework output
Signed-off-by: Ruben Bridgewater <[email protected]>
1 parent 12294e4 commit 5fb30dc

File tree

3 files changed

+41
-28
lines changed

3 files changed

+41
-28
lines changed

lib/internal/util/inspect.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -863,10 +863,7 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
863863
return `${braces[0]}}`;
864864
}
865865
} else if (typeof value === 'function') {
866-
const matches = FunctionPrototypeToString(value).match(classRegExp);
867-
base = matches ?
868-
getClassBase(matches[1], value, constructor, tag) :
869-
getFunctionBase(value, constructor, tag);
866+
base = getFunctionBase(value, constructor, tag);
870867
if (keys.length === 0 && protoProps === undefined)
871868
return ctx.stylize(base, 'special');
872869
} else if (isRegExp(value)) {
@@ -1060,7 +1057,6 @@ function getBoxedBase(value, ctx, keys, constructor, tag) {
10601057
}
10611058

10621059
function getClassBase(string, value, constructor, tag) {
1063-
string = string.replace(stripCommentsRegExp, '');
10641060
const parts = string.split(/\s+/);
10651061
if (parts[1] === 'extends') {
10661062
parts[3] = parts[2];
@@ -1090,10 +1086,19 @@ function getClassBase(string, value, constructor, tag) {
10901086
if (superClass !== undefined) {
10911087
base += ` extends ${superClass}`;
10921088
}
1093-
return `${base} {}`;
1089+
return `[${base}]`;
10941090
}
10951091

10961092
function getFunctionBase(value, constructor, tag) {
1093+
const stringified = FunctionPrototypeToString(value);
1094+
if (stringified.slice(0, 5) === 'class') {
1095+
const match = stringified
1096+
.replace(stripCommentsRegExp, ' ')
1097+
.match(classRegExp);
1098+
if (match !== null) {
1099+
return getClassBase(match[1], value, constructor, tag);
1100+
}
1101+
}
10971102
let type = 'Function';
10981103
if (isGeneratorFunction(value)) {
10991104
type = `Generator${type}`;

test/parallel/test-repl-top-level-await.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ async function ordinaryTests() {
116116
['await 0; function foo() {}'],
117117
['foo', '[Function: foo]'],
118118
['class Foo {}; await 1;', '1'],
119-
['Foo', 'class Foo {}'],
119+
['Foo', '[class Foo]'],
120120
['if (await true) { function bar() {}; }'],
121121
['bar', '[Function: bar]'],
122122
['if (await true) { class Bar {}; }'],

test/parallel/test-util-inspect.js

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1952,50 +1952,58 @@ assert.strictEqual(util.inspect('"\'${a}'), "'\"\\'${a}'");
19521952

19531953
// Verify that classes are properly inspected.
19541954
[
1955+
/* eslint-disable spaced-comment, no-multi-spaces, brace-style */
19551956
// The whitespace is intentional.
1956-
// eslint-disable-next-line no-multi-spaces
1957-
[class { }, 'class (anonymous) {}'],
1958-
[class extends Error { log() {} }, 'class (anonymous) extends Error {}'],
1959-
[class A { constructor(a) { this.a = a; } log() { return this.a; } }],
1957+
[class { }, '[class (anonymous)]'],
1958+
[class extends Error { log() {} }, '[class (anonymous) extends Error]'],
1959+
[class A { constructor(a) { this.a = a; } log() { return this.a; } },
1960+
'[class A]'],
19601961
[class
1961-
// Random comments are part of the stringified result
1962-
äß extends TypeError {}, 'class äß extends TypeError {}'],
1963-
// The whitespace and new line is intended!
1964-
// eslint-disable-next-line no-multi-spaces
1965-
[class X extends Error
1966-
// eslint-disable-next-line brace-style
1967-
{}, 'class X extends Error {}']
1962+
// Random { // comments /* */ are part of the toString() result
1963+
/* eslint-disable-next-line space-before-blocks */
1964+
äß/**/extends/*{*/TypeError{}, '[class äß extends TypeError]'],
1965+
/* The whitespace and new line is intended! */
1966+
// Foobar !!!
1967+
[class X extends /****/ Error
1968+
// More comments
1969+
{}, '[class X extends Error]']
1970+
/* eslint-enable spaced-comment, no-multi-spaces, brace-style */
19681971
].forEach(([clazz, string]) => {
1969-
if (string === undefined)
1970-
string = Function.prototype.toString.call(clazz).split(/\s+/).join(' ');
1971-
const open = string.indexOf('{');
19721972
const inspected = util.inspect(clazz);
1973-
assert.strictEqual(inspected, `${string.slice(0, open + 1)}}`);
1973+
assert.strictEqual(inspected, string);
19741974
Object.defineProperty(clazz, Symbol.toStringTag, {
19751975
value: 'Woohoo'
19761976
});
1977-
const parts = inspected.split(' ');
1978-
parts.pop();
1977+
const parts = inspected.slice(0, -1).split(' ');
19791978
const [, name, ...rest] = parts;
1979+
rest.unshift('[Woohoo]');
1980+
if (rest.length) {
1981+
rest[rest.length - 1] += ']';
1982+
}
19801983
assert.strictEqual(
19811984
util.inspect(clazz),
1982-
['class', name, '[Woohoo]', ...rest, '{}'].join(' ')
1985+
['[class', name, ...rest].join(' ')
19831986
);
19841987
Object.setPrototypeOf(clazz, null);
19851988
assert.strictEqual(
19861989
util.inspect(clazz),
1987-
['class', name, '[null prototype] [Woohoo]', ...rest, '{}'].join(' ')
1990+
['[class', name, '[null prototype]', ...rest].join(' ')
19881991
);
19891992
Object.defineProperty(clazz, 'name', { value: 'Foo' });
19901993
const newName = name === '(anonymous)' ? 'Foo' : `${name} [Foo]`;
19911994
assert.strictEqual(
19921995
util.inspect(clazz),
1993-
['class', newName, '[null prototype] [Woohoo]', ...rest, '{}'].join(' ')
1996+
['[class', newName, '[null prototype]', ...rest].join(' ')
19941997
);
19951998
Object.setPrototypeOf(clazz, Number.prototype);
19961999
assert.strictEqual(
19972000
util.inspect(clazz),
1998-
['class', newName, '[Number] [Woohoo]', ...rest, '{}'].join(' ')
2001+
['[class', newName, '[Number]', ...rest].join(' ')
2002+
);
2003+
clazz.foo = true;
2004+
assert.strictEqual(
2005+
util.inspect(clazz),
2006+
['[class', newName, '[Number]', ...rest, '{ foo: true }'].join(' ')
19992007
);
20002008
});
20012009

0 commit comments

Comments
 (0)