From 6bd7f2d0c0df0361b96509e337e60c77467fde36 Mon Sep 17 00:00:00 2001 From: Denis Malinochkin Date: Fri, 30 Aug 2019 04:32:03 +0300 Subject: [PATCH] fix(eslint-plugin): [typedef] handle AssignmentPattern inside TSParameterProperty (#923) --- packages/eslint-plugin/src/rules/typedef.ts | 9 ++++ .../eslint-plugin/tests/rules/typedef.test.ts | 47 +++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/packages/eslint-plugin/src/rules/typedef.ts b/packages/eslint-plugin/src/rules/typedef.ts index 3d9d76f3872..19c4244d9f9 100644 --- a/packages/eslint-plugin/src/rules/typedef.ts +++ b/packages/eslint-plugin/src/rules/typedef.ts @@ -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; diff --git a/packages/eslint-plugin/tests/rules/typedef.test.ts b/packages/eslint-plugin/tests/rules/typedef.test.ts index d5ea514f59e..c9e35c356eb 100644 --- a/packages/eslint-plugin/tests/rules/typedef.test.ts +++ b/packages/eslint-plugin/tests/rules/typedef.test.ts @@ -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; } @@ -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 {