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): [@typescript-eslint/typedef] support default value for parameter #785

Merged
merged 3 commits into from Aug 2, 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
19 changes: 15 additions & 4 deletions packages/eslint-plugin/src/rules/typedef.ts
Expand Up @@ -69,10 +69,21 @@ export default util.createRule<[Options], MessageIds>({

function checkParameters(params: TSESTree.Parameter[]) {
for (const param of params) {
if (
param.type !== AST_NODE_TYPES.TSParameterProperty &&
!param.typeAnnotation
) {
let annotationNode: TSESTree.Node | undefined;

switch (param.type) {
case AST_NODE_TYPES.AssignmentPattern:
annotationNode = param.left;
break;
case AST_NODE_TYPES.TSParameterProperty:
annotationNode = param.parameter;
break;
default:
annotationNode = param;
break;
}

if (annotationNode !== undefined && !annotationNode.typeAnnotation) {
report(param, getNodeName(param));
}
}
Expand Down
46 changes: 44 additions & 2 deletions packages/eslint-plugin/tests/rules/typedef.test.ts
Expand Up @@ -101,13 +101,21 @@ ruleTester.run('typedef', rule, {
},
],
},
// Parameters
// Function parameters
`function receivesNumber(a: number): void { }`,
`function receivesStrings(a: string, b: string): void { }`,
`function receivesNumber([a]: [number]): void { }`,
`function receivesNumbers([a, b]: number[]): void { }`,
`function receivesString({ a }: { a: string }): void { }`,
`function receivesStrings({ a, b }: { [i: string ]: string }): void { }`,
`function receivesNumber(a: number = 123): void { }`,
// Method parameters
`class Test {
public method(x: number): number { return x; }
}`,
`class Test {
public method(x: number = 123): number { return x; }
}`,
// Property declarations
`type Test = {
member: number;
Expand Down Expand Up @@ -291,7 +299,7 @@ ruleTester.run('typedef', rule, {
},
],
},
// Parameters
// Function parameters
{
code: `function receivesNumber(a): void { }`,
errors: [
Expand Down Expand Up @@ -350,6 +358,40 @@ ruleTester.run('typedef', rule, {
},
],
},
// Method parameters
{
code: `class Test {
public method(x): number { return x; }
}`,
errors: [
{
column: 23,
messageId: 'expectedTypedefNamed',
},
],
},
{
code: `class Test {
public method(x = 123): number { return x; }
}`,
errors: [
{
column: 23,
messageId: 'expectedTypedef',
},
],
},
{
code: `class Test {
public constructor(public x) { }
}`,
errors: [
{
column: 28,
messageId: 'expectedTypedef',
},
],
},
// Property declarations
{
code: `type Test = {
Expand Down