Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3534c6e

Browse files
authored
fix(eslint-plugin): [default-param-last] handle param props (typescript-eslint#1650)
1 parent 4eedd7f commit 3534c6e

File tree

3 files changed

+153
-2
lines changed

3 files changed

+153
-2
lines changed

packages/eslint-plugin/docs/rules/default-param-last.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ Examples of **incorrect** code for this rule:
1212
function f(a = 0, b: number) {}
1313
function f(a: number, b = 0, c: number) {}
1414
function f(a: number, b?: number, c: number) {}
15+
class Foo {
16+
constructor(public a = 10, private b: number) {}
17+
}
18+
class Foo {
19+
constructor(public a?: number, private b: number) {}
20+
}
1521
```
1622

1723
Examples of **correct** code for this rule:
@@ -24,6 +30,12 @@ function f(a: number, b = 0) {}
2430
function f(a: number, b?: number) {}
2531
function f(a: number, b?: number, c = 0) {}
2632
function f(a: number, b = 0, c?: number) {}
33+
class Foo {
34+
constructor(public a, private b = 0) {}
35+
}
36+
class Foo {
37+
constructor(public a, private b?: number) {}
38+
}
2739
```
2840

2941
<sup>Taken with ❤️ [from ESLint core](https://github.com/eslint/eslint/blob/master/docs/rules/default-param-last.md)</sup>

packages/eslint-plugin/src/rules/default-param-last.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ export default createRule({
5151
): void {
5252
let hasSeenPlainParam = false;
5353
for (let i = node.params.length - 1; i >= 0; i--) {
54-
const param = node.params[i];
54+
const current = node.params[i];
55+
const param =
56+
current.type === AST_NODE_TYPES.TSParameterProperty
57+
? current.parameter
58+
: current;
5559

5660
if (isPlainParam(param)) {
5761
hasSeenPlainParam = true;
@@ -63,7 +67,7 @@ export default createRule({
6367
(isOptionalParam(param) ||
6468
param.type === AST_NODE_TYPES.AssignmentPattern)
6569
) {
66-
context.report({ node: param, messageId: 'shouldBeLast' });
70+
context.report({ node: current, messageId: 'shouldBeLast' });
6771
}
6872
}
6973
}

packages/eslint-plugin/tests/rules/default-param-last.test.ts

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,51 @@ ruleTester.run('default-param-last', rule, {
4242
'const foo = (a: number, b = 1, c?: number) => {}',
4343
'const foo = (a: number, b?: number, c = 1) => {}',
4444
'const foo = (a: number, b = 1, ...c) => {}',
45+
`
46+
class Foo {
47+
constructor(a: number, b: number, c: number) {}
48+
}
49+
`,
50+
`
51+
class Foo {
52+
constructor(a: number, b?: number, c = 1) {}
53+
}
54+
`,
55+
`
56+
class Foo {
57+
constructor(a: number, b = 1, c?: number) {}
58+
}
59+
`,
60+
`
61+
class Foo {
62+
constructor(public a: number, protected b: number, private c: number) {}
63+
}
64+
`,
65+
`
66+
class Foo {
67+
constructor(public a: number, protected b?: number, private c = 10) {}
68+
}
69+
`,
70+
`
71+
class Foo {
72+
constructor(public a: number, protected b = 10, private c?: number) {}
73+
}
74+
`,
75+
`
76+
class Foo {
77+
constructor(a: number, protected b?: number, private c = 0) {}
78+
}
79+
`,
80+
`
81+
class Foo {
82+
constructor(a: number, b?: number, private c = 0) {}
83+
}
84+
`,
85+
`
86+
class Foo {
87+
constructor(a: number, private b?: number, c = 0) {}
88+
}
89+
`,
4590
],
4691
invalid: [
4792
{
@@ -527,5 +572,95 @@ ruleTester.run('default-param-last', rule, {
527572
},
528573
],
529574
},
575+
{
576+
code: `
577+
class Foo {
578+
constructor(public a: number, protected b?: number, private c: number) {}
579+
}
580+
`,
581+
errors: [
582+
{
583+
messageId: 'shouldBeLast',
584+
line: 3,
585+
column: 33,
586+
endColumn: 53,
587+
},
588+
],
589+
},
590+
{
591+
code: `
592+
class Foo {
593+
constructor(public a: number, protected b = 0, private c: number) {}
594+
}
595+
`,
596+
errors: [
597+
{
598+
messageId: 'shouldBeLast',
599+
line: 3,
600+
column: 33,
601+
endColumn: 48,
602+
},
603+
],
604+
},
605+
{
606+
code: `
607+
class Foo {
608+
constructor(public a?: number, private b: number) {}
609+
}
610+
`,
611+
errors: [
612+
{
613+
messageId: 'shouldBeLast',
614+
line: 3,
615+
column: 15,
616+
endColumn: 32,
617+
},
618+
],
619+
},
620+
{
621+
code: `
622+
class Foo {
623+
constructor(public a = 0, private b: number) {}
624+
}
625+
`,
626+
errors: [
627+
{
628+
messageId: 'shouldBeLast',
629+
line: 3,
630+
column: 15,
631+
endColumn: 27,
632+
},
633+
],
634+
},
635+
{
636+
code: `
637+
class Foo {
638+
constructor(a = 0, b: number) {}
639+
}
640+
`,
641+
errors: [
642+
{
643+
messageId: 'shouldBeLast',
644+
line: 3,
645+
column: 15,
646+
endColumn: 20,
647+
},
648+
],
649+
},
650+
{
651+
code: `
652+
class Foo {
653+
constructor(a?: number, b: number) {}
654+
}
655+
`,
656+
errors: [
657+
{
658+
messageId: 'shouldBeLast',
659+
line: 3,
660+
column: 15,
661+
endColumn: 25,
662+
},
663+
],
664+
},
530665
],
531666
});

0 commit comments

Comments
 (0)