-
Notifications
You must be signed in to change notification settings - Fork 238
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
feat: create no-restricted-matchers
rule
#575
Conversation
7bfa550
to
7aa5cac
Compare
🧐 |
Should we go for |
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.
This looks greta! Only nit I have is the name of the rule.
I think we should deprecated the other rules you mention, but can do that in a follow-up for a cleaner changelog 🙂
Yeah that's fair - for some reason I thought that eslint had some |
7aa5cac
to
3c1bac8
Compare
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.
Remember to verify name of rule in commit when squashing
ban-matchers
ruleno-banned-matchers
rule
Before I merge, do you have any thoughts about the using options for a I'm just conscious about changing the syntax quickly after merging and requiring a migration until the next (next) major. |
3c1bac8
to
bb99788
Compare
Hmm, rename to And I think the syntax in that rule is a good starting point. For replacements, maybe something like https://github.com/sindresorhus/eslint-plugin-unicorn/blob/master/docs/rules/prevent-abbreviations.md? |
bb99788
to
9778666
Compare
Ohh I like that implementation; but do you think it's worth implementing that now, or waiting to see if it's asked for? |
I'm fine with waiting |
no-banned-matchers
ruleno-restricted-matchers
rule
# [23.11.0](v23.10.0...v23.11.0) (2020-05-12) ### Features * create `no-restricted-matchers` rule ([#575](#575)) ([ac926e7](ac926e7))
🎉 This PR is included in version 23.11.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
This rule lets us deprecate & drop at least the following:
no-truthy-falsy
no-expect-resolves
prefer-inline-snapshots
In theory we could drop a few more, but I think they have their own context that makes them worth keeping:
prefer-called-with
: I've not come up with a nice syntax that lets you ban a matcher unless it has a modifier, so you could bantoHaveBeenCalled
but that would also bannot.toHaveBeenCalled
.prefer-strict-equals
: while this can be handled byban-matchers
, I feel like this is one we want to recommend (so having its own rule gives it a name + specific documentation), and provide an autofix/suggestion for (more on this below)no-alias-methods
: again as with the case ofprefer-strict-equals
, this has a fixer, and a history behind it.The case for syntax
This rule lets you ban
*.modifier.matcher
&*.matcher
but notexpect.matcher
. The ability to ban justmodifier.matcher
comes naturally, but I couldn't come up with a nice way of letting you ban only a matcher without any modifiers; in saying that I don't think there's a lot of gain in being able to do that.The case for naming
While this rule is called
ban-matchers
, it does let you ban modifiers as well, and in the code I've referred to the actual things that are banned as "chains".I've named the rule
ban-matchers
as "matchers" I feel is a commonly understood and used name, and so unlikely to cause confusion - it also has the desired effect of gelling with its sister@typescript-eslint
ruleban-types
(In saying that I realise the
Expect
page does refer to them all under the "methods" headers, and we do haveno-alias-methods
, so maybe "methods" is the better one to use - the more I type the more I think maybe thats the better name, except its a bit generic for what it's implying. "ban-expect-methods" maybe? 🤔)At the very least, I struggled to come up with a more catch-all term for
not
,resolves
, &rejects
than "modifier", which I don't think it used a lot, and so felt it would just be overly confusing to try and pick an exact name.The case for
no-alias-methods
Having looked over this rule, I think @SimenB a push should be made to deprecate & later remove the alias matchers from jest core.
Here's my thinking:
style
ruleset, which while not the same as being in therecommended
rule set, still counts for something in my eyes i.e we don't haveno-truthy-falsy
orprefer-called-with
- both rules that I personally think are Good, and help strengthen your tests, but didn't make the cut thatno-alias-methods
did (in saying that, maybe they should be in thestyle
ruleset?)expect.extend
in a start script to restore the aliased matchers - taking that one further, you could publish it as a package on npm; jest could even do this as@jest/expect-aliases
(or something better named - actually better yet they can be brought intojest-extended
).I don't know the history behind the aliases, and happy to be schooled on them, but I know that jest has rejected a fair few matchers in the past to keep things lean and only include the batteries it needs.
This is conversation that definitely happen over at jest, and happy to port it over, but wanted to raise it here as I felt this rule + the addition of
no-deprecated-functions
provided a good launching point for kicking off this discussion.Going down this path would mean folding
no-alias-methods
intono-deprecated-functions
.The case for options
Currently we use the second options parameter as a map for the banned matchers, which takes either
null
or a string to be used as a custom message.This means if we ever want to add options, we'd have to do a deprecation & migration.
This also means that if we wanted to recommend
ban-matchers
, it would be slightly more annoying for people to extend off.As such, we could go the same vein as
@typescript-eslint/ban-types
:We have a
matchers
property that takes the map, and then can add additional properties i.eextendDefaults
(with the "defaults" being any bans we recommend).While I don't have any bans to recommend off the top of my head, this would also provide a way that we could collapse
no-alias-methods
into this rule, by having abanAliasMethods
option.The case for fixing
@typescript-eslint/ban-types
supports afixWith
option which it uses for providing its fixer.I think having this is less important for us as jest has a far smaller number of matchers, and an even smaller number of matchers that can be swapped out without being a little dangerous.
In saying that, that's a good reason to look to use the suggestion api to provide a quick-fix.
We could take an object instead of a string for the value of the key-value pair:
This would let us fold in
prefer-strict-equal
into `ban-matchers (tho we'd still weaken the documentation).This would increase the rules complexity, but still be relatively easy to do.
closes #330
closes #545
closes #312
closes #234
closes #230