From f1c3b18f7aadc81f7dca7aa32aa1a8fe424e04e7 Mon Sep 17 00:00:00 2001 From: G r e y Date: Mon, 13 Apr 2020 11:28:56 -0500 Subject: [PATCH] fix(eslint-plugin): [unbound-method] false positive on property function initializer (#1890) --- packages/eslint-plugin/src/rules/unbound-method.ts | 5 +++++ .../tests/rules/unbound-method.test.ts | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/packages/eslint-plugin/src/rules/unbound-method.ts b/packages/eslint-plugin/src/rules/unbound-method.ts index 629aec50cca..fde58dc4e6f 100644 --- a/packages/eslint-plugin/src/rules/unbound-method.ts +++ b/packages/eslint-plugin/src/rules/unbound-method.ts @@ -198,6 +198,11 @@ function isDangerousMethod(symbol: ts.Symbol, ignoreStatic: boolean): boolean { } switch (valueDeclaration.kind) { + case ts.SyntaxKind.PropertyDeclaration: + return ( + (valueDeclaration as ts.PropertyDeclaration).initializer?.kind === + ts.SyntaxKind.FunctionExpression + ); case ts.SyntaxKind.MethodDeclaration: case ts.SyntaxKind.MethodSignature: return !( diff --git a/packages/eslint-plugin/tests/rules/unbound-method.test.ts b/packages/eslint-plugin/tests/rules/unbound-method.test.ts index 733b81e16b1..df1d50ac136 100644 --- a/packages/eslint-plugin/tests/rules/unbound-method.test.ts +++ b/packages/eslint-plugin/tests/rules/unbound-method.test.ts @@ -367,5 +367,19 @@ instance.unbound = x; // THIS SHOULD NOT }, ], }, + { + code: ` +class Foo { + unbound = function() {}; +} +const unbound = new Foo().unbound; + `, + errors: [ + { + line: 5, + messageId: 'unbound', + }, + ], + }, ], });