Skip to content

Commit 457de3e

Browse files
authored
no longer needed the indentation of a logical operation if it's the operand of a conditional expression (#1441)
1 parent f37c450 commit 457de3e

5 files changed

Lines changed: 12 additions & 22 deletions

File tree

src/binary-operator-printers/logical.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,11 @@ import { shouldGroupOrIndent } from './utils/should-group-or-indent.js';
66

77
const { indent } = doc.builders;
88

9-
const indentIfNecessaryBuilder = (path, options) => (document) => {
9+
const indentIfNecessaryBuilder = (path) => (document) => {
1010
for (let i = 0, { node } = path; ; i += 1) {
1111
const parentNode = path.getParentNode(i);
1212
if (notIndentParentTypes.includes(parentNode.type)) return document;
13-
if (
14-
options.experimentalTernaries &&
15-
parentNode.type === 'Conditional' &&
16-
parentNode.condition === node
17-
)
13+
if (parentNode.type === 'Conditional' && parentNode.condition === node)
1814
return document;
1915
if (shouldGroupOrIndent(parentNode, [])) return indent(document);
2016
if (node === parentNode.right) return document;

src/binary-operator-printers/printers/create-binary-operation-printer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const createBinaryOperationPrinter =
2222
(groupIfNecessaryBuilder, indentIfNecessaryBuilder) =>
2323
(node, path, print, options) => {
2424
const groupIfNecessary = groupIfNecessaryBuilder(path);
25-
const indentIfNecessary = indentIfNecessaryBuilder(path, options);
25+
const indentIfNecessary = indentIfNecessaryBuilder(path);
2626

2727
return groupIfNecessary([
2828
path.call(print, 'left'),

src/slang-printers/create-binary-operation-printer.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ export const createBinaryOperationPrinter =
4242
path: AstPath<BinaryOperation>
4343
) => (document: Doc) => Doc,
4444
indentRulesBuilder: (
45-
path: AstPath<BinaryOperation>,
46-
options: ParserOptions<AstNode>
45+
path: AstPath<BinaryOperation>
4746
) => (document: Doc) => Doc
4847
) =>
4948
(
@@ -53,7 +52,7 @@ export const createBinaryOperationPrinter =
5352
options: ParserOptions<AstNode>
5453
): Doc => {
5554
const groupRules = groupRulesBuilder(path);
56-
const indentRules = indentRulesBuilder(path, options);
55+
const indentRules = indentRulesBuilder(path);
5756

5857
return groupRules([
5958
path.call(print, 'leftOperand'),

src/slang-printers/print-logical-operation.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,20 @@ import {
77
shouldNotIndent
88
} from './print-binary-operation.js';
99

10-
import type { AstPath, Doc, ParserOptions } from 'prettier';
11-
import type {
12-
AstNode,
13-
BinaryOperation,
14-
StrictAstNode
15-
} from '../slang-nodes/types.d.ts';
10+
import type { AstPath, Doc } from 'prettier';
11+
import type { BinaryOperation, StrictAstNode } from '../slang-nodes/types.d.ts';
1612

1713
const { indent } = doc.builders;
1814

1915
const logicalGroupRulesBuilder = binaryGroupRulesBuilder(() => false);
2016

2117
const logicalIndentRulesBuilder =
22-
(path: AstPath<BinaryOperation>, options: ParserOptions<AstNode>) =>
18+
(path: AstPath<BinaryOperation>) =>
2319
(document: Doc): Doc => {
2420
for (let i = 1, node = path.node; ; i++) {
2521
const parent = path.getNode(i) as StrictAstNode;
2622
if (shouldNotIndent(parent, path, i)) break;
2723
if (
28-
options.experimentalTernaries &&
2924
parent.kind === NonterminalKind.ConditionalExpression &&
3025
parent.operand === node
3126
)

tests/format/BinaryOperators/__snapshots__/format.test.js.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ contract ComparisonOperators {
484484
>= veryVeryVeryVeryVeryLongVariableCalledA;
485485
a +=
486486
veryVeryVeryVeryVeryLongVariableCalledA
487-
|| veryVeryVeryVeryVeryLongVariableCalledB
487+
|| veryVeryVeryVeryVeryLongVariableCalledB
488488
? 1
489489
: 2
490490
) {}
@@ -638,7 +638,7 @@ contract LogicalOperators {
638638
a <= veryVeryVeryVeryVeryLongVariableCalledA;
639639
a +=
640640
veryVeryVeryVeryVeryLongVariableCalledA
641-
|| veryVeryVeryVeryVeryLongVariableCalledB
641+
|| veryVeryVeryVeryVeryLongVariableCalledB
642642
? 1
643643
: 2
644644
) {}
@@ -1167,7 +1167,7 @@ contract ComparisonOperators {
11671167
veryVeryVeryVeryVeryLongVariableCalledA;
11681168
a +=
11691169
veryVeryVeryVeryVeryLongVariableCalledA ||
1170-
veryVeryVeryVeryVeryLongVariableCalledB
1170+
veryVeryVeryVeryVeryLongVariableCalledB
11711171
? 1
11721172
: 2
11731173
) {}
@@ -1321,7 +1321,7 @@ contract LogicalOperators {
13211321
a <= veryVeryVeryVeryVeryLongVariableCalledA;
13221322
a +=
13231323
veryVeryVeryVeryVeryLongVariableCalledA ||
1324-
veryVeryVeryVeryVeryLongVariableCalledB
1324+
veryVeryVeryVeryVeryLongVariableCalledB
13251325
? 1
13261326
: 2
13271327
) {}

0 commit comments

Comments
 (0)