useConsistentBuiltinInstantiation
Ce contenu n’est pas encore disponible dans votre langue.
Diagnostic Category: lint/style/useConsistentBuiltinInstantiation
Since: v1.7.2
Sources:
- Same as:
no-new-wrappers
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, withoutnew
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.
Examples
Section titled ExamplesInvalid
Section titled Invalidcode-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);
│ ----
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();
│ ++++