useConsistentBuiltinInstantiation (since v1.7.2)
Diagnostic Category: lint/nursery/useConsistentBuiltinInstantiation
Sources:
- Same as:
unicorn/new-for-builtins - Same as:
no-new-wrappers
Enforce the use of new for all builtins, except String, Number, Boolean, Symbol and BigInt.
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
- ArrayBuffer
- BigInt64Array
- BigUint64Array
- DataView
- Date
- Error
- EvalError
- FinalizationRegistry
- Float32Array
- Float64Array
- Function
- Int16Array
- Int32Array
- Int8Array
- Map
- Object
- Promise
- Proxy
- RangeError
- ReferenceError
- RegExp
- Set
- SharedArrayBuffer
- SyntaxError
- TypeError
- URIError
- Uint16Array
- Uint32Array
- Uint8Array
- Uint8ClampedArray
- WeakMap
- WeakRef
- WeakSet
Disallows the use of new for following builtins:
- BigInt
- Boolean
- Number
- String
- Symbol
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.
Examples
Section titled ExamplesInvalid
Section titled Invalidconst text = new String(10);nursery/useConsistentBuiltinInstantiation.js:1:14 lint/nursery/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();nursery/useConsistentBuiltinInstantiation.js:1:13 lint/nursery/useConsistentBuiltinInstantiation FIXABLE ━━━━━━━━━━
⚠ Use new Date() instead of Date().
> 1 │ const now = Date();
│ ^^^^^^
2 │
ℹ Unsafe fix: Add new keyword.
1 │ const·now·=·new·Date();
│ ++++
const map = Map([ ['foo', 'bar']]);nursery/useConsistentBuiltinInstantiation.js:1:13 lint/nursery/useConsistentBuiltinInstantiation FIXABLE ━━━━━━━━━━
⚠ Use new Map() instead of Map().
> 1 │ const map = Map([
│ ^^^^^
> 2 │ ['foo', 'bar']
> 3 │ ]);
│ ^^
4 │
ℹ Unsafe fix: Add new keyword.
1 │ const·map·=·new·Map([
│ ++++
Valid
Section titled Validconst text = String(10);const now = new Date();const map = new Map([ ['foo', 'bar']]);