Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/eslint-plugin/docs/rules/default-param-last.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ Examples of **incorrect** code for this rule:
function f(a = 0, b: number) {}
function f(a: number, b = 0, c: number) {}
function f(a: number, b?: number, c: number) {}
class Foo {
constructor(public a = 10, private b: number) {}
}
class Foo {
constructor(public a?: number, private b: number) {}
}
```

Examples of **correct** code for this rule:
Expand All @@ -24,6 +30,12 @@ function f(a: number, b = 0) {}
function f(a: number, b?: number) {}
function f(a: number, b?: number, c = 0) {}
function f(a: number, b = 0, c?: number) {}
class Foo {
constructor(public a, private b = 0) {}
}
class Foo {
constructor(public a, private b?: number) {}
}
```

<sup>Taken with ❤️ [from ESLint core](https://github.com/eslint/eslint/blob/master/docs/rules/default-param-last.md)</sup>
8 changes: 6 additions & 2 deletions packages/eslint-plugin/src/rules/default-param-last.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ export default createRule({
): void {
let hasSeenPlainParam = false;
for (let i = node.params.length - 1; i >= 0; i--) {
const param = node.params[i];
const current = node.params[i];
const param =
current.type === AST_NODE_TYPES.TSParameterProperty
? current.parameter
: current;

if (isPlainParam(param)) {
hasSeenPlainParam = true;
Expand All @@ -63,7 +67,7 @@ export default createRule({
(isOptionalParam(param) ||
param.type === AST_NODE_TYPES.AssignmentPattern)
) {
context.report({ node: param, messageId: 'shouldBeLast' });
context.report({ node: current, messageId: 'shouldBeLast' });
}
}
}
Expand Down
135 changes: 135 additions & 0 deletions packages/eslint-plugin/tests/rules/default-param-last.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,51 @@ ruleTester.run('default-param-last', rule, {
'const foo = (a: number, b = 1, c?: number) => {}',
'const foo = (a: number, b?: number, c = 1) => {}',
'const foo = (a: number, b = 1, ...c) => {}',
`
class Foo {
constructor(a: number, b: number, c: number) {}
}
`,
`
class Foo {
constructor(a: number, b?: number, c = 1) {}
}
`,
`
class Foo {
constructor(a: number, b = 1, c?: number) {}
}
`,
`
class Foo {
constructor(public a: number, protected b: number, private c: number) {}
}
`,
`
class Foo {
constructor(public a: number, protected b?: number, private c = 10) {}
}
`,
`
class Foo {
constructor(public a: number, protected b = 10, private c?: number) {}
}
`,
`
class Foo {
constructor(a: number, protected b?: number, private c = 0) {}
}
`,
`
class Foo {
constructor(a: number, b?: number, private c = 0) {}
}
`,
`
class Foo {
constructor(a: number, private b?: number, c = 0) {}
}
`,
],
invalid: [
{
Expand Down Expand Up @@ -527,5 +572,95 @@ ruleTester.run('default-param-last', rule, {
},
],
},
{
code: `
class Foo {
constructor(public a: number, protected b?: number, private c: number) {}
}
`,
errors: [
{
messageId: 'shouldBeLast',
line: 3,
column: 33,
endColumn: 53,
},
],
},
{
code: `
class Foo {
constructor(public a: number, protected b = 0, private c: number) {}
}
`,
errors: [
{
messageId: 'shouldBeLast',
line: 3,
column: 33,
endColumn: 48,
},
],
},
{
code: `
class Foo {
constructor(public a?: number, private b: number) {}
}
`,
errors: [
{
messageId: 'shouldBeLast',
line: 3,
column: 15,
endColumn: 32,
},
],
},
{
code: `
class Foo {
constructor(public a = 0, private b: number) {}
}
`,
errors: [
{
messageId: 'shouldBeLast',
line: 3,
column: 15,
endColumn: 27,
},
],
},
{
code: `
class Foo {
constructor(a = 0, b: number) {}
}
`,
errors: [
{
messageId: 'shouldBeLast',
line: 3,
column: 15,
endColumn: 20,
},
],
},
{
code: `
class Foo {
constructor(a?: number, b: number) {}
}
`,
errors: [
{
messageId: 'shouldBeLast',
line: 3,
column: 15,
endColumn: 25,
},
],
},
],
});