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): [unbound-method] ignore assignments _to_ methods #1736

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions packages/eslint-plugin/src/rules/unbound-method.ts
Expand Up @@ -241,6 +241,9 @@ function isSafeUse(node: TSESTree.Node): boolean {
case AST_NODE_TYPES.BinaryExpression:
return ['instanceof', '==', '!=', '===', '!=='].includes(parent.operator);

case AST_NODE_TYPES.AssignmentExpression:
return parent.operator === '=';
bradzacher marked this conversation as resolved.
Show resolved Hide resolved

case AST_NODE_TYPES.TSNonNullExpression:
case AST_NODE_TYPES.TSAsExpression:
case AST_NODE_TYPES.TSTypeAssertion:
Expand Down
12 changes: 12 additions & 0 deletions packages/eslint-plugin/tests/rules/unbound-method.test.ts
Expand Up @@ -147,6 +147,9 @@ ruleTester.run('unbound-method', rule, {

"typeof ContainsMethods.boundStatic === 'function';",
"typeof ContainsMethods.unboundStatic === 'function';",

'instance.unbound = () => {};',
'instance.unbound = instance.unbound.bind(instance);',
].map(addContainsMethodsClass),
`
interface RecordA {
Expand Down Expand Up @@ -211,6 +214,15 @@ if(myCondition || x.mightBeDefined) {
console.log('hello world')
}
`,
// https://github.com/typescript-eslint/typescript-eslint/issues/1256
`
class A {
unbound(): void {
this.unbound = undefined;
this.unbound = this.unbound.bind(this);
}
}
`,
],
invalid: [
{
Expand Down