util: add null prototype support for date
PR-URL: https://github.com/nodejs/node/pull/25144 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
baa4b9b425
commit
81b25eac21
@ -77,6 +77,7 @@ function uncurryThis(func) {
|
|||||||
const propertyIsEnumerable = uncurryThis(Object.prototype.propertyIsEnumerable);
|
const propertyIsEnumerable = uncurryThis(Object.prototype.propertyIsEnumerable);
|
||||||
const regExpToString = uncurryThis(RegExp.prototype.toString);
|
const regExpToString = uncurryThis(RegExp.prototype.toString);
|
||||||
const dateToISOString = uncurryThis(Date.prototype.toISOString);
|
const dateToISOString = uncurryThis(Date.prototype.toISOString);
|
||||||
|
const dateToString = uncurryThis(Date.prototype.toString);
|
||||||
const errorToString = uncurryThis(Error.prototype.toString);
|
const errorToString = uncurryThis(Error.prototype.toString);
|
||||||
|
|
||||||
const bigIntValueOf = uncurryThis(BigInt.prototype.valueOf);
|
const bigIntValueOf = uncurryThis(BigInt.prototype.valueOf);
|
||||||
@ -646,12 +647,15 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
|
|||||||
return ctx.stylize(base, 'regexp');
|
return ctx.stylize(base, 'regexp');
|
||||||
} else if (isDate(value)) {
|
} else if (isDate(value)) {
|
||||||
// Make dates with properties first say the date
|
// Make dates with properties first say the date
|
||||||
|
base = Number.isNaN(dateGetTime(value)) ?
|
||||||
|
dateToString(value) :
|
||||||
|
dateToISOString(value);
|
||||||
|
const prefix = getPrefix(constructor, tag, 'Date');
|
||||||
|
if (prefix !== 'Date ')
|
||||||
|
base = `${prefix}${base}`;
|
||||||
if (keys.length === 0) {
|
if (keys.length === 0) {
|
||||||
if (Number.isNaN(dateGetTime(value)))
|
return ctx.stylize(base, 'date');
|
||||||
return ctx.stylize(String(value), 'date');
|
|
||||||
return ctx.stylize(dateToISOString(value), 'date');
|
|
||||||
}
|
}
|
||||||
base = dateToISOString(value);
|
|
||||||
} else if (isError(value)) {
|
} else if (isError(value)) {
|
||||||
// Make error with message first say the error.
|
// Make error with message first say the error.
|
||||||
base = formatError(value);
|
base = formatError(value);
|
||||||
|
@ -117,8 +117,8 @@ assert.throws(
|
|||||||
{
|
{
|
||||||
code: 'ERR_ASSERTION',
|
code: 'ERR_ASSERTION',
|
||||||
message: `${defaultMsgStartFull}\n\n` +
|
message: `${defaultMsgStartFull}\n\n` +
|
||||||
'+ 2016-01-01T00:00:00.000Z\n- 2016-01-01T00:00:00.000Z {\n' +
|
'+ 2016-01-01T00:00:00.000Z\n- MyDate 2016-01-01T00:00:00.000Z' +
|
||||||
"- '0': '1'\n- }"
|
" {\n- '0': '1'\n- }"
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
assert.throws(
|
assert.throws(
|
||||||
@ -126,7 +126,7 @@ assert.throws(
|
|||||||
{
|
{
|
||||||
code: 'ERR_ASSERTION',
|
code: 'ERR_ASSERTION',
|
||||||
message: `${defaultMsgStartFull}\n\n` +
|
message: `${defaultMsgStartFull}\n\n` +
|
||||||
'+ 2016-01-01T00:00:00.000Z {\n' +
|
'+ MyDate 2016-01-01T00:00:00.000Z {\n' +
|
||||||
"+ '0': '1'\n+ }\n- 2016-01-01T00:00:00.000Z"
|
"+ '0': '1'\n+ }\n- 2016-01-01T00:00:00.000Z"
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
@ -1663,7 +1663,9 @@ assert.strictEqual(util.inspect('"\'${a}'), "'\"\\'${a}'");
|
|||||||
'byteOffset: undefined,\n buffer: undefined }'],
|
'byteOffset: undefined,\n buffer: undefined }'],
|
||||||
[new SharedArrayBuffer(2), '[SharedArrayBuffer: null prototype] ' +
|
[new SharedArrayBuffer(2), '[SharedArrayBuffer: null prototype] ' +
|
||||||
'{ [Uint8Contents]: <00 00>, byteLength: undefined }'],
|
'{ [Uint8Contents]: <00 00>, byteLength: undefined }'],
|
||||||
[/foobar/, '[RegExp: null prototype] /foobar/']
|
[/foobar/, '[RegExp: null prototype] /foobar/'],
|
||||||
|
[new Date('Sun, 14 Feb 2010 11:48:40 GMT'),
|
||||||
|
'[Date: null prototype] 2010-02-14T11:48:40.000Z']
|
||||||
].forEach(([value, expected]) => {
|
].forEach(([value, expected]) => {
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
util.inspect(Object.setPrototypeOf(value, null)),
|
util.inspect(Object.setPrototypeOf(value, null)),
|
||||||
@ -1707,6 +1709,50 @@ assert.strictEqual(util.inspect('"\'${a}'), "'\"\\'${a}'");
|
|||||||
assert(/\[Symbol\(foo\)]: 'yeah'/.test(res), res);
|
assert(/\[Symbol\(foo\)]: 'yeah'/.test(res), res);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Date null prototype checks
|
||||||
|
{
|
||||||
|
class CustomDate extends Date {
|
||||||
|
}
|
||||||
|
|
||||||
|
const date = new CustomDate('Sun, 14 Feb 2010 11:48:40 GMT');
|
||||||
|
assert.strictEqual(util.inspect(date), 'CustomDate 2010-02-14T11:48:40.000Z');
|
||||||
|
|
||||||
|
// add properties
|
||||||
|
date.foo = 'bar';
|
||||||
|
assert.strictEqual(util.inspect(date),
|
||||||
|
'{ CustomDate 2010-02-14T11:48:40.000Z foo: \'bar\' }');
|
||||||
|
|
||||||
|
// check for null prototype
|
||||||
|
Object.setPrototypeOf(date, null);
|
||||||
|
assert.strictEqual(util.inspect(date),
|
||||||
|
'{ [Date: null prototype] 2010-02-14T11:48:40.000Z' +
|
||||||
|
' foo: \'bar\' }');
|
||||||
|
|
||||||
|
const anotherDate = new CustomDate('Sun, 14 Feb 2010 11:48:40 GMT');
|
||||||
|
Object.setPrototypeOf(anotherDate, null);
|
||||||
|
assert.strictEqual(util.inspect(anotherDate),
|
||||||
|
'[Date: null prototype] 2010-02-14T11:48:40.000Z');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for invalid dates and null prototype
|
||||||
|
{
|
||||||
|
class CustomDate extends Date {
|
||||||
|
}
|
||||||
|
|
||||||
|
const date = new CustomDate('invalid_date');
|
||||||
|
assert.strictEqual(util.inspect(date), 'CustomDate Invalid Date');
|
||||||
|
|
||||||
|
// add properties
|
||||||
|
date.foo = 'bar';
|
||||||
|
assert.strictEqual(util.inspect(date),
|
||||||
|
'{ CustomDate Invalid Date foo: \'bar\' }');
|
||||||
|
|
||||||
|
// check for null prototype
|
||||||
|
Object.setPrototypeOf(date, null);
|
||||||
|
assert.strictEqual(util.inspect(date),
|
||||||
|
'{ [Date: null prototype] Invalid Date foo: \'bar\' }');
|
||||||
|
}
|
||||||
|
|
||||||
assert.strictEqual(inspect(1n), '1n');
|
assert.strictEqual(inspect(1n), '1n');
|
||||||
assert.strictEqual(inspect(Object(-1n)), '[BigInt: -1n]');
|
assert.strictEqual(inspect(Object(-1n)), '[BigInt: -1n]');
|
||||||
assert.strictEqual(inspect(Object(13n)), '[BigInt: 13n]');
|
assert.strictEqual(inspect(Object(13n)), '[BigInt: 13n]');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user