Back to Biomejs

useDefaultParameterLast

src/content/docs/linter/rules/use-default-parameter-last.mdx

latest17.1 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.0.0` - Diagnostic Category: [`lint/style/useDefaultParameterLast`](/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 [**warning**](/reference/diagnostics#warning). - Sources: - Same as [`default-param-last`](https://eslint.org/docs/latest/rules/default-param-last) - Same as [`@typescript-eslint/default-param-last`](https://typescript-eslint.io/rules/default-param-last)

How to configure

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

Description

Enforce default function parameters and optional function parameters to be last.

Default and optional parameters that precede a required parameter cannot be omitted at call site.

Examples

Invalid

js
function f(a = 0, b) {}
<pre class="language-text"><code class="language-text">code-block.js:1:12 <a href="https://biomejs.dev/linter/rules/use-default-parameter-last">lint/style/useDefaultParameterLast</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Orange;">⚠</span></strong> <span style="color: Orange;">This </span><span style="color: Orange;"><strong>default parameter</strong></span><span style="color: Orange;"> should follow the last </span><span style="color: Orange;"><strong>required parameter</strong></span><span style="color: Orange;"> or should be a </span><span style="color: Orange;"><strong>required parameter</strong></span><span style="color: Orange;">.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>function f(a = 0, b) &#123;&#125; <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>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">The last </span><span style="color: lightgreen;"><strong>required parameter</strong></span><span style="color: lightgreen;"> is here:</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>function f(a = 0, b) &#123;&#125; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">A </span><span style="color: lightgreen;"><strong>default parameter</strong></span><span style="color: lightgreen;"> that precedes a </span><span style="color: lightgreen;"><strong>required parameter</strong></span><span style="color: lightgreen;"> cannot be omitted at call site.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Unsafe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Turn the parameter into a </span><span style="color: lightgreen;"><strong>required parameter</strong></span><span style="color: lightgreen;">.</span> <strong> 1 │ </strong>function<span style="opacity: 0.8;">·</span>f(a<span style="opacity: 0.8;"><span style="color: Tomato;">·</span></span><span style="color: Tomato;">=</span><span style="opacity: 0.8;"><span style="color: Tomato;">·</span></span><span style="color: Tomato;">0</span>,<span style="opacity: 0.8;">·</span>b)<span style="opacity: 0.8;">·</span>&#123;&#125; <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
function f(a, b = 0, c) {}
<pre class="language-text"><code class="language-text">code-block.js:1:15 <a href="https://biomejs.dev/linter/rules/use-default-parameter-last">lint/style/useDefaultParameterLast</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Orange;">⚠</span></strong> <span style="color: Orange;">This </span><span style="color: Orange;"><strong>default parameter</strong></span><span style="color: Orange;"> should follow the last </span><span style="color: Orange;"><strong>required parameter</strong></span><span style="color: Orange;"> or should be a </span><span style="color: Orange;"><strong>required parameter</strong></span><span style="color: Orange;">.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>function f(a, b = 0, c) &#123;&#125; <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>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">The last </span><span style="color: lightgreen;"><strong>required parameter</strong></span><span style="color: lightgreen;"> is here:</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>function f(a, b = 0, c) &#123;&#125; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">A </span><span style="color: lightgreen;"><strong>default parameter</strong></span><span style="color: lightgreen;"> that precedes a </span><span style="color: lightgreen;"><strong>required parameter</strong></span><span style="color: lightgreen;"> cannot be omitted at call site.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Unsafe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Turn the parameter into a </span><span style="color: lightgreen;"><strong>required parameter</strong></span><span style="color: lightgreen;">.</span> <strong> 1 │ </strong>function<span style="opacity: 0.8;">·</span>f(a,<span style="opacity: 0.8;">·</span>b<span style="opacity: 0.8;"><span style="color: Tomato;">·</span></span><span style="color: Tomato;">=</span><span style="opacity: 0.8;"><span style="color: Tomato;">·</span></span><span style="color: Tomato;">0</span>,<span style="opacity: 0.8;">·</span>c)<span style="opacity: 0.8;">·</span>&#123;&#125; <strong> │ </strong> <span style="color: Tomato;">-</span><span style="color: Tomato;">-</span><span style="color: Tomato;">-</span><span style="color: Tomato;">-</span> </code></pre>
ts
function f(a: number, b?: number, c: number) {}
<pre class="language-text"><code class="language-text">code-block.ts:1:23 <a href="https://biomejs.dev/linter/rules/use-default-parameter-last">lint/style/useDefaultParameterLast</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Orange;">⚠</span></strong> <span style="color: Orange;">This </span><span style="color: Orange;"><strong>optional parameter</strong></span><span style="color: Orange;"> should follow the last </span><span style="color: Orange;"><strong>required parameter</strong></span><span style="color: Orange;"> or should be a </span><span style="color: Orange;"><strong>required parameter</strong></span><span style="color: Orange;">.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>function f(a: number, b?: number, c: number) &#123;&#125; <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>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">The last </span><span style="color: lightgreen;"><strong>required parameter</strong></span><span style="color: lightgreen;"> is here:</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>function f(a: number, b?: number, c: number) &#123;&#125; <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>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">A </span><span style="color: lightgreen;"><strong>optional parameter</strong></span><span style="color: lightgreen;"> that precedes a </span><span style="color: lightgreen;"><strong>required parameter</strong></span><span style="color: lightgreen;"> cannot be omitted at call site.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Unsafe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Turn the parameter into a </span><span style="color: lightgreen;"><strong>required parameter</strong></span><span style="color: lightgreen;">.</span> <strong> 1 │ </strong>function<span style="opacity: 0.8;">·</span>f(a:<span style="opacity: 0.8;">·</span>number,<span style="opacity: 0.8;">·</span>b<span style="color: Tomato;">?</span>:<span style="opacity: 0.8;">·</span>number,<span style="opacity: 0.8;">·</span>c:<span style="opacity: 0.8;">·</span>number)<span style="opacity: 0.8;">·</span>&#123;&#125; <strong> │ </strong> <span style="color: Tomato;">-</span> </code></pre>
ts
class Foo {
    constructor(readonly a = 10, readonly b: number) {}
}
<pre class="language-text"><code class="language-text">code-block.ts:2:17 <a href="https://biomejs.dev/linter/rules/use-default-parameter-last">lint/style/useDefaultParameterLast</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Orange;">⚠</span></strong> <span style="color: Orange;">This </span><span style="color: Orange;"><strong>default parameter</strong></span><span style="color: Orange;"> should follow the last </span><span style="color: Orange;"><strong>required parameter</strong></span><span style="color: Orange;"> or should be a </span><span style="color: Orange;"><strong>required parameter</strong></span><span style="color: Orange;">.</span> <strong>1 │ </strong>class Foo &#123; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>2 │ </strong> constructor(readonly a = 10, readonly b: number) &#123;&#125; <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>3 │ </strong>&#125; <strong>4 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">The last </span><span style="color: lightgreen;"><strong>required parameter</strong></span><span style="color: lightgreen;"> is here:</span> <strong>1 │ </strong>class Foo &#123; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>2 │ </strong> constructor(readonly a = 10, readonly b: number) &#123;&#125; <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>3 │ </strong>&#125; <strong>4 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">A </span><span style="color: lightgreen;"><strong>default parameter</strong></span><span style="color: lightgreen;"> that precedes a </span><span style="color: lightgreen;"><strong>required parameter</strong></span><span style="color: lightgreen;"> cannot be omitted at call site.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Unsafe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Turn the parameter into a </span><span style="color: lightgreen;"><strong>required parameter</strong></span><span style="color: lightgreen;">.</span> <strong> 2 │ </strong><span style="opacity: 0.8;">·</span><span style="opacity: 0.8;">·</span><span style="opacity: 0.8;">·</span><span style="opacity: 0.8;">·</span>constructor(readonly<span style="opacity: 0.8;">·</span>a<span style="opacity: 0.8;"><span style="color: Tomato;">·</span></span><span style="color: Tomato;">=</span><span style="opacity: 0.8;"><span style="color: Tomato;">·</span></span><span style="color: Tomato;">1</span><span style="color: Tomato;">0</span>,<span style="opacity: 0.8;">·</span>readonly<span style="opacity: 0.8;">·</span>b:<span style="opacity: 0.8;">·</span>number)<span style="opacity: 0.8;">·</span>&#123;&#125; <strong> │ </strong> <span style="color: Tomato;">-</span><span style="color: Tomato;">-</span><span style="color: Tomato;">-</span><span style="color: Tomato;">-</span><span style="color: Tomato;">-</span> </code></pre>

Valid

js
function f(a, b = 0) {}
ts
function f(a: number, b?: number, c = 0) {}
ts
function f(a: number, b = 0, c?: number) {}
</TabItem> </Tabs>