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

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

Closed
4 tasks done
AndersRobstad opened this issue Nov 5, 2024 · 2 comments Β· Fixed by #10299
Closed
4 tasks done
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@AndersRobstad
Copy link

AndersRobstad commented Nov 5, 2024

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

@AndersRobstad AndersRobstad added bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Nov 5, 2024
@kirkwaiblinger kirkwaiblinger changed the title Bug: [prefer-nullish-coalescing] <short description of the issue> Bug: [prefer-nullish-coalescing] ignoreConditionalTests doesn't ignore the boolean ! operator in a condition Nov 5, 2024
@kirkwaiblinger
Copy link
Member

kirkwaiblinger commented Nov 5, 2024

The detection for this option used to be very buggy, and was recently fixed up, see #10153. We didn't have explicit test cases for this case, so it may not have been an intentional feature, but we should probably add it.

@MathiasWP
Copy link

Should definitely be fixed! πŸ˜„

const string1 = '';
const string2 = 'foo1';

// This...
if(!(string1 || string2)) {

}

// ..cannot be replaced with this
if(!(string1 ?? string2)) {

}

Current implementation gives false positives. The first case evaluates to false while the second one evaluates to true

@kirkwaiblinger kirkwaiblinger added accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for team members to take a look labels Nov 6, 2024
@github-actions github-actions bot added the locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. label Nov 24, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
3 participants