Skip to content

Commit c4cb7e2

Browse files
committed
util.inspect: fix numericSeparator for scientific notation numbers
When numericSeparator: true is set, util.inspect produces corrupted output like '1e-.1e-_7' for numbers in scientific notation (e.g. 1e-7). The fix adds an early return for scientific notation strings before the decimal-split + numeric separator logic runs. Fixes #62981
1 parent bb85d23 commit c4cb7e2

1 file changed

Lines changed: 19 additions & 6 deletions

File tree

lib/internal/util/inspect.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,13 +1539,18 @@ function getBoxedBase(value, ctx, keys, constructor, tag) {
15391539
base += ` (${constructor})`;
15401540
}
15411541
}
1542-
base += `: ${formatPrimitive(stylizeNoColor, fn(value), ctx)}]`;
15431542
if (tag !== '' && tag !== constructor) {
15441543
base += ` [${tag}]`;
15451544
}
1546-
if (keys.length !== 0 || ctx.stylize === stylizeNoColor)
1547-
return base;
1548-
return ctx.stylize(base, StringPrototypeToLowerCase(type));
1545+
if (constructor !== null) {
1546+
const superName = ObjectGetPrototypeOf(value).name;
1547+
if (superName) {
1548+
base += ` extends ${superName}`;
1549+
}
1550+
} else {
1551+
base += ' extends [null prototype]';
1552+
}
1553+
return `${base}: ${formatPrimitive(stylizeNoColor, fn(value), ctx)}]`;
15491554
}
15501555

15511556
function getClassBase(value, constructor, tag) {
@@ -1835,7 +1840,7 @@ function improveStack(stack, constructor, name, tag) {
18351840
}
18361841
const prefix = StringPrototypeSlice(getPrefix(constructor, tag, fallback), 0, -1);
18371842
if (name !== prefix) {
1838-
if (StringPrototypeIncludes(prefix, name)) {
1843+
if (StringPrototypeSlice(prefix, -len) === name) {
18391844
if (len === 0) {
18401845
stack = `${prefix}: ${stack}`;
18411846
} else {
@@ -2204,12 +2209,20 @@ function formatNumber(fn, number, numericSeparator) {
22042209
if (!NumberIsFinite(number) || StringPrototypeIncludes(numberString, 'e')) {
22052210
return fn(numberString, 'number');
22062211
}
2212+
if (StringPrototypeIncludes(numberString, 'e')) {
2213+
return fn(numberString, 'number');
2214+
}
22072215
return fn(addNumericSeparator(numberString), 'number');
22082216
}
22092217
if (NumberIsNaN(number)) {
22102218
return fn(numberString, 'number');
22112219
}
22122220

2221+
// Scientific notation cannot have numeric separators applied sensibly
2222+
if (StringPrototypeIncludes(numberString, 'e')) {
2223+
return fn(numberString, 'number');
2224+
}
2225+
22132226
const decimalIndex = StringPrototypeIndexOf(numberString, '.');
22142227
const integerPart = StringPrototypeSlice(numberString, 0, decimalIndex);
22152228
const fractionalPart = StringPrototypeSlice(numberString, decimalIndex + 1);
@@ -2414,8 +2427,8 @@ function formatMap(value, ctx, ignored, recurseTimes) {
24142427
const maxLength = MathMin(MathMax(0, ctx.maxArrayLength), length);
24152428
const remaining = length - maxLength;
24162429
const output = [];
2417-
ctx.indentationLvl += 2;
24182430
let i = 0;
2431+
ctx.indentationLvl += 2;
24192432
for (const { 0: k, 1: v } of value) {
24202433
if (i >= maxLength) break;
24212434
ArrayPrototypePush(

0 commit comments

Comments
 (0)