Back to Biomejs

noFloatingClasses

src/content/docs/linter/rules/no-floating-classes.mdx

latest3.8 KB
Original Source

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

<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> :::caution This rule is part of the [nursery](/linter/#nursery) group. This means that it is experimental and the behavior can change at any time. ::: ## Summary - Rule available since: `v2.3.12` - Diagnostic Category: [`lint/nursery/noFloatingClasses`](/reference/diagnostics#diagnostic-category) - This rule doesn't have a fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - Sources: - Same as [`no-new`](https://eslint.org/docs/latest/rules/no-new)

How to configure

json
{
	"linter": {
		"rules": {
			"nursery": {
				"noFloatingClasses": "error"
			}
		}
	}
}

Description

Disallow new operators outside of assignments or comparisons.

The goal of using new with a constructor is typically to create an object of a particular type and store that object in a variable, such as:

js
const person = new Person();

It's less common to use new and not store the result, such as:

js
new Person();

In this case, the created object is thrown away because its reference isn't stored anywhere, and in many cases, this means that the constructor should be replaced with a function that doesn't require new to be used.

Examples

Invalid

js
new Thing();
<pre class="language-text"><code class="language-text">code-block.js:1:1 <a href="https://biomejs.dev/linter/rules/no-floating-classes">lint/nursery/noFloatingClasses</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Using the &#96;new&#96; operator outside of assignments or comparisons is not allowed.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>new Thing(); <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>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">The created object is thrown away because its reference isn't stored anywhere. Assign the object to a variable or replace with a function that doesn't require &#96;new&#96; to be used.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit </span><span style="color: lightgreen;"><a href="https://biomejs.dev/linter/#nursery">https://biomejs.dev/linter/#nursery</a></span><span style="color: lightgreen;"> for more information.</span> </code></pre>

Valid

js
const thing = new Thing();
</TabItem> </Tabs>