Skip to content

Commit

Permalink
fix(eslint-plugin): [typedef] support default value for parameter (#785)
Browse files Browse the repository at this point in the history
  • Loading branch information
octogonz authored and bradzacher committed Aug 2, 2019
1 parent 73f8c79 commit 84916e6
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
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

0 comments on commit 84916e6

Please sign in to comment.