Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(eslint-plugin): [unbound-method] report property signatures with function types#11682

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
ronami wants to merge1 commit intotypescript-eslint:main
base:main
Choose a base branch
Loading
fromronami:unbound-method-property-signature
Draft
Show file tree
Hide file tree
Changes fromall 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 deletionspackages/eslint-plugin/src/rules/unbound-method.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionspackages/eslint-plugin/tests/rules/unbound-method.test.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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
MemberAuthor

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 athis: 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 DownExpand Up@@ -965,7 +993,7 @@ function foo({ unbound }: { unbound: () => string } | Foo) {}
errors: [
{
line: 5,
messageId: 'unbound',
messageId: 'unboundWithoutThisAnnotation',
Copy link
MemberAuthor

@ronamironamiOct 5, 2025
edited
Loading

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(constintersectionPartoftsutils
.unionConstituents(services.getTypeAtLocation(node))
.flatMap(unionPart=>
tsutils.intersectionConstituents(unionPart),
)){
constreported=checkIfMethodAndReport(
property.key,
intersectionPart.getProperty(property.key.name),
);
if(reported){
break;
}
}

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

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

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Somewhat related:#11683.

},
],
},
Expand DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp