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] Work around class prototype bug #499

Merged
merged 1 commit into from May 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
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',
},
],
},
],
});