Skip to content

useConsistentBuiltinInstantiation

诊断类别:lint/style/useConsistentBuiltinInstantiation

¥Diagnostic Category: lint/style/useConsistentBuiltinInstantiation

自从:v1.7.2

¥Since: v1.7.2

来源:

¥Sources:

强制所有内置命令使用 newStringNumberBoolean 除外。

¥Enforce the use of new for all builtins, except String, Number and Boolean.

new Builtin()Builtin() 的工作方式相同,但应优先考虑 new 以与其他构造函数保持一致。强制使用 new 来处理以下内置函数:

¥new Builtin() and Builtin() work the same, but new should be preferred for consistency with other constructors. Enforces the use of new for following builtins:

  • AggregateError

  • 数组

    ¥Array

  • 日期

    ¥Date

  • 错误

    ¥Error

  • EvalError

  • 对象

    ¥Object

  • Promise

  • RangeError

  • ReferenceError

  • RegExp

  • SyntaxError

  • TypeError

  • URIError

禁止将 new 用于以下内置函数:

¥Disallows the use of new for following builtins:

  • 布尔值

    ¥Boolean

  • 数字

    ¥Number

  • 字符串

    ¥String

这些不应该使用 new,因为这会为原始值创建对象封装器,这不是你想要的。但是,如果没有 new,它们也可以用于将值强制转换为该类型。

¥These should not use new as that would create object wrappers for the primitive values, which is not what you want. However, without new they can be useful for coercing a value to that type.

请注意,需要实例化 new 的内置函数和不需要实例化 new 的内置函数(SymbolBigInt)受 noInvalidBuiltinInstantiation 规则的约束。

¥Note that, builtins that require new to be instantiated and builtins that require no new to be instantiated (Symbol and BigInt) are covered by the noInvalidBuiltinInstantiation rule.

¥Examples

¥Invalid

const text = new String(10);
code-block.js:1:14 lint/style/useConsistentBuiltinInstantiation  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━

Use String() instead of new String().

> 1 │ const text = new String(10);
^^^^^^^^^^^^^^
2 │

Unsafe fix: Remove new keyword.

1 │ const·text·=·new·String(10);
----
const now = Date();
code-block.js:1:13 lint/style/useConsistentBuiltinInstantiation  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━

Use new Date() instead of Date().

> 1 │ const now = Date();
^^^^^^
2 │

Unsafe fix: Add new keyword.

1 │ const·now·=·new·Date();
++++

¥Valid

const text = String(10);
const now = new Date();

¥Related links