Skip to content

useConsistentBuiltinInstantiation (since v1.7.2)

Diagnostic Category: lint/nursery/useConsistentBuiltinInstantiation

Sources:

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 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.

const 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([
              ++++     
const text = String(10);
const now = new Date();
const map = new Map([
['foo', 'bar']
]);