Skip to content

useDefaultParameterLast

诊断类别:lint/style/useDefaultParameterLast

¥Diagnostic Category: lint/style/useDefaultParameterLast

自从:v1.0.0

¥Since: v1.0.0

来源:

¥Sources:

强制 switch 语句中的默认子句位于最后

¥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

function f(a = 0, b) {}
code-block.js:1:12 lint/style/useDefaultParameterLast  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This default parameter should follow the last required parameter or should be a required parameter.

> 1 │ function f(a = 0, b) {}
^^^^^
2 │

The last required parameter is here:

> 1 │ function f(a = 0, b) {}
^
2 │

A default parameter that precedes a required parameter cannot be omitted at call site.

Unsafe fix: Turn the parameter into a required parameter.

1 │ function·f(a·=·0,·b)·{}
----
function f(a, b = 0, c) {}
code-block.js:1:15 lint/style/useDefaultParameterLast  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This default parameter should follow the last required parameter or should be a required parameter.

> 1 │ function f(a, b = 0, c) {}
^^^^^
2 │

The last required parameter is here:

> 1 │ function f(a, b = 0, c) {}
^
2 │

A default parameter that precedes a required parameter cannot be omitted at call site.

Unsafe fix: Turn the parameter into a required parameter.

1 │ function·f(a,·b·=·0,·c)·{}
----
function f(a: number, b?: number, c: number) {}
code-block.ts:1:23 lint/style/useDefaultParameterLast  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This optional parameter should follow the last required parameter or should be a required parameter.

> 1 │ function f(a: number, b?: number, c: number) {}
^^^^^^^^^^
2 │

The last required parameter is here:

> 1 │ function f(a: number, b?: number, c: number) {}
^^^^^^^^^
2 │

A optional parameter that precedes a required parameter cannot be omitted at call site.

Unsafe fix: Turn the parameter into a required parameter.

1 │ function·f(a:·number,·b?:·number,·c:·number)·{}
-
class Foo {
constructor(readonly a = 10, readonly b: number) {}
}
code-block.ts:2:17 lint/style/useDefaultParameterLast  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This default parameter should follow the last required parameter or should be a required parameter.

1 │ class Foo {
> 2 │ constructor(readonly a = 10, readonly b: number) {}
^^^^^^^^^^^^^^^
3 │ }
4 │

The last required parameter is here:

1 │ class Foo {
> 2 │ constructor(readonly a = 10, readonly b: number) {}
^^^^^^^^^^^^^^^^^^
3 │ }
4 │

A default parameter that precedes a required parameter cannot be omitted at call site.

Unsafe fix: Turn the parameter into a required parameter.

2 │ ····constructor(readonly·a·=·10,·readonly·b:·number)·{}
-----

¥Valid

function f(a, b = 0) {}
function f(a: number, b?: number, c = 0) {}
function f(a: number, b = 0, c?: number) {}

¥Related links