sindresorhus / eslint-plugin-unicorn

More than 100 powerful ESLint rules
MIT License
4.2k stars 363 forks source link

Rule proposal: `consistent-date-clone` #2437

Open fisker opened 1 month ago

fisker commented 1 month ago

Description

I just learned that Date constructor accepts Date since ES6.

If Type(value) is Object and value has a [[DateValue]] internal slot, then Let tv be thisTimeValue(value).

https://262.ecma-international.org/6.0/#sec-date-constructor

Fail

new Date(date.getTime());
new Date(
    date.getFullYear(),
    date.getMonth(),
    date.getDate(),
    date.getHours(),
    date.getMinutes(),
    date.getSeconds(),
    date.getMilliseconds(),
);

Pass

new Date(date)

Proposed rule name

consistent-date-clone

Additional Info

Note: new Date(date) doesn't work before ES6

Let v be ToPrimitive(value). If Type(v) is String, then Parse v as a date, in exactly the same manner as for the parse method (15.9.4.2); let V be the time value for this date. Else, let V be ToNumber(v). Set the [[PrimitiveValue]] internal property of the newly constructed object to TimeClip(V) and return

https://262.ecma-international.org/5.1/#sec-15.9.3.2

sindresorhus commented 4 weeks ago

Accepted