Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Break multiline parenthesized logical expressions #11103

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions changelog_unreleased/javascript/11103.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#### Break multiline parenthesized logical expressions (#11103 by @alex12058)

<!-- prettier-ignore -->
```jsx
// Input
const funnelSnapshotCard =
(report === MY_OVERVIEW && !ReportGK.xar_metrics_active_capitol_v2) ||
(
report === COMPANY_OVERVIEW &&
!ReportGK.xar_metrics_active_capitol_v2_company_metrics
) ? (
<ReportMetricsFunnelSnapshotCard metrics={metrics} />
) : null;

// Prettier stable
const funnelSnapshotCard =
(report === MY_OVERVIEW && !ReportGK.xar_metrics_active_capitol_v2) ||
(report === COMPANY_OVERVIEW &&
!ReportGK.xar_metrics_active_capitol_v2_company_metrics) ? (
<ReportMetricsFunnelSnapshotCard metrics={metrics} />
) : null;

// Prettier main
const funnelSnapshotCard =
(report === MY_OVERVIEW && !ReportGK.xar_metrics_active_capitol_v2) ||
(
report === COMPANY_OVERVIEW &&
!ReportGK.xar_metrics_active_capitol_v2_company_metrics
) ? (
<ReportMetricsFunnelSnapshotCard metrics={metrics} />
) : null;
```
4 changes: 3 additions & 1 deletion src/language-js/print/binaryish.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const {
isObjectProperty,
isEnabledHackPipeline,
} = require("../utils/index.js");
const needsParens = require("../needs-parens.js");

/** @typedef {import("../../document").Doc} Doc */

Expand Down Expand Up @@ -81,7 +82,8 @@ function printBinaryishExpression(path, options, print) {
if (
(isCallExpression(parent) && parent.callee === node) ||
parent.type === "UnaryExpression" ||
(isMemberExpression(parent) && !parent.computed)
(isMemberExpression(parent) && !parent.computed) ||
(parent.type === "LogicalExpression" && needsParens(path, options))
) {
return group([indent([softline, ...parts]), softline]);
}
Expand Down
42 changes: 24 additions & 18 deletions tests/format/js/binary-expressions/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -447,11 +447,13 @@ const user = renderedUser || (

const user2 =
renderedUser ||
(shouldRenderUser && (
<div>
<User name={this.state.user.name} age={this.state.user.age} />
</div>
));
(
shouldRenderUser && (
<div>
<User name={this.state.user.name} age={this.state.user.age} />
</div>
)
);

const avatar = hasAvatar && <Gravatar user={author} size={size} />;

Expand Down Expand Up @@ -578,13 +580,15 @@ prevState = prevState ||

prevState =
prevState ||
(defaultState && {
catalogs: [],
loadState: LOADED,
opened: false,
searchQuery: "",
selectedCatalog: null,
});
(
defaultState && {
catalogs: [],
loadState: LOADED,
opened: false,
searchQuery: "",
selectedCatalog: null,
}
);

prevState = prevState ||
(useDefault && defaultState) || {
Expand All @@ -604,12 +608,14 @@ this.steps = steps || [

this.steps =
steps ||
(checkStep && [
{
name: "mock-module",
path: "/nux/mock-module",
},
]);
(
checkStep && [
{
name: "mock-module",
path: "/nux/mock-module",
},
]
);

this.steps = (steps && checkStep) || [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,65 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`indent_multiline.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
// Indent multiline logical expressions used as function arguments
foo(long_long_long_long_long_long_long_condition_1 &&
long_long_long_long_long_long_long_varaible_2);
foo(long_long_long_long_long_long_long_condition_1 ||
long_long_long_long_long_long_long_varaible_2);
foo(long_long_long_long_long_long_long_condition_1 ??
long_long_long_long_long_long_long_varaible_2);

// Indent multiline logical sub expressions
long_long_long_long_long_long_long_condition_1 ||
(long_long_long_long_long_long_long_condition_2 &&
long_long_long_long_long_long_long_condition_3);
long_long_long_long_long_long_long_condition_1 &&
(long_long_long_long_long_long_long_condition_2 ||
long_long_long_long_long_long_long_condition_3);
long_long_long_long_long_long_long_condition_1 ||
(long_long_long_long_long_long_long_condition_2 ??
long_long_long_long_long_long_long_condition_3);

=====================================output=====================================
// Indent multiline logical expressions used as function arguments
foo(
long_long_long_long_long_long_long_condition_1 &&
long_long_long_long_long_long_long_varaible_2
);
foo(
long_long_long_long_long_long_long_condition_1 ||
long_long_long_long_long_long_long_varaible_2
);
foo(
long_long_long_long_long_long_long_condition_1 ??
long_long_long_long_long_long_long_varaible_2
);

// Indent multiline logical sub expressions
long_long_long_long_long_long_long_condition_1 ||
(
long_long_long_long_long_long_long_condition_2 &&
long_long_long_long_long_long_long_condition_3
);
long_long_long_long_long_long_long_condition_1 &&
(
long_long_long_long_long_long_long_condition_2 ||
long_long_long_long_long_long_long_condition_3
);
long_long_long_long_long_long_long_condition_1 ||
(
long_long_long_long_long_long_long_condition_2 ??
long_long_long_long_long_long_long_condition_3
);

================================================================================
`;

exports[`issue-7024.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
Expand All @@ -13,8 +73,10 @@ const radioSelectedAttr =

=====================================output=====================================
const radioSelectedAttr =
(isAnyValueSelected &&
node.getAttribute(radioAttr.toLowerCase()) === radioValue) ||
(
isAnyValueSelected &&
node.getAttribute(radioAttr.toLowerCase()) === radioValue
) ||
(!isAnyValueSelected && values[a].default === true) ||
a === 0;

Expand Down
18 changes: 18 additions & 0 deletions tests/format/js/logical_expressions/indent_multiline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Indent multiline logical expressions used as function arguments
foo(long_long_long_long_long_long_long_condition_1 &&
long_long_long_long_long_long_long_varaible_2);
foo(long_long_long_long_long_long_long_condition_1 ||
long_long_long_long_long_long_long_varaible_2);
foo(long_long_long_long_long_long_long_condition_1 ??
long_long_long_long_long_long_long_varaible_2);

// Indent multiline logical sub expressions
long_long_long_long_long_long_long_condition_1 ||
(long_long_long_long_long_long_long_condition_2 &&
long_long_long_long_long_long_long_condition_3);
long_long_long_long_long_long_long_condition_1 &&
(long_long_long_long_long_long_long_condition_2 ||
long_long_long_long_long_long_long_condition_3);
long_long_long_long_long_long_long_condition_1 ||
(long_long_long_long_long_long_long_condition_2 ??
long_long_long_long_long_long_long_condition_3);
6 changes: 4 additions & 2 deletions tests/format/js/return/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ function f() {
return (
property.isIdentifier() &&
FUNCTIONS[property.node.name] &&
(object.isIdentifier(JEST_GLOBAL) ||
(callee.isMemberExpression() && shouldHoistExpression(object))) &&
(
object.isIdentifier(JEST_GLOBAL) ||
(callee.isMemberExpression() && shouldHoistExpression(object))
) &&
FUNCTIONS[property.node.name](expr.get("arguments"))
);

Expand Down
24 changes: 16 additions & 8 deletions tests/format/js/ternaries/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ room = room.map((row, rowIndex) => (
=====================================output=====================================
const funnelSnapshotCard =
(report === MY_OVERVIEW && !ReportGK.xar_metrics_active_capitol_v2) ||
(report === COMPANY_OVERVIEW &&
!ReportGK.xar_metrics_active_capitol_v2_company_metrics) ? (
(
report === COMPANY_OVERVIEW &&
!ReportGK.xar_metrics_active_capitol_v2_company_metrics
) ? (
<ReportMetricsFunnelSnapshotCard metrics={metrics} />
) : null;

Expand Down Expand Up @@ -66,8 +68,10 @@ room = room.map((row, rowIndex) => (
=====================================output=====================================
const funnelSnapshotCard =
(report === MY_OVERVIEW && !ReportGK.xar_metrics_active_capitol_v2) ||
(report === COMPANY_OVERVIEW &&
!ReportGK.xar_metrics_active_capitol_v2_company_metrics) ? (
(
report === COMPANY_OVERVIEW &&
!ReportGK.xar_metrics_active_capitol_v2_company_metrics
) ? (
<ReportMetricsFunnelSnapshotCard metrics={metrics} />
) : null;

Expand Down Expand Up @@ -108,8 +112,10 @@ room = room.map((row, rowIndex) => (
=====================================output=====================================
const funnelSnapshotCard =
(report === MY_OVERVIEW && !ReportGK.xar_metrics_active_capitol_v2) ||
(report === COMPANY_OVERVIEW &&
!ReportGK.xar_metrics_active_capitol_v2_company_metrics) ? (
(
report === COMPANY_OVERVIEW &&
!ReportGK.xar_metrics_active_capitol_v2_company_metrics
) ? (
<ReportMetricsFunnelSnapshotCard metrics={metrics} />
) : null;

Expand Down Expand Up @@ -149,8 +155,10 @@ room = room.map((row, rowIndex) => (
=====================================output=====================================
const funnelSnapshotCard =
(report === MY_OVERVIEW && !ReportGK.xar_metrics_active_capitol_v2) ||
(report === COMPANY_OVERVIEW &&
!ReportGK.xar_metrics_active_capitol_v2_company_metrics) ? (
(
report === COMPANY_OVERVIEW &&
!ReportGK.xar_metrics_active_capitol_v2_company_metrics
) ? (
<ReportMetricsFunnelSnapshotCard metrics={metrics} />
) : null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ function f() {
throw (
property.isIdentifier() &&
FUNCTIONS[property.node.name] &&
(object.isIdentifier(JEST_GLOBAL) ||
(callee.isMemberExpression() && shouldHoistExpression(object))) &&
(
object.isIdentifier(JEST_GLOBAL) ||
(callee.isMemberExpression() && shouldHoistExpression(object))
) &&
FUNCTIONS[property.node.name](expr.get("arguments"))
);

Expand Down
6 changes: 4 additions & 2 deletions tests/format/typescript/as/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,10 @@ angular.module("foo").directive("formIsolator", () => {
this.initialValues =
undefined;
const extraRendererAttrs = ((attrs.rendererAttrs &&
this.utils.safeParseJsonString(attrs.rendererAttrs)) ||
const extraRendererAttrs = ((
attrs.rendererAttrs &&
this.utils.safeParseJsonString(attrs.rendererAttrs)
) ||
Object.create(null)) as FieldService.RendererAttributes;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is so ugly, but out scope this PR

const extraRendererAttrs = (
    (
      attrs.rendererAttrs &&
      this.utils.safeParseJsonString(attrs.rendererAttrs)
    ) ||
    Object.create(null)
  ) as FieldService.RendererAttributes;

should be expected.

const annotate = (angular.injector as any).$$annotate as (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,10 @@ namespace TypeScript.WebTsc {
// [0xFF,0xFE] and [0xFE,0xFF] mean utf-16 (little or big endian), otherwise default to utf-8
fileStream.Charset =
bom.length >= 2 &&
((bom.charCodeAt(0) === 0xff && bom.charCodeAt(1) === 0xfe) ||
(bom.charCodeAt(0) === 0xfe && bom.charCodeAt(1) === 0xff))
(
(bom.charCodeAt(0) === 0xff && bom.charCodeAt(1) === 0xfe) ||
(bom.charCodeAt(0) === 0xfe && bom.charCodeAt(1) === 0xff)
)
? "unicode"
: "utf-8";
}
Expand Down