Skip to content

Bug: [strict-boolean-expressions] Only the first operand in a chain of && is checked #5518

Closed
@phaux

Description

@phaux

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=4.7.4&sourceType=module&code=PQKgBApgzgNglgOwC5gAJIJ4AdoGMBOcWSAtNPMsFEobqQEYD2jMEAhgmQB5b7RRxGCKAC4wAbQj58jfABowAbzBsYMRgHcAcgFcAtvSliAZqqgQwAXwC6YEMABQD4MDABGSNNkLGAawVwxmA65gAmYIhgAFIAygAaYLiMOjDhfAihUmAADA7ZYABkBUqWANxOLu6eMvhg9DooUAAWyal1FgBMCoa4bCEWxnD41GAdYIw4+ByhUCp8YABESRlwSILCCw4eRaOFxYplFa4eUjV1DWDNreGGYADMW3u7O3dPB+XOrmOnsrP1jS0UjcLAAWBwACg8AB8oaMAJRPcGvGFgEEInbvI5VH7yca+AD8ESQswgAEcdHAAG6qCDIMBIRgqelSBBsfAYR4YyxgFEIFIwD6Vf70ppwWZihAAchQ03GCBgGGxXlqwquQPao05xXyvP5gq+1V+PgJkHJVJpdIZESCEBg5mtkDtEAh232lgRKPBYx24msHthfLU+suazUuxxfwuaratzBkKe2X9YC9CaTgYFTiAA&eslintrc=N4KABGBEBOCuA2BTAzpAXGYBfEWg&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA

Repro Code

/* eslint @typescript-eslint/strict-boolean-expressions: [error, { allowNumber: false }] */

// 1 error, ok, if used in JSX could render 0
0 && {};

// 1 error but should be 2, because first 2 operands are "conditions"
1 && 2 && {};

// 1 error but should be 3
1 && 2 && 3 && {};

// 2 errors but should be 4
(1 || 2) && (3 || 4) && {};

// 1 error, ok? its equivalent to a ternary
1 && {} || null;

// but this isn't and only 1 error but should be 2
1 && 0 || null;

// 2 errors, ok? equivalent to if else if else
(1 && {}) || (2 && []) || null;

// still 2 errors but should be 4
(1 && 0) || (2 && 0) || null;

ESLint Config

module.exports = {
  parser: "@typescript-eslint/parser",
  rules: {
    "@typescript-eslint/strict-boolean-expressions": ["error", { "allowNumber" : false }],
  },
};

tsconfig

{
  "compilerOptions": {
    "strict": true
  }
}

Expected Result

I'm not even sure what the correct behavior should be here.
I think when given a chain of && or ||, all the operands should be checked except the last one.
If somebody used cond && valIfTrue || valIfFalse instead of ternary it would be a breaking change for them.

Actual Result

It checks only the first operand in a chain.
It was implemented that way to allow cond && doIfTrue() || doIfFalse() but its flawed and shouldn't be used after all.

Additional Info

I can send a PR

Versions

package version
@typescript-eslint/eslint-plugin 5.20.0
@typescript-eslint/parser 5.20.0
TypeScript 4.6.3
ESLint 8.11.0
node 16.14.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issuebugSomething isn't workingpackage: 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