-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): [no-unnecessary-condition][strict-boolean-expressions] add option to make the rules error on files without strictNullChecks
turned on
#2345
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
Conversation
Thanks for the PR, @bradzacher! typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community. The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately. Thanks again! 🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint. As a thank you, your profile/company logo will be added to our main README which receives thousands of unique visitors per day. |
Codecov Report
@@ Coverage Diff @@
## v4 #2345 +/- ##
=======================================
Coverage 92.86% 92.86%
=======================================
Files 286 286
Lines 9064 9073 +9
Branches 2517 2521 +4
=======================================
+ Hits 8417 8426 +9
Misses 319 319
Partials 328 328
Flags with carried forward coverage won't be shown. Click here to find out more.
|
If it requires manual enabling, then an error is okay. If we ever want to add strict-bool-exprs to the recommended set (it's not recommended IIRC), then it would make more sense to make it do nothing, to not everwhelm the new users with errors. So the question is whether are there any plans to include strict-bool-exprs in default config. I actually wanted to propose that in #1423 but I forgot 😅 |
I would love to - but I think that it's kind of stylistic (many people don't like being strict...), so no doubt we'd get a lot of push-back about it.
I'm thinking maybe we should create a 'recommended-strict' ruleset which gives us another level of recommendation for people? |
@bradzacher We had this talk about I tried enabling this rule (the 3.0 version) on a few projects at work, and guess what. It reported a lot of errors, and upon looking at them, about half of them looked like an actual error or oversight, something that could potentially cause a bug in the runtime. I think we should make this rule even more user friendly: suggest using nullish coalescing in the message, provide suggestion or auto fix for the common cases, etc, and then add it to the recommended set. As for |
Definitely! I could see offering a whole suite of suggestions, such as:declare const foo: string | null | undefined;
declare const bar: string | null;
declare const baz: string | undefined;
// input
if (foo) {}
if (bar) {}
if (baz) {}
// suggestion 1 - strict equality
if (foo !== null && foo !== undefined && foo !== '') {}
if (bar !== null && bar !== '') {}
if (baz !== undefined && baz !== '') {}
// suggestion 2 - loose nullish equality
if (foo != null && foo !== '') {}
if (bar != null && bar !== '') {}
if (baz != null && baz !== '') {}
// suggestion 3 - just strict nullish
if (foo !== null && foo !== undefined) {}
if (bar !== null) {}
if (baz !== undefined) {}
// suggestion 4 - just loose nullish
if (foo != null) {}
if (bar != null) {}
if (baz != null) {}
// suggestion 5 - just string equality
if (foo !== '') {}
if (bar !== '') {}
if (baz !== '') {} We could also add a configurable autofixer with two options eg My concern with adding it to recommended is that there's a lot of users who would rather do I 100% think there's value in the rule, and since working with flow (where this is a forced part of the typechecker itself), I've come to see tangible value in it - but I know a lot of people will hate it, and I don't know if I can deal with the very vocal minority fighting our recommended set again. |
cf29440
to
12b9c8a
Compare
…essions] add option to make the rules error on files without `strictNullChecks` turned on
c40023e
to
0a9eeed
Compare
If the default options are used, then simple nullish coalescing could be suggested: if (foo) {}
// fixed
if (foo ?? "") {}
if (foo ?? 0) {}
if (foo ?? false) {} I like how this makes it explicit what's happening, so that the developer has to think "do I really want that behavior?". If the developer is not very familiar with JS it will force them to find out what
I don't see any value in suggesting anything other than I'm in favor of the following solution:
I really really hate pointing out these errors to colleagues. They are like a plague. Sometimes I feel like a bad person for pointing them out everytime. Even senior developers sometimes make them. |
Some codebases don't use If the codebase is using the eqeqeq rule with the default option, then fixing to Side note - some codebases also go as far as to do
|
I've never worked with such codebase. I know that transpilers generate code like this, because some weird obsolete DOM properties behave differently otherwise (document.all?). It could be supported as a rule option though.
Yeah, but that's normal that rules can collide with each other. Even typescript-eslint has colliding rules (eg. require-await and require-async, I don't remember the names exactly). As long as they're not colliding with other recommended sets it should be okay to include them as recommended IMO. eqeqeq is not in the recommended eslint config. I do use it but I use the "smart" option which is compatible.
TypeScript already guards against it. Also I think it's a runtime error to reassign undefined in module context (they get "use strict" by default), but I might be wrong. It could also be supported as an option, but not the default.
I don't like how this changes the semantics when the types are wrong. If you're writing a library, but the user doesn't use TS, then
That's probably because this syntax didn't exist until recently. I still like it the most for the reasons I already said.
That would be fine. The only question remaining is which should be in the recommended set. I vote for "nullish-coalescing". The other option is to not autofix, but show all the possibilities as suggestions by default. Also note that the "nullish-coalescing" fix/suggestion is the only one which doesn't change the runtime semantics. The other ones change the condition from checking for falsy to checking for nullish. Autofixing to nullish-coalescing doesn't change anything. It only makes it explicit what's happening. |
…sions] add option to make the rules error on files without `strictNullChecks` turned on (#2345)
…sions] add option to make the rules error on files without `strictNullChecks` turned on (#2345)
…sions] add option to make the rules error on files without `strictNullChecks` turned on (#2345)
I'm very much sick of closing issues about these rules by users not using
strictNullChecks
.I wish that TS would just make this true by default...
This will make the rules report an unsilenceable error for every file that is not using
strictNullChecks
.cc rule authors @phaux and @Retsam as you're both passionate about these rules.
Do you think this is a valid change for both rules?