Back to Biomejs

useImportsFirst

src/content/docs/linter/rules/use-imports-first.mdx

latest5.0 KB
Original Source

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

<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> :::note This rule has been implemented but not released yet. It will be available in the next release. ::: :::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 - Diagnostic Category: [`lint/nursery/useImportsFirst`](/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 [`import/first`](https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/first.md)

How to configure

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

Description

Enforce that all imports appear at the top of the module.

Import statements that appear after non-import statements are harder to find and may indicate disorganized code. Keeping all imports together at the top makes dependencies immediately visible.

Directives such as "use strict" are always allowed before imports, since they are parsed separately from module items.

This rule only applies to ES module import statements. CommonJS require() calls are not covered.

Examples

Invalid

js
import { foo } from "foo";
const bar = 1;
import { baz } from "baz";
<pre class="language-text"><code class="language-text">code-block.js:3:1 <a href="https://biomejs.dev/linter/rules/use-imports-first">lint/nursery/useImportsFirst</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This import appears after a non-import statement.</span> <strong>1 │ </strong>import &#123; foo &#125; from &quot;foo&quot;; <strong>2 │ </strong>const bar = 1; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>3 │ </strong>import &#123; baz &#125; from &quot;baz&quot;; <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><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>4 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Scattering imports makes it harder to see the module's dependencies at a glance.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Move all import statements before any other statements.</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
import { foo } from "foo";
import { bar } from "bar";
const baz = 1;
js
"use strict";
import { foo } from "foo";
</TabItem> </Tabs>