Files
test-repo/changelog_unreleased/javascript/18397.md
T
2025-12-06 14:39:00 +08:00

1.4 KiB

Improve logical not expression print (#18397, #18401 by @fisker)

  • Fixed inner logical expression can be double parenthesized in some case.
  • Inline the expression in if/while/do..while condition, to reduce diff when changing condition to negated value.
// Input
if (!(
  // `import("foo")`
  node.type === "ImportExpression" ||
  // `type foo = import("foo")`
  node.type === "TSImportType"
)) {
} else if (
  // `import("foo")`
  node.type === "ImportExpression" ||
  // `type foo = import("foo")`
  node.type === "TSImportType" 
) {
}

// Prettier stable
if (
  !(
    // `import("foo")`
    (
      node.type === "ImportExpression" ||
      // `type foo = import("foo")`
      node.type === "TSImportType"
    )
  )
) {
} else if (
  // `import("foo")`
  node.type === "ImportExpression" ||
  // `type foo = import("foo")`
  node.type === "TSImportType"
) {
}

// Prettier main
if (!(
  // `import("foo")`
  node.type === "ImportExpression" ||
  // `type foo = import("foo")`
  node.type === "TSImportType"
)) {
} else if (
  // `import("foo")`
  node.type === "ImportExpression" ||
  // `type foo = import("foo")`
  node.type === "TSImportType"
) {
}

This code example above is a real world case right here in the Prettier codebase