Back to Prettier

18397

changelog_unreleased/javascript/18397.md

3.9.01.4 KB
Original Source

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.
<!-- prettier-ignore -->
jsx
// 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