-
Notifications
You must be signed in to change notification settings - Fork 889
Whitelist option for no-unbound-method
#4472
Whitelist option for no-unbound-method
#4472
Conversation
Thanks for your interest in palantir/tslint, @piotrgajow! Before we can accept your pull request, you need to sign our contributor license agreement - just visit https://cla.palantir.com/ and follow the instructions. Once you sign, I'll automatically update this pull request. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exciting!
I see that some tests are failing... Can anyone tell me what is the difference between |
Hi @piotrgajow! The difference is which TypeScript version the tests are running against. TSLint supports all the versions listed in the tests, down to 2.1. It'd be a breaking change to drop support, even though it's a little annoying how some APIs have changed or added since then. Suggestion: try installing TypeScript 2.1 locally and compile against it? It could be there's an API change that is missed. Feel free to tag me if that doesn't catch it and I can take a deeper look! This'd be a really great PR to get in. |
Hi @JoshuaKGoldberg, I have tried installing Typescript 2.1/2.4 locally, but then I didn't manage to run the tests - it fails during compilation phase with:
But these files are not strictly related to changes from this PR... I have installed typescipt with version 2.1, and then removed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found a couple edge cases 😉
src/rules/noUnboundMethodRule.ts
Outdated
return allowTypeof; | ||
} | ||
if (isCallExpression(node.parent)) { | ||
const expression = node.parent.expression as ts.Identifier; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as ts.Identifier
It's actually possible for node.parent.expression
to not be an identifier. You'll want to add test cases for:
(condition ? expectA : expectB)(c.method);
(await someObject)(c.method);
(await someMethod())(c.method);
I don't think we should worry about trying to allow these even if expectA
and expectB
are whitelisted. Suggestion: just don't run this check if the parent's expression isn't an identifier.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, changed as you suggested. I maybe might include it in some future PR
src/rules/noUnboundMethodRule.ts
Outdated
} | ||
if (isCallExpression(node.parent)) { | ||
const expression = node.parent.expression as ts.Identifier; | ||
const callingMethodName = expression.escapedText as string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expression.escapedText
Looks like escapedText
is what's giving you trouble in in TS<=2.4. Switch to .text
and they seem to work fine: https://circleci.com/gh/JoshuaKGoldberg/tslint/1267
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that was the issue. Thanks!
AH, that's fun. I guess you'd have to compile in newer TS so it would understand your syntax, then run using a lower version TS. See comments in PR for resolution! 😊 |
- Ignore case when parent of call expression is not an identifier
It seems that I finally managed to fix all of the issues 😃 . |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎊, thanks @piotrgajow!
This ended up being a tricker option to add than it looked, so +1 for getting this through! Very excited to not have to disable the rule in tests. 😄
fixing no-side-effect-code, deprecated, no-inferred-empty-object-type, no-unnecessary-type-assertion, restrict-plus-operands tslint errors. expect is added to the whitelist for no-unbound-method, because expect doesn't call function, see palantir/tslint#4472 for more details.
fixing no-side-effect-code, deprecated, no-inferred-empty-object-type, no-unnecessary-type-assertion, restrict-plus-operands tslint errors. expect is added to the whitelist for no-unbound-method, because expect doesn't call function, see palantir/tslint#4472 for more details.
fixing no-side-effect-code, deprecated, no-inferred-empty-object-type, no-unnecessary-type-assertion, restrict-plus-operands tslint errors. expect is added to the whitelist for no-unbound-method, because expect doesn't call function, see palantir/tslint#4472 for more details.
PR checklist
no-unbound-method
in Jasmine spy have been called test #3755Overview of change:
Added configuration option to whitelist methods and operators for
no-unbound-method
rule, so that it will not report errors in cases such as:expect(x.method).toHaveBeenCalled()
typeof x.method === 'function'
Is there anything you'd like reviewers to focus on?
Please verify that options for this rule are backwards compatible.
CHANGELOG.md entry:
[new-rule-option] Added
whitelist
option tono-unbound-method