Skip to content

Commit

Permalink
fix(eslint-plugin): [unbound-method] Work around class prototype bug (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
bradzacher committed May 9, 2019
1 parent 43fa09c commit 3219aa7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/eslint-plugin/src/rules/unbound-method.ts
Expand Up @@ -74,6 +74,10 @@ export default util.createRule<Options, MessageIds>({

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:
Expand Down
32 changes: 30 additions & 2 deletions packages/eslint-plugin/tests/rules/unbound-method.test.ts
Expand Up @@ -97,7 +97,7 @@ instane.boundStatic && 0;
ContainsMethods.boundStatic ? 1 : 0;
ContainsMethods.unboundStatic ? 1 : 0;
`,
`,
`interface RecordA {
readonly type: "A"
readonly a: {}
Expand All @@ -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: [
{
Expand Down Expand Up @@ -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',
},
],
},
],
});

0 comments on commit 3219aa7

Please sign in to comment.