Skip to content

💅 noExtraBooleanCast safe fix drops required parentheses when Boolean(cond ? a : b) is a ternary condition #11289

Description

@nonsugarless

Environment information

Details
CLI:
  Version:                      2.5.6
  Color support:                true

Platform:
  CPU Architecture:             aarch64
  OS:                           macos

Environment:
  BIOME_DISTRIBUTION:                npm
  BIOME_LOG_PATH:                    unset
  BIOME_LOG_PREFIX_NAME:             unset
  BIOME_LOG_LEVEL:                   unset
  BIOME_LOG_KIND:                    unset
  BIOME_CONFIG_PATH:                 unset
  BIOME_THREADS:                     unset
  BIOME_WATCHER_KIND:                unset
  BIOME_WATCHER_POLLING_INTERVAL:    unset
  NO_COLOR:                     unset
  TERM:                         xterm-256color
  JS_RUNTIME_VERSION:           v24.13.0
  JS_RUNTIME_NAME:              node
  NODE_PACKAGE_MANAGER:         pnpm/11.13.0

Biome Configuration:
  Status:                       Loaded successfully.
  Path:                         biome.jsonc
  Formatter enabled:            true
  Linter enabled:               true
  Assist enabled:               true
  VCS enabled:                  true
  HTML full support enabled:    unset

Linter:
  JavaScript enabled:           true
  JSON enabled:                 unset
  CSS enabled:                  unset
  GraphQL enabled:              unset
  Recommended:                  unset
  Enabled rules:
    complexity/noExtraBooleanCast

Workspace:
  Open Documents:               0

Rule name

complexity/noExtraBooleanCast

Playground link

https://biomejs.dev/playground/?lintRules=all&tab=analyzer-fixes&pane=Diagnostics&code=ZABlAGMAbABhAHIAZQAgAGMAbwBuAHMAdAAgAGEAOgAgAHUAbgBrAG4AbwB3AG4AOwAKAGQAZQBjAGwAYQByAGUAIABjAG8AbgBzAHQAIABiADoAIAB1AG4AawBuAG8AdwBuADsACgBkAGUAYwBsAGEAcgBlACAAYwBvAG4AcwB0ACAAYwA6ACAAdQBuAGsAbgBvAHcAbgA7AAoAZQB4AHAAbwByAHQAIABjAG8AbgBzAHQAIAByAGUAcwB1AGwAdAAgAD0AIABCAG8AbwBsAGUAYQBuACgAYQAgAD8AIABiACAAOgAgAGMAKQAgAD8AIAAiAFgAIgAgADoAIAAiAFkAIgA7AAoA

Expected result

The safe fix should preserve the semantics of the original code by wrapping the extracted ternary in parentheses.

Given this input:

declare const a: unknown;
declare const b: unknown;
declare const c: unknown;
export const result = Boolean(a ? b : c) ? "X" : "Y";

Expected fix output:

export const result = (a ? b : c) ? "X" : "Y";

Actual fix output:

export const result = a ? b : c ? "X" : "Y";

This parses as a ? b : (c ? "X" : "Y"), which changes both the behavior and the type of the expression. For example:

const a = true, b = 42, c = 0;
Boolean(a ? b : c) ? "X" : "Y"; // => "X"
a ? b : c ? "X" : "Y";          // => 42

Even though the fix is marked as safe, it changes program behavior.

This looks like a remaining case of #7225, which was fixed by #7244 for operator precedence inside Boolean(...).
However, the case where Boolean(ternary) itself appears as the condition of another ternary still reproduces in 2.5.6 and also on the playground's next version.

Code of Conduct

  • I agree to follow Biome's Code of Conduct

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    S-Needs triageStatus: this issue needs to be triaged

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions