-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Enhancement: [prefer-nullish-coalescing] should allow if statement assignment (??=) to be optional #10995
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
Comments
This comment has been minimized.
This comment has been minimized.
Hi @asselin ! Yep, that's a good point, this should be configurable with an option, like the rest of the checks in this rule. @bobber205 I understand mistakes can be frustrating, but please avoid jumping straight to language like calling things "insane". This was nothing but a simple oversight which is easy to rectify. We, meaning us on the team as well as the generous community contributors who pitch and implement features, do our best to get things right the first time. But, sometimes things slip through the cracks, and we are grateful for community feedback such as this issue report to find out about them so that we can fix them. |
cc @OlivierZal |
I'm definitely +1 on giving an option for this, but am surprised at the immediate and strong pushback about how undesirable enforcing This is not a request to engage angrily or debate, I just want to understand 🙂 |
@kirkwaiblinger, I can take it. Do we go with an opt-in or an opt-out? Opt-in is less breaking-change. |
Personally, I'd say just opt-out because
But not strongly opinionated, either way is fine by me 👍 |
On the naming, the proposed |
if (!foo) {
foo = callAFunctionToGetAValue();
}
// vs
foo ??= callAFunctionToGetAValue(); 🤷 Looks clearer and cleaner to me but hey it's a stylistic decision I guess. |
I'm good with opt-out. I expect when upgrading eslint that there will be issues to deal with, either fixing code, or shutting off new rules / adding options to opt out of new behavior. Thank you @kirkwaiblinger, @OlivierZal, @JoshuaKGoldberg, @bradzacher for all your work on eslint! |
You asked for it, now you pay the penalty of reading my ramblings about how I read and understand code :) When I read:
My internal dialog is "If not foo, then foo equals .......", which immediately triggers "this is setting a default". "??=" is a shorthand that I still need to mentally expand to understand what it's doing:
"Foo question-question-equals callAFunctionToGetAValue.... that's the same as foo equals foo question-question callAFunctionToGetAValue". Then I have to picture that in my mind:
Then go back and start parsing again: "foo equals foo or* callAFunctionToGetAValue. ... ok, that's setting a default". "or*" is my mental "this is almost or (||) but not exactly... remember that in case it's important" I think "??" is now getting to a point of general adoption with the vast population of JS/TS devs, and this rule (particularly with autofixes) helps drive it. But I don't think "??=" is there yet. Part of the problem may be that there's no good name to use when reading "??" or "??=" (contrast with e.g. "plus-equals" for +=, or "or-equals" for ||=). I also don't think just recognizing "??=" as a pattern and internalizing it as an idiom is widespread yet. So I would expect many other devs are going to go through the same mental expansion process, which is of course much slower than speed reading through an if statement. My 2c |
Nitpick about the equivalence. foo ??= callAFunctionToGetAValue();
// equivalent to
foo ?? (foo = callAFunctionToGetAValue()); This is an minor distinction from your interpretation - your interpretation always does an assignment, regardless of if |
This comment has been minimized.
This comment has been minimized.
FWIW the new behavior of this rule was also polarizing at my company, so having an option to disable it sounds good. However, personally I don't think that the current behavior makes code any stylistically worse, and certainly not significantly worse. If one is willing to use |
Personally, I like that you only have to specify the assignee once 🤷♂ if (foo.bar == null) {
foo.bat = 'typo, whoops';
}
// vs
foo.bar ??= 'all good'; but we're all on the same page that it's totally subjective |
Before You File a Proposal Please Confirm You Have Done The Following...
My proposal is suitable for this project
Link to the rule's documentation
https://typescript-eslint.io/rules/prefer-nullish-coalescing/
Description
This #10829 recent enhancement makes the rule check and recommend nullish coalescing for if() statements. IMO, this is not simpler to read, and won't include this to my organization's set of coding rules. The other recommendations from this rule are useful, and I'd like to keep it enabled, but this rule doesn't have a way to turn off the recommendation for the if() case specifically.
Please add an option to disable nullish coalescing checks for if() statements. Suggested name: ignoreIfStatements
Fail
Pass
Additional Info
No response
The text was updated successfully, but these errors were encountered: