Skip to content

Commit

Permalink
fix(eslint-plugin): [typedef] handle AssignmentPattern inside TSParam…
Browse files Browse the repository at this point in the history
…eterProperty (#923)
  • Loading branch information
mrmlnc authored and bradzacher committed Aug 30, 2019
1 parent a4e625f commit 6bd7f2d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/eslint-plugin/src/rules/typedef.ts
Expand Up @@ -79,6 +79,15 @@ export default util.createRule<[Options], MessageIds>({
break;
case AST_NODE_TYPES.TSParameterProperty:
annotationNode = param.parameter;

// Check TS parameter property with default value like `constructor(private param: string = 'something') {}`
if (
annotationNode &&
annotationNode.type === AST_NODE_TYPES.AssignmentPattern
) {
annotationNode = annotationNode.left;
}

break;
default:
annotationNode = param;
Expand Down
47 changes: 47 additions & 0 deletions packages/eslint-plugin/tests/rules/typedef.test.ts
Expand Up @@ -109,6 +109,19 @@ ruleTester.run('typedef', rule, {
`function receivesString({ a }: { a: string }): void { }`,
`function receivesStrings({ a, b }: { [i: string ]: string }): void { }`,
`function receivesNumber(a: number = 123): void { }`,
// Constructor parameters
`class Test {
constructor() {}
}`,
`class Test {
constructor(param: string) {}
}`,
`class Test {
constructor(param: string = 'something') {}
}`,
`class Test {
constructor(private param: string = 'something') {}
}`,
// Method parameters
`class Test {
public method(x: number): number { return x; }
Expand Down Expand Up @@ -401,6 +414,40 @@ ruleTester.run('typedef', rule, {
},
],
},
// Constructor parameters
{
code: `class Test {
constructor(param) {}
}`,
errors: [
{
column: 21,
messageId: 'expectedTypedefNamed',
},
],
},
{
code: `class Test {
constructor(param = 'something') {}
}`,
errors: [
{
column: 21,
messageId: 'expectedTypedef',
},
],
},
{
code: `class Test {
constructor(private param = 'something') {}
}`,
errors: [
{
column: 21,
messageId: 'expectedTypedef',
},
],
},
// Method parameters
{
code: `class Test {
Expand Down

0 comments on commit 6bd7f2d

Please sign in to comment.