Skip to content

Commit 7dab908

Browse files
antsmartianBridgeAR
authored andcommitted
util: add null prototype support for date
PR-URL: nodejs#25144 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 5494891 commit 7dab908

File tree

3 files changed

+59
-9
lines changed

3 files changed

+59
-9
lines changed

lib/internal/util/inspect.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ function uncurryThis(func) {
7777
const propertyIsEnumerable = uncurryThis(Object.prototype.propertyIsEnumerable);
7878
const regExpToString = uncurryThis(RegExp.prototype.toString);
7979
const dateToISOString = uncurryThis(Date.prototype.toISOString);
80+
const dateToString = uncurryThis(Date.prototype.toString);
8081
const errorToString = uncurryThis(Error.prototype.toString);
8182

8283
const bigIntValueOf = uncurryThis(BigInt.prototype.valueOf);
@@ -645,12 +646,15 @@ function formatRaw(ctx, value, recurseTimes) {
645646
return ctx.stylize(base, 'regexp');
646647
} else if (isDate(value)) {
647648
// Make dates with properties first say the date
649+
base = Number.isNaN(dateGetTime(value)) ?
650+
dateToString(value) :
651+
dateToISOString(value);
652+
const prefix = getPrefix(constructor, tag, 'Date');
653+
if (prefix !== 'Date ')
654+
base = `${prefix}${base}`;
648655
if (keys.length === 0) {
649-
if (Number.isNaN(dateGetTime(value)))
650-
return ctx.stylize(String(value), 'date');
651-
return ctx.stylize(dateToISOString(value), 'date');
656+
return ctx.stylize(base, 'date');
652657
}
653-
base = dateToISOString(value);
654658
} else if (isError(value)) {
655659
// Make error with message first say the error.
656660
base = formatError(value);

test/parallel/test-assert-deep.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,16 @@ assert.throws(
117117
{
118118
code: 'ERR_ASSERTION',
119119
message: `${defaultMsgStartFull}\n\n` +
120-
'+ 2016-01-01T00:00:00.000Z\n- 2016-01-01T00:00:00.000Z {\n' +
121-
"- '0': '1'\n- }"
120+
'+ 2016-01-01T00:00:00.000Z\n- MyDate 2016-01-01T00:00:00.000Z' +
121+
" {\n- '0': '1'\n- }"
122122
}
123123
);
124124
assert.throws(
125125
() => assert.deepStrictEqual(date2, date),
126126
{
127127
code: 'ERR_ASSERTION',
128128
message: `${defaultMsgStartFull}\n\n` +
129-
'+ 2016-01-01T00:00:00.000Z {\n' +
129+
'+ MyDate 2016-01-01T00:00:00.000Z {\n' +
130130
"+ '0': '1'\n+ }\n- 2016-01-01T00:00:00.000Z"
131131
}
132132
);

test/parallel/test-util-inspect.js

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,8 +1645,10 @@ assert.strictEqual(util.inspect('"\'${a}'), "'\"\\'${a}'");
16451645
'[DataView: null prototype] {\n byteLength: undefined,\n ' +
16461646
'byteOffset: undefined,\n buffer: undefined }'],
16471647
[new SharedArrayBuffer(2), '[SharedArrayBuffer: null prototype] ' +
1648-
'{ byteLength: undefined }'],
1649-
[/foobar/, '[RegExp: null prototype] /foobar/']
1648+
'{ byteLength: undefined }'],
1649+
[/foobar/, '[RegExp: null prototype] /foobar/'],
1650+
[new Date('Sun, 14 Feb 2010 11:48:40 GMT'),
1651+
'[Date: null prototype] 2010-02-14T11:48:40.000Z']
16501652
].forEach(([value, expected]) => {
16511653
assert.strictEqual(
16521654
util.inspect(Object.setPrototypeOf(value, null)),
@@ -1690,6 +1692,50 @@ assert.strictEqual(util.inspect('"\'${a}'), "'\"\\'${a}'");
16901692
assert(/\[Symbol\(foo\)]: 'yeah'/.test(res), res);
16911693
});
16921694

1695+
// Date null prototype checks
1696+
{
1697+
class CustomDate extends Date {
1698+
}
1699+
1700+
const date = new CustomDate('Sun, 14 Feb 2010 11:48:40 GMT');
1701+
assert.strictEqual(util.inspect(date), 'CustomDate 2010-02-14T11:48:40.000Z');
1702+
1703+
// add properties
1704+
date.foo = 'bar';
1705+
assert.strictEqual(util.inspect(date),
1706+
'{ CustomDate 2010-02-14T11:48:40.000Z foo: \'bar\' }');
1707+
1708+
// check for null prototype
1709+
Object.setPrototypeOf(date, null);
1710+
assert.strictEqual(util.inspect(date),
1711+
'{ [Date: null prototype] 2010-02-14T11:48:40.000Z' +
1712+
' foo: \'bar\' }');
1713+
1714+
const anotherDate = new CustomDate('Sun, 14 Feb 2010 11:48:40 GMT');
1715+
Object.setPrototypeOf(anotherDate, null);
1716+
assert.strictEqual(util.inspect(anotherDate),
1717+
'[Date: null prototype] 2010-02-14T11:48:40.000Z');
1718+
}
1719+
1720+
// Check for invalid dates and null prototype
1721+
{
1722+
class CustomDate extends Date {
1723+
}
1724+
1725+
const date = new CustomDate('invalid_date');
1726+
assert.strictEqual(util.inspect(date), 'CustomDate Invalid Date');
1727+
1728+
// add properties
1729+
date.foo = 'bar';
1730+
assert.strictEqual(util.inspect(date),
1731+
'{ CustomDate Invalid Date foo: \'bar\' }');
1732+
1733+
// check for null prototype
1734+
Object.setPrototypeOf(date, null);
1735+
assert.strictEqual(util.inspect(date),
1736+
'{ [Date: null prototype] Invalid Date foo: \'bar\' }');
1737+
}
1738+
16931739
assert.strictEqual(inspect(1n), '1n');
16941740
assert.strictEqual(inspect(Object(-1n)), '[BigInt: -1n]');
16951741
assert.strictEqual(inspect(Object(13n)), '[BigInt: 13n]');

0 commit comments

Comments
 (0)