Skip to content
Draft
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
10 changes: 10 additions & 0 deletions packages/eslint-plugin/src/rules/unbound-method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,15 @@ function checkIfMethod(
}
return checkMethod(assignee as ts.FunctionExpression, ignoreStatic);
}
case ts.SyntaxKind.PropertySignature: {
const type = (valueDeclaration as ts.PropertySignature).type;
if (type?.kind !== ts.SyntaxKind.FunctionType) {
return {
dangerous: false,
};
}
return checkMethod(type as ts.FunctionTypeNode, ignoreStatic);
}
case ts.SyntaxKind.MethodDeclaration:
case ts.SyntaxKind.MethodSignature: {
return checkMethod(
Expand All @@ -343,6 +352,7 @@ function checkIfMethod(
function checkMethod(
valueDeclaration:
| ts.FunctionExpression
| ts.FunctionTypeNode
| ts.MethodDeclaration
| ts.MethodSignature,
ignoreStatic: boolean,
Expand Down
96 changes: 94 additions & 2 deletions packages/eslint-plugin/tests/rules/unbound-method.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,11 @@ class Foo {
type foo = new ({ unbound }: Foo) => void;
`,
'const { unbound } = { unbound: () => {} };',
'function foo({ unbound }: { unbound: () => void } = { unbound: () => {} }) {}',
`
function foo(
{ unbound }: { unbound: (this: void) => void } = { unbound: () => {} },
Copy link
Member Author

Choose a reason for hiding this comment

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

To my understanding, this previously valid test is in fact invalid without a this: void parameter type.

) {}
`,
// https://github.com/typescript-eslint/typescript-eslint/issues/1866
`
class BaseClass {
Expand All @@ -480,6 +484,30 @@ class OtherClass extends BaseClass {
const oc = new OtherClass();
oc.superLogThis();
`,
`
interface Foo {
bound: (this: void) => number;
}

declare const foo: Foo;
foo.bound;
`,
`
interface Foo {
bound: (this: void) => number;
}

declare const foo: Foo;
const { bound } = foo;
`,
`
type Foo = {
bound: (this: void) => number;
};

declare const foo: Foo;
const { bound } = foo;
`,
],
invalid: [
{
Expand Down Expand Up @@ -965,7 +993,7 @@ function foo({ unbound }: { unbound: () => string } | Foo) {}
errors: [
{
line: 5,
messageId: 'unbound',
messageId: 'unboundWithoutThisAnnotation',
Copy link
Member Author

@ronami ronami Oct 5, 2025

Choose a reason for hiding this comment

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

This is interesting, it seems the rule iterates on union types and reports the first violation:

for (const intersectionPart of tsutils
.unionConstituents(services.getTypeAtLocation(node))
.flatMap(unionPart =>
tsutils.intersectionConstituents(unionPart),
)) {
const reported = checkIfMethodAndReport(
property.key,
intersectionPart.getProperty(property.key.name),
);
if (reported) {
break;
}
}

In this case, the union starts with { unbound: () => string }, with Foo being second. Currently, { unbound: () => string } doesn't report, which causes the report to come from Foo.

This PR made it so { unbound: () => string } does trigger a report, which changes the error message accordingly.

Copy link
Member Author

Choose a reason for hiding this comment

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

Somewhat related: #11683.

},
],
},
Expand Down Expand Up @@ -1252,5 +1280,69 @@ const f = objectLiteral.f;
},
],
},
{
code: `
interface Foo {
bound: () => number;
}

declare const foo: Foo;
foo.bound;
`,
errors: [
{
line: 7,
messageId: 'unboundWithoutThisAnnotation',
},
],
},
{
code: `
interface Foo {
bound: () => number;
}

declare const foo: Foo;
const { bound } = foo;
`,
errors: [
{
line: 7,
messageId: 'unboundWithoutThisAnnotation',
},
],
},
{
code: `
interface Foo {
bound: (this: Foo) => number;
}

declare const foo: Foo;
foo.bound;
`,
errors: [
{
line: 7,
messageId: 'unbound',
},
],
},
{
code: `
type Foo = {
bound: () => number;
};

declare const foo: Foo;
foo.bound;
`,
errors: [
{
line: 7,
messageId: 'unboundWithoutThisAnnotation',
},
],
},
],
});
Loading