Skip to content
← Back to rules

unicorn/consistent-date-clone 风格

An auto-fix is available for this rule.

它的作用

当将一个 Date 对象作为参数传递给 Date 构造函数时,可以直接克隆该对象,无需进行时间戳转换。此规则强制使用直接的 Date 克隆方式,而不是通过 .getTime() 进行转换。

为什么这是个问题?

使用 .getTime()Date 对象转换为时间戳,然后再将其转换回 Date,是冗余且不必要的。直接将 Date 对象传递给 Date 构造函数更加简洁高效。

示例

此规则的 错误 代码示例:

js
new Date(date.getTime());

此规则的 正确 代码示例:

js
new Date(date);

如何使用

要通过配置文件或命令行启用此规则,可以使用:

json
{
  "rules": {
    "unicorn/consistent-date-clone": "error"
  }
}
bash
oxlint --deny unicorn/consistent-date-clone

参考资料