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

[no-unnecessary-condition] What's the point of ignoreRhs: false option? #1017

Closed
ark120202 opened this issue Sep 29, 2019 · 17 comments · Fixed by #1163
Closed

[no-unnecessary-condition] What's the point of ignoreRhs: false option? #1017

ark120202 opened this issue Sep 29, 2019 · 17 comments · Fixed by #1163
Labels
breaking change This change will require a new major version to be released default rule options Discussions about default rule options package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Milestone

Comments

@ark120202
Copy link

ark120202 commented Sep 29, 2019

I am probably missing something, but reading through all of comments in original PR (#699) I couldn't find a reason to include ignoreRhs option. It probably made sense for strict-boolean-expressions, but I can't think of any cases where I'd want return items.length && items[0].toUpperCase() to be reported, since there is no condition part.

At the same time I think RHS should always be reported when used in a condition, like:

function test(foo: { bar: true } | undefined) {
  if (foo && foo.bar) {}
  //         ^^^^^^^
}

(note: items.length && items[0].toUpperCase() from docs isn't correct example for this rule, since toUpperCase returns possibly falsy value)

EDIT: Actually, I think it might make sense for this to be behavior for strict-boolean-expressions as well. Looks like that option was added just to avoid breaking change, see palantir/tslint#4447.

@ark120202 ark120202 added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Sep 29, 2019
@sindresorhus
Copy link

Here's a use-case: xojs/eslint-config-xo-typescript#17

// @EdJoPaTo @yangmingshan

@bradzacher
Copy link
Member

With rules it's much better to implement them with zero options and relax them over time as people find cases that are reasonable to want to ignore.

In this case, the rule was implemented strictly, making the assumption that you didn't use short circuiting - && is a boolean operator, after all.

Then someone decided to add the option, because they felt that it should be safe to allow short circuiting.

Whilst adding a new option is a minor feature bump, turning on the option as default is a major breaking bump.

I'm not sure if it's a good idea to turn it on by default - in 3.7 they are implementing optional chaining and nullish coalescing, which will handle many of the cases people use short circuiting for.

That's a point for discussion however

@bradzacher bradzacher added recommended-rules Discussion about recommended rule sets and removed triage Waiting for maintainers to take a look labels Oct 2, 2019
@bradzacher bradzacher added this to the 3.0.0 milestone Oct 2, 2019
@bradzacher bradzacher added default rule options Discussions about default rule options and removed recommended-rules Discussion about recommended rule sets labels Oct 2, 2019
@ark120202
Copy link
Author

In this case, the rule was implemented strictly, making the assumption that you didn't use short circuiting

I guess that's about strict-boolean-expressions rule? In no-unnecessary-condition it was there since the first commit: 5ec23e1#diff-1dcdebcfacc649bba21e6ffe12f28ba1R43.

&& is a boolean operator, after all

That's a good point, I can see how someone would like to enforce that and it fits strict-boolean-expressions well, but I don't see why it should be in no-unnecessary-condition

@bradzacher
Copy link
Member

Sorry, I forget which one had what at PR time.
In that case it was that the PR author created the rule via copy pasting strict-boolean-expressions, and then didn't think about changing the option to be default on.
I think I had a busy week, so I didn't review it in depth enough to make a decision on if the options should be on or if it should even exist.

I'm not sure if there is a case for having the option turned off in no-unnecessary-condition.

@Retsam
Copy link
Contributor

Retsam commented Oct 21, 2019

Original author here, unless I'm misreading @ark120202's comment, it's not a question of changing the default; it's that the option is unnecessary and can be removed altogether.

And I think they're right. Instead of using an option to determine when the right-hand-side should be checked, it should be done by the context of the expression.

In the context of a condition, (e.g. if(foo && bar)) it should always check bar to ensure the condition is necessary. In other contexts (e.g. return foo && bar; it doesn't ever make sense to check bar.

I'll see if I can make the changes to make this happen. I'm not sure if it'll count as a breaking change to remove an option and unify both cases.

@bradzacher
Copy link
Member

Related: #1118

Removing an option is a breaking change. It is not backward compatible.

@jzarzeckis

This comment has been minimized.

@glen-84

This comment has been minimized.

@jzarzeckis

This comment has been minimized.

@glen-84

This comment has been minimized.

@jzarzeckis

This comment has been minimized.

@ark120202

This comment has been minimized.

@glen-84

This comment has been minimized.

@jzarzeckis

This comment has been minimized.

@glen-84

This comment has been minimized.

@jzarzeckis

This comment has been minimized.

@bradzacher
Copy link
Member

bradzacher commented Dec 5, 2019

Commenting on an issue like this is not the place to report or discuss unrelated bugs.
Next time start with opening a new issue and fill out the template, so we can look into the problem with all the information required.

@typescript-eslint typescript-eslint locked as off-topic and limited conversation to collaborators Dec 5, 2019
@typescript-eslint typescript-eslint unlocked this conversation Dec 5, 2019
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
breaking change This change will require a new major version to be released default rule options Discussions about default rule options package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants