Aller au contenu

useConsistentBuiltinInstantiation

Ce contenu n’est pas encore disponible dans votre langue.

Diagnostic Category: lint/style/useConsistentBuiltinInstantiation

Since: v1.7.2

Sources:

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

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

Disallows the use of new for following builtins:

  • Boolean
  • Number
  • String

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.

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.

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();
++++
const text = String(10);
const now = new Date();