Skip to content

Commit ca6921a

Browse files
committed
fix(prettier): don't indent logical values twice after =
Assignment layouts already indent a logical value after = or :. Skip the logical printer's extra right-operand indent in those layouts.
1 parent 7134831 commit ca6921a

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

‎packages/prettier-plugin/src/index.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2803,6 +2803,18 @@ function printTsrxNode(node, path, options, print, args) {
28032803
node.operator,
28042804
[line, path.call((childPath) => print(childPath, { isConditionalTest: true }), 'right')],
28052805
]);
2806+
} else if (
2807+
// `printAssignment` already indents this value after `=` or `:`.
2808+
// Indenting the right operand again would place it one level deeper.
2809+
args?.assignmentLayout === 'break-after-operator' ||
2810+
args?.assignmentLayout === 'chain-tail'
2811+
) {
2812+
logicalResult = group([
2813+
path.call(print, 'left'),
2814+
' ',
2815+
node.operator,
2816+
[line, path.call(print, 'right')],
2817+
]);
28062818
} else {
28072819
logicalResult = group([
28082820
path.call(print, 'left'),

‎packages/prettier-plugin/src/index.test.js‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8899,6 +8899,36 @@ class A {
88998899
expect(result).toBeWithNewline(expected);
89008900
});
89018901

8902+
it('breaks after : or = before a logical value without indenting it twice', async () => {
8903+
const input = `const options = {
8904+
description: someVeryLongVariableNameNumberOne && someVeryLongVariableNameNumberTwoooooooo,
8905+
};
8906+
class A {
8907+
description = someVeryLongVariableNameNumberOne || someVeryLongVariableNameNumberTwoooooooo;
8908+
}
8909+
description = someVeryLongVariableNameNumberOne ?? someVeryLongVariableNameNumberTwoooooooo;
8910+
window.aaaaaaaaaaaaaaaaaa = window.bbbbbbbbbbbbbbbbbbbbbbbb = window.cccccccccccccccccccc = someVeryLongVariableNameNumberOne && someVeryLongVariableNameNumberTwoooooooo;`;
8911+
const expected = `const options = {
8912+
description:
8913+
someVeryLongVariableNameNumberOne &&
8914+
someVeryLongVariableNameNumberTwoooooooo,
8915+
};
8916+
class A {
8917+
description =
8918+
someVeryLongVariableNameNumberOne ||
8919+
someVeryLongVariableNameNumberTwoooooooo;
8920+
}
8921+
description =
8922+
someVeryLongVariableNameNumberOne ?? someVeryLongVariableNameNumberTwoooooooo;
8923+
window.aaaaaaaaaaaaaaaaaa =
8924+
window.bbbbbbbbbbbbbbbbbbbbbbbb =
8925+
window.cccccccccccccccccccc =
8926+
someVeryLongVariableNameNumberOne &&
8927+
someVeryLongVariableNameNumberTwoooooooo;`;
8928+
const result = await format(input);
8929+
expect(result).toBeWithNewline(expected);
8930+
});
8931+
89028932
it('keeps a value that can break by itself on the operator line', async () => {
89038933
const source = `const result = someFunction(
89048934
argumentNumberOne,

0 commit comments

Comments
 (0)