Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Improve typedef for callback used as parameter #4533

Merged
merged 1 commit into from
Oct 5, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/rules/typedefRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class TypedefWalker extends Lint.AbstractWalker<Options> {
}

private checkArrowFunction({ parent, parameters, type }: ts.ArrowFunction): void {
if (parent.kind !== ts.SyntaxKind.CallExpression && !isTypedPropertyDeclaration(parent)) {
if (!isTypedPropertyDeclaration(parent)) {
this.checkTypeAnnotation("arrow-call-signature", parameters, type);
}
}
Expand Down
3 changes: 3 additions & 0 deletions test/rules/typedef/all/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,15 @@ class TsLint {
}

setTimeout(() => {}, 1000);
~~ [expected arrow-call-signature to have a typedef]
setTimeout(function() {}, 1000);
~~ [expected call-signature to have a typedef]

someFunc(n => n+1);
~~~ [expected arrow-call-signature to have a typedef]
~ [expected arrow-parameter: 'n' to have a typedef]
someFunc(n => {});
~~~ [expected arrow-call-signature to have a typedef]
~ [expected arrow-parameter: 'n' to have a typedef]

class A {
Expand Down
6 changes: 6 additions & 0 deletions test/rules/typedef/arrow-call-signature/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ var obj = {
}

setTimeout(() => {}, 1000);
~~ [expected arrow-call-signature to have a typedef]
setTimeout(function() {}, 1000);

const foo = n => n+1
~~~~ [expected arrow-call-signature to have a typedef]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does the failure range extend to the =? that seems a little off

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In const foo = n => n+1 location.pos = 11
in const foo = (n => n+1) location.pos = 13

I didn't touch this part


someFunc(n => n+1);
~~~ [expected arrow-call-signature to have a typedef]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, feels strange to include the ( in the failure range

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's made for (n) => n+1
.....................~~~ [expected arrow-call-signature to have a typedef]

The Node is only n.
But the code use this.addFailure(location.pos - 1, location.end + 1, failure);
I didn't touch this part

someFunc(n => {});
~~~ [expected arrow-call-signature to have a typedef]
8 changes: 8 additions & 0 deletions test/rules/typedef/arrow-parameter/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,11 @@ var NoTypesFn = function (a, b) {}
var NoTypesArrowFn = (a, b) => {}
~ [expected arrow-parameter: 'a' to have a typedef]
~ [expected arrow-parameter: 'b' to have a typedef]

someFunc(n => n+1);
~ [expected arrow-parameter: 'n' to have a typedef]
someFunc((n) => n+1);
~ [expected arrow-parameter: 'n' to have a typedef]
someFunc((n,m) => n+m);
~ [expected arrow-parameter: 'n' to have a typedef]
~ [expected arrow-parameter: 'm' to have a typedef]