useConsistentBuiltinInstantiation
Ce contenu n’est pas encore disponible dans votre langue.
Summary
Section titled “Summary”- Rule available since:
v1.7.2 - Diagnostic Category:
lint/style/useConsistentBuiltinInstantiation - This rule has an unsafe fix.
- The default severity of this rule is information.
- Sources:
- Same as
no-new-wrappers
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "style": { "useConsistentBuiltinInstantiation": "error" } } }}Description
Section titled “Description”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
newas that would create object wrappers for the primitive values, which is not what you want. However, withoutnewthey 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 “Examples”Invalid
Section titled “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();
│ ++++
const text = String(10);const now = new Date();Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.