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] Add support for non-strict mode #996

Closed
glen-84 opened this issue Sep 22, 2019 · 15 comments
Closed

[no-unnecessary-condition] Add support for non-strict mode #996

glen-84 opened this issue Sep 22, 2019 · 15 comments
Labels
bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@glen-84
Copy link
Contributor

glen-84 commented Sep 22, 2019

Repro

{
  "rules": {
    "@typescript-eslint/no-unnecessary-condition": ["error", { "ignoreRhs": true }],
  }
}
class Thing {}

class Example {
    private test: Thing | null;

    public run(): void {
        if (this.test) { // Error here.
            console.log(this.test);
        }
    }
}

Expected Result

No error.

Actual Result

Error: Unnecessary conditional, value is always truthy.

Additional Info

n/a

Versions

package version
@typescript-eslint/eslint-plugin 2.3.0
@typescript-eslint/parser 2.3.0
TypeScript 3.5.3
ESLint 6.1.0
node 10.16.3
npm 6.9.0
@glen-84 glen-84 added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Sep 22, 2019
@bradzacher bradzacher added awaiting response Issues waiting for a reply from the OP or another party and removed triage Waiting for maintainers to take a look labels Sep 23, 2019
@bradzacher
Copy link
Member

I'm unable to reproduce this against master.
Do you have strictNullChecks on?

If you don't - then the actual type of this.test degrades to Thing - hence the rule flags the condition as unnecessary.

@glen-84
Copy link
Contributor Author

glen-84 commented Sep 24, 2019

I'll try to test this again on Friday.

@glen-84
Copy link
Contributor Author

glen-84 commented Sep 27, 2019

strictNullChecks was enabled, but I think that I had forgotten to reload VS Code, so the ESLint extension may have been running with strictNullChecks disabled.

Having said that, with strictNullChecks disabled, shouldn't:

class Thing {}

class Example {
    private test: Thing;

    public run(): void {
        if (this.test) {
            // Error here.
            console.log(this.test);
        }
    }
}

... be fine, if null and undefined are in the domain of every type?

It's possible that I am misunderstanding something, but you'd think that code like the above would be common in a world without strictNullChecks.

@bradzacher bradzacher added bug Something isn't working and removed awaiting response Issues waiting for a reply from the OP or another party labels Sep 28, 2019
@bradzacher
Copy link
Member

You're correct in your understanding of what happens when it's turned off.

But incorrect in the understanding of how the typechecker reports types.
When strict mode is off, the types reported by the checker API aren't any different to when it's on.

So when we query if the type is nullable etc, the checker api just tells us that it's of type Thing - it's up to us to implement the non-strict logic.

We've done it for a few rules, so we need to add it where appropriate.

@glen-84
Copy link
Contributor Author

glen-84 commented Sep 28, 2019

Ya, I knew that it was being reported as Thing (by hovering over the symbol in VS Code), I just wasn't sure whether the APIs provided additional metadata, or whether it had to be handled manually.

@Stephen2
Copy link

Stephen2 commented Oct 2, 2019

Am I seeing the same thing here?
image

image

@Stephen2
Copy link

Stephen2 commented Oct 2, 2019

Seems inaccurate to me. Just started happening after an upgrade, I'm gonna rollback and see what happens

EDIT: Actually, there's something else going on. I'm narrowing it down, and will report back.

@Stephen2
Copy link

Stephen2 commented Oct 2, 2019

Confirmed, going from 2.1.0 -> 2.2.0 causes these false positives for me.

I even rolled back typescript version to 3.5.3 because I was getting the giant warning message about being outside estree supported version

But still no luck.

@Stephen2
Copy link

Stephen2 commented Oct 2, 2019

blerg... I tried hacking up node-modules to figure out what might be the cause, but I didn't understand it :(

Sorry... I tried to help

@glen-84
Copy link
Contributor Author

glen-84 commented Oct 2, 2019

@Stephen2 Are you using "ignoreRhs": true?

@Stephen2
Copy link

Stephen2 commented Oct 2, 2019

I'm not

@Stephen2
Copy link

Stephen2 commented Oct 2, 2019

Turning that on makes the list of 20 silly ones go down to 3 mostly reasonable ones 🍾

Am I misunderstanding how the rules works?

@glen-84
Copy link
Contributor Author

glen-84 commented Oct 2, 2019

@Stephen2 #1017.

@bradzacher bradzacher changed the title [no-unnecessary-condition] False positive on class field [no-unnecessary-condition] Add support for non-strict mode Oct 2, 2019
@derekparsons718
Copy link

I would love to see this rule work better when strict null checks is disabled.

Until that happens, I would suggest updating the documentation to indicate this limitation. A little note like "this rule requires strict-null-checks" could save people like me from scratching their heads so much :)

Thanks for all of your hard work!

@bradzacher
Copy link
Member

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 30, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

No branches or pull requests

4 participants