Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Bug: [prefer-nullish-coalescing] ignoreConditionalTests doesn't ignore the boolean ! operator in a condition #10284

Closed
@AndersRobstad

Description

@AndersRobstad

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Playground Link

https://typescript-eslint.io/play/#ts=5.6.3&fileType=.ts&code=DYUwLgBAZg9jBcEDOYBOBLAdgcwgHwgFdMATEKLEEiAXggHJYZ6BuAKFEgCMBDVRFBhz4ipcpWp16vVKzZsQADwAOMVJCjEAxmHQxMEMCBQBhHkhAAKAJQQA3mwCQ6KBEtN8BGbYePHW-SQYUAA6YBhsS0Y4CDUIGXprdkcAXzY0hRU1DW1dfUNjMDMLACYbeycXNwBCdxi8Lz5rHyd-QOCQMIiozBgNGLiEpKc0jI86YjIKTCp2GVpRKYl2IA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Y6RAM0WlqYSNkAC1pkA9gEMkyMswDm6MAG1IvaOOiQAuuDABfEPqA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkAhqqrAO4BSZlemArugDTgaMABbpoAa3YUwXXgJpR0JALKwAJtwwBJIniyxE02f0G0AZrEzR0AYWIlkJPbpt0HRAOY6AYmnQA5OnglIx4TBUhMJVhUADd0dmJVDQxQuVMoEnFkRAAZZAAjG1EJNPChElhuK3RlOkNOMPkKrmRoPDLm2iJYAFUiaBLxdDUdNXQADxGAQWhrEg4ZJozIHq0kVDbkPAAldDxq0k6Vnu8GVDxhTCqPYVcSJR0AZWZtweOInv7uB7Vc2GgDEWxi6UC%2BRB%2BIwACnRMEF9lhgctPrB1ohNtBtth4phMMhxh8hPB1Jp0HtKqhuARiNJIAVuEQ1BhMJBQZAcQU6AR4MlSU8AJ66OgTaRmIHoMCCAC%2BIClQA&tokens=false

Repro Code

let foo: string | undefined = 'foo';
let bar: string | undefined = 'bar';

export function testCase() {
	if (foo || bar) {
		console.log('foo or bar');
	}
}

export function testCase2() {
	if (!(foo || bar)) {
		console.log('not foo or bar');
	}
}

foo = undefined;
bar = undefined;

ESLint Config

module.exports = {
  "rules": {
    "@typescript-eslint/prefer-nullish-coalescing": ["error"]
  }
}

tsconfig

{
  "compilerOptions": {
    "allowJs": true,
    "checkJs": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "sourceMap": true,
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitOverride": true,
    "moduleResolution": "bundler",
    "verbatimModuleSyntax": false 
  }
}

Expected Result

I would have expected that the if-statement inside testCase2 also is allowed, but after updating from 8.12.2 to 8.13.0 it results in an error suggesting to use ?? instead referring to the prefer-nullish-coalescing rule.

I think it should to be allowed to do this check to see whether one of two strings are truthy inside an if-statement as shown in testCase2.

Actual Result

In the example provided testCase2 will not run the console.log statement when foo="" and bar="bar", however, the testCase1 runs the if-statement in the same case. This is due to the difference between || and ?? when it comes to the empty string.

Meaning the rule now suggests something that is logically not the same, and would change the behavior of code similar to the one shown in the example

Additional Info

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issuebugSomething isn't workinglocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.package: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions