Skip to content

Commit

Permalink
fixup: address comment
Browse files Browse the repository at this point in the history
Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
BridgeAR committed May 8, 2020
1 parent 3e7470e commit 03007cb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 46 deletions.
52 changes: 21 additions & 31 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ const numberRegExp = /^(0|[1-9][0-9]*)$/;
const coreModuleRegExp = /^ at (?:[^/\\(]+ \(|)((?<![/\\]).+)\.js:\d+:\d+\)?$/;
const nodeModulesRegExp = /[/\\]node_modules[/\\](.+?)(?=[/\\])/g;

const classRegExp = /^(class\s+[^(]*?)\s*{/;
const classRegExp = /^(\s+[^(]*?)\s*{/;
// eslint-disable-next-line node-core/no-unescaped-regexp-dot
const stripCommentsRegExp = /(\/\/.*?\n)|(\/\*(.|\n)*?\*\/)/g;

Expand Down Expand Up @@ -1065,47 +1065,37 @@ function getBoxedBase(value, ctx, keys, constructor, tag) {
return ctx.stylize(base, type.toLowerCase());
}

function getClassBase(string, value, constructor, tag) {
const parts = string.split(/\s+/);
if (parts[1] === 'extends') {
parts[3] = parts[2];
parts[1] = '';
}
const [, name, , superClass] = parts;
function getClassBase(value, constructor, tag) {
const hasName = ObjectPrototypeHasOwnProperty(value, 'name');
const name = (hasName && value.name) || '(anonymous)';
let base = `class ${name}`;
if (name !== value.name) {
if (name === '') {
base += value.name;
} else if (value.name) {
base += ` [${value.name}]`;
}
} else if (name === '') {
base += '(anonymous)';
}
if (constructor !== 'Function') {
if (constructor === null) {
base += ' [null prototype]';
} else {
base += ` [${constructor}]`;
}
if (constructor !== 'Function' && constructor !== null) {
base += ` [${constructor}]`;
}
if (tag !== '' && constructor !== tag) {
base += ` [${tag}]`;
}
if (superClass !== undefined) {
base += ` extends ${superClass}`;
if (constructor !== null) {
const superName = ObjectGetPrototypeOf(value).name;
if (superName) {
base += ` extends ${superName}`;
}
} else {
base += ' extends [null prototype]';
}
return `[${base}]`;
}

function getFunctionBase(value, constructor, tag) {
const stringified = FunctionPrototypeToString(value);
if (stringified.slice(0, 5) === 'class') {
const match = stringified
.replace(stripCommentsRegExp, ' ')
.match(classRegExp);
if (match !== null) {
return getClassBase(match[1], value, constructor, tag);
if (stringified.slice(0, 5) === 'class' && stringified.endsWith('}')) {
const slice = stringified.slice(5, -1);
const bracketIndex = slice.indexOf('{');
if (bracketIndex !== -1 &&
(!slice.slice(0, bracketIndex).includes('(') ||
// Slow path to guarantee that it's indeed a class.
classRegExp.test(slice.replace(stripCommentsRegExp)))) {
return getClassBase(value, constructor, tag);
}
}
let type = 'Function';
Expand Down
29 changes: 14 additions & 15 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2007,27 +2007,20 @@ assert.strictEqual(util.inspect('"\'${a}'), "'\"\\'${a}'");
util.inspect(clazz),
['[class', name, ...rest].join(' ')
);
if (rest.length) {
rest[rest.length - 1] = rest[rest.length - 1].slice(0, -1);
rest.length = 1;
}
Object.setPrototypeOf(clazz, null);
assert.strictEqual(
util.inspect(clazz),
['[class', name, '[null prototype]', ...rest].join(' ')
['[class', name, ...rest, 'extends [null prototype]]'].join(' ')
);
Object.defineProperty(clazz, 'name', { value: 'Foo' });
const newName = name === '(anonymous)' ? 'Foo' : `${name} [Foo]`;
assert.strictEqual(
util.inspect(clazz),
['[class', newName, '[null prototype]', ...rest].join(' ')
);
Object.setPrototypeOf(clazz, Number.prototype);
assert.strictEqual(
util.inspect(clazz),
['[class', newName, '[Number]', ...rest].join(' ')
);
const res = ['[class', 'Foo', ...rest, 'extends [null prototype]]'].join(' ');
assert.strictEqual(util.inspect(clazz), res);
clazz.foo = true;
assert.strictEqual(
util.inspect(clazz),
['[class', newName, '[Number]', ...rest, '{ foo: true }'].join(' ')
);
assert.strictEqual(util.inspect(clazz), `${res} { foo: true }`);
});

// "class" properties should not be detected as "class".
Expand All @@ -2054,6 +2047,12 @@ assert.strictEqual(util.inspect('"\'${a}'), "'\"\\'${a}'");
util.inspect(Foo),
'[Function: Foo]'
);
const fn = function() {};
Object.defineProperty(fn, 'name', { value: 'class Foo {}' });
assert.strictEqual(
util.inspect(fn),
'[Function: class Foo {}]'
);
}

// Verify that throwing in valueOf and toString still produces nice results.
Expand Down

0 comments on commit 03007cb

Please sign in to comment.