Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Whitelist option for no-unbound-method #4472

Merged

Conversation

piotrgajow
Copy link
Contributor

PR checklist

Overview 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 to no-unbound-method

@palantirtech
Copy link
Member

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.

Copy link
Contributor

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exciting!

src/rules/noUnboundMethodRule.ts Outdated Show resolved Hide resolved
src/rules/noUnboundMethodRule.ts Outdated Show resolved Hide resolved
src/rules/noUnboundMethodRule.ts Outdated Show resolved Hide resolved
src/rules/noUnboundMethodRule.ts Outdated Show resolved Hide resolved
src/rules/noUnboundMethodRule.ts Outdated Show resolved Hide resolved
@piotrgajow
Copy link
Contributor Author

I see that some tests are failing... Can anyone tell me what is the difference between test2.1, test2.4 and for example test2.7?

@JoshuaKGoldberg
Copy link
Contributor

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.

@piotrgajow
Copy link
Contributor Author

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:

$ yarn compile:test && yarn test:rules
yarn run v1.10.1
$ tsc -p test
src/formatterLoader.ts(73,17): error TS1005: '(' expected.
src/formatterLoader.ts(89,13): error TS1005: '(' expected.
src/language/utils.ts(128,49): error TS1005: ',' expected.
src/language/utils.ts(128,53): error TS1005: ',' expected.
src/ruleLoader.ts(122,13): error TS1005: '(' expected.
src/rules/fileNameCasingRule.ts(62,13): error TS1005: '(' expected.
src/rules/noImplicitDependenciesRule.ts(159,17): error TS1005: '(' expected.
src/runner.ts(149,28): error TS1109: Expression expected.
src/runner.ts(149,34): error TS1134: Variable declaration expected.
src/utils.ts(292,13): error TS1005: '(' expected.
error Command failed with exit code 2.

But these files are not strictly related to changes from this PR...
Could you maybe direct me somehow?
Maybe I did something wrong?

I have installed typescipt with version 2.1, and then removed strictFunctionTypes and strictPropertyInitialization from tsconfig.json as compiler complained that these options are unknown.

Copy link
Contributor

@JoshuaKGoldberg JoshuaKGoldberg left a 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 😉

return allowTypeof;
}
if (isCallExpression(node.parent)) {
const expression = node.parent.expression as ts.Identifier;
Copy link
Contributor

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.

Copy link
Contributor Author

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

}
if (isCallExpression(node.parent)) {
const expression = node.parent.expression as ts.Identifier;
const callingMethodName = expression.escapedText as string;
Copy link
Contributor

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

Copy link
Contributor Author

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!

@JoshuaKGoldberg
Copy link
Contributor

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! 😊

@piotrgajow
Copy link
Contributor Author

It seems that I finally managed to fix all of the issues 😃 .

Copy link
Contributor

@JoshuaKGoldberg JoshuaKGoldberg left a 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. 😄

@JoshuaKGoldberg JoshuaKGoldberg merged commit 39201ac into palantir:master Feb 3, 2019
Margar1ta pushed a commit to Margar1ta/mosaic that referenced this pull request Oct 10, 2019
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.
pimenovoleg pushed a commit to positive-js/mosaic that referenced this pull request Oct 10, 2019
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.
pimenovoleg pushed a commit to positive-js/mosaic that referenced this pull request Oct 18, 2019
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.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants