Skip to content

Bug: @typescript-eslint/strict-boolean-expressions breaks code on --fix #7312

Closed
@fasenderos

Description

@fasenderos

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.

Issue Description

Running eslint --fix breaks code when the rule @typescript-eslint/strict-boolean-expressions has the option allowNullableObject set to false

Code before fix

type Items = {
    foo: string
}
const items: Items[] = [
    {foo: 'bar'},
    {foo: 'bar'},
]
function someFn (items?: Items[]) {
    return JSON.parse(
        JSON.stringify(items || []),
    );
}
const test = someFn(items)
console.log(test); // [ { foo: 'bar' }, { foo: 'bar' } ]

code after fix

function someFn (items?: Items[]) {
    return JSON.parse(
        JSON.stringify((items != null) || []),
    );
}
const test = someFn(items)
console.log(test); // true

What did you expect to happen?
Code gets changed with the ?? operator or not changed at all

Reproduction Repository Link

https://github.com/fasenderos/typescript-eslint-strict-boolean

Repro Steps

Clone the repo and run npm install, then try the code before fix

  • npm run build
  • npm start

The output will be [ { foo: 'bar' }, { foo: 'bar' } ]

Now run the fix and try again

  • npm run lint:fix
  • npm run build
  • npm start

The output will be true

Versions

package version
@typescript-eslint/eslint-plugin ^6.2.0
@typescript-eslint/parser ^6.2.0
TypeScript ^5.1.6
ESLint ^8.45.0
node 18.16.1

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