From 8f56a2f0d0f0a706ffd58d293851b008c907c73b Mon Sep 17 00:00:00 2001 From: Vapurrmaid Date: Sun, 12 Apr 2020 23:40:07 -0400 Subject: [PATCH 1/2] fix(eslint-plugin): unbound prop fn initializer --- packages/eslint-plugin/src/rules/unbound-method.ts | 10 ++++++++++ .../tests/rules/unbound-method.test.ts | 14 ++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/packages/eslint-plugin/src/rules/unbound-method.ts b/packages/eslint-plugin/src/rules/unbound-method.ts index 629aec50cca2..da2be80841e7 100644 --- a/packages/eslint-plugin/src/rules/unbound-method.ts +++ b/packages/eslint-plugin/src/rules/unbound-method.ts @@ -197,6 +197,16 @@ function isDangerousMethod(symbol: ts.Symbol, ignoreStatic: boolean): boolean { return false; } + if (valueDeclaration.kind === ts.SyntaxKind.PropertyDeclaration) { + const propertyDeclaration = valueDeclaration as ts.PropertyDeclaration; + if ( + propertyDeclaration.initializer && + propertyDeclaration.initializer.kind === ts.SyntaxKind.FunctionExpression + ) { + return true; + } + } + switch (valueDeclaration.kind) { case ts.SyntaxKind.MethodDeclaration: case ts.SyntaxKind.MethodSignature: diff --git a/packages/eslint-plugin/tests/rules/unbound-method.test.ts b/packages/eslint-plugin/tests/rules/unbound-method.test.ts index 733b81e16b17..df1d50ac1369 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', + }, + ], + }, ], }); From d21e2fd30759970e283ddfda63f40909c207c728 Mon Sep 17 00:00:00 2001 From: Vapurrmaid Date: Mon, 13 Apr 2020 10:09:05 -0400 Subject: [PATCH 2/2] Review: Translate logic into switch --- .../eslint-plugin/src/rules/unbound-method.ts | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/packages/eslint-plugin/src/rules/unbound-method.ts b/packages/eslint-plugin/src/rules/unbound-method.ts index da2be80841e7..fde58dc4e6fa 100644 --- a/packages/eslint-plugin/src/rules/unbound-method.ts +++ b/packages/eslint-plugin/src/rules/unbound-method.ts @@ -197,17 +197,12 @@ function isDangerousMethod(symbol: ts.Symbol, ignoreStatic: boolean): boolean { return false; } - if (valueDeclaration.kind === ts.SyntaxKind.PropertyDeclaration) { - const propertyDeclaration = valueDeclaration as ts.PropertyDeclaration; - if ( - propertyDeclaration.initializer && - propertyDeclaration.initializer.kind === ts.SyntaxKind.FunctionExpression - ) { - return true; - } - } - 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 !(