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

Rule proposal: prefer-t-regex #163

Closed
sindresorhus opened this issue Jan 7, 2017 · 5 comments · Fixed by #247
Closed

Rule proposal: prefer-t-regex #163

sindresorhus opened this issue Jan 7, 2017 · 5 comments · Fixed by #247
Labels
enhancement 🎁 Rewarded on Issuehunt This issue has been rewarded on Issuehunt help wanted

Comments

@sindresorhus
Copy link
Member

sindresorhus commented Jan 7, 2017

Issuehunt badges

Users should prefer the regex assertion as it's more readable and gives better output.

Fail

t.true(/\d+/.test(foo()));

Pass

t.regex(foo(), /\d+/);

Same for the negated ones, with t.notRegex.


Would also be useful to be able to detect the regex from a variable somewhere else in the scope as I often share a regex between multiple asserts:

const re = /\d+/;



t.true(re.test(foo()));

This could be auto-fixable, right?


Should we also detect the following?

t.true(foo().search(/\d+/));
t.truthy(foo().match(/\d+/));
IssueHunt Summary

gmartigny gmartigny has been rewarded.

Sponsors (Total: $60.00)

Tips

@jfmengels
Copy link
Contributor

Sounds good to me 👍
This sounds auto-fixable yes.

In the case of t.true(re.test(foo()));, I would think that .test() inside a t.true() call is a sign of a regex, so I think we can report it. Maybe not auto-fix it for safety's sake.

Should we also detect the following?

t.true(foo().search(/\d+/));

Didn't know this API 🤔

t.truthy(foo().match(/\d+/));

For both of these, if they have regexes as the sole argument, I'd say yes. For match, I'd personnally even like it if got autofixed to .test() and t.true

@sindresorhus
Copy link
Member Author

For match, I'd personnally even like it if got autofixed to .test() and t.true

Huh? Why not t.regex()?

@jfmengels
Copy link
Contributor

Ha, yes, that's better obviously 😅

@IssueHuntBot
Copy link

@IssueHunt has funded $60.00 to this issue.


GMartigny added a commit to GMartigny/eslint-plugin-ava that referenced this issue Apr 22, 2019
@issuehunt-oss issuehunt-oss bot added the 💵 Funded on Issuehunt This issue has been funded on Issuehunt label May 10, 2019
@issuehunt-oss issuehunt-oss bot added 🎁 Rewarded on Issuehunt This issue has been rewarded on Issuehunt and removed 💵 Funded on Issuehunt This issue has been funded on Issuehunt labels May 29, 2019
@IssueHuntBot
Copy link

@sindresorhus has rewarded $54.00 to @GMartigny. See it on IssueHunt

  • 💰 Total deposit: $60.00
  • 🎉 Repository reward(0%): $0.00
  • 🔧 Service fee(10%): $6.00

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement 🎁 Rewarded on Issuehunt This issue has been rewarded on Issuehunt help wanted
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants