diff --git a/packages/eslint-plugin/src/rules/unbound-method.ts b/packages/eslint-plugin/src/rules/unbound-method.ts index a8a0f41b90d..6b177f1dccf 100644 --- a/packages/eslint-plugin/src/rules/unbound-method.ts +++ b/packages/eslint-plugin/src/rules/unbound-method.ts @@ -74,6 +74,10 @@ export default util.createRule({ function isDangerousMethod(symbol: ts.Symbol, ignoreStatic: boolean) { const { valueDeclaration } = symbol; + if (!valueDeclaration) { + // working around https://github.com/microsoft/TypeScript/issues/31294 + return false; + } switch (valueDeclaration.kind) { case ts.SyntaxKind.MethodDeclaration: diff --git a/packages/eslint-plugin/tests/rules/unbound-method.test.ts b/packages/eslint-plugin/tests/rules/unbound-method.test.ts index e2757630acb..061b4edf524 100644 --- a/packages/eslint-plugin/tests/rules/unbound-method.test.ts +++ b/packages/eslint-plugin/tests/rules/unbound-method.test.ts @@ -97,7 +97,7 @@ instane.boundStatic && 0; ContainsMethods.boundStatic ? 1 : 0; ContainsMethods.unboundStatic ? 1 : 0; -`, + `, `interface RecordA { readonly type: "A" readonly a: {} @@ -111,7 +111,20 @@ type AnyRecord = RecordA | RecordB function test(obj: AnyRecord) { switch (obj.type) { } -}`, +} + `, + // https://github.com/typescript-eslint/typescript-eslint/issues/496 + ` +class CommunicationError { + constructor() { + const x = CommunicationError.prototype; + } +} + `, + ` +class CommunicationError {} +const x = CommunicationError.prototype; + `, ], invalid: [ { @@ -283,5 +296,20 @@ ContainsMethods.unboundStatic; }, ], }, + // https://github.com/typescript-eslint/typescript-eslint/issues/496 + { + code: ` +class CommunicationError { + foo() {} +} +const x = CommunicationError.prototype.foo; + `, + errors: [ + { + line: 5, + messageId: 'unbound', + }, + ], + }, ], });