Back to Biomejs

useConsistentBuiltinInstantiation

src/content/docs/linter/rules/use-consistent-builtin-instantiation.mdx

latest6.9 KB
Original Source

import { Tabs, TabItem } from '@astrojs/starlight/components';

<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v1.7.2` - Diagnostic Category: [`lint/style/useConsistentBuiltinInstantiation`](/reference/diagnostics#diagnostic-category) - This rule isn't recommended, so you need to enable it. - This rule has an [**unsafe**](/linter/#unsafe-fixes) fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - Sources: - Same as [`no-new-wrappers`](https://eslint.org/docs/latest/rules/no-new-wrappers)

How to configure

json
{
	"linter": {
		"rules": {
			"style": {
				"useConsistentBuiltinInstantiation": "error"
			}
		}
	}
}

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

Examples

Invalid

js
const text = new String(10);
<pre class="language-text"><code class="language-text">code-block.js:1:14 <a href="https://biomejs.dev/linter/rules/use-consistent-new-builtin">lint/style/useConsistentBuiltinInstantiation</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Use </span><span style="color: lightgreen;"><strong>String()</strong></span><span style="color: lightgreen;"> instead of </span><span style="color: lightgreen;"><strong>new String()</strong></span><span style="color: lightgreen;">.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>const text = new String(10); <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Unsafe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Remove </span><span style="color: lightgreen;"><strong>new</strong></span><span style="color: lightgreen;"> keyword.</span> <strong> 1 │ </strong>const<span style="opacity: 0.8;">·</span>text<span style="opacity: 0.8;">·</span>=<span style="opacity: 0.8;">·</span><span style="color: Tomato;">n</span><span style="color: Tomato;">e</span><span style="color: Tomato;">w</span><span style="opacity: 0.8;"><span style="color: Tomato;">·</span></span>String(10); <strong> │ </strong> <span style="color: Tomato;">-</span><span style="color: Tomato;">-</span><span style="color: Tomato;">-</span><span style="color: Tomato;">-</span> </code></pre>
js
const now = Date();
<pre class="language-text"><code class="language-text">code-block.js:1:13 <a href="https://biomejs.dev/linter/rules/use-consistent-new-builtin">lint/style/useConsistentBuiltinInstantiation</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Use </span><span style="color: lightgreen;"><strong>new Date()</strong></span><span style="color: lightgreen;"> instead of </span><span style="color: lightgreen;"><strong>Date()</strong></span><span style="color: lightgreen;">.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>const now = Date(); <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Unsafe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Add </span><span style="color: lightgreen;"><strong>new</strong></span><span style="color: lightgreen;"> keyword.</span> <strong> 1 │ </strong>const<span style="opacity: 0.8;">·</span>now<span style="opacity: 0.8;">·</span>=<span style="opacity: 0.8;">·</span><span style="color: MediumSeaGreen;">n</span><span style="color: MediumSeaGreen;">e</span><span style="color: MediumSeaGreen;">w</span><span style="opacity: 0.8;"><span style="color: MediumSeaGreen;">·</span></span>Date(); <strong> │ </strong> <span style="color: MediumSeaGreen;">+</span><span style="color: MediumSeaGreen;">+</span><span style="color: MediumSeaGreen;">+</span><span style="color: MediumSeaGreen;">+</span> </code></pre>

Valid

js
const text = String(10);
js
const now = new Date();
</TabItem> </Tabs>