Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): [typedef] handle AssignmentPattern inside TSParameterProperty #923

Merged
merged 2 commits into from Aug 30, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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