-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathprint-logical-operation.ts
More file actions
37 lines (32 loc) · 1.23 KB
/
print-logical-operation.ts
File metadata and controls
37 lines (32 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { NonterminalKind } from '@nomicfoundation/slang/cst';
import { doc } from 'prettier';
import { isBinaryOperation } from '../slang-utils/is-binary-operation.js';
import { createBinaryOperationPrinter } from './create-binary-operation-printer.js';
import {
binaryGroupRulesBuilder,
shouldNotIndent
} from './print-binary-operation.js';
import type { AstPath, Doc } from 'prettier';
import type { BinaryOperation, StrictAstNode } from '../slang-nodes/types.d.ts';
const { indent } = doc.builders;
const logicalGroupRulesBuilder = binaryGroupRulesBuilder(() => false);
const logicalIndentRulesBuilder =
(node: BinaryOperation, path: AstPath<StrictAstNode>) =>
(document: Doc): Doc => {
for (let i = 1, current = node, parent; ; i++, current = parent) {
parent = path.getNode(i)!;
if (shouldNotIndent(parent, path, i)) break;
if (
parent.kind === NonterminalKind.ConditionalExpression &&
parent.operand === current
)
break;
if (!isBinaryOperation(parent)) return indent(document);
if (current === parent.rightOperand) break;
}
return document;
};
export const printLogicalOperation = createBinaryOperationPrinter(
logicalGroupRulesBuilder,
logicalIndentRulesBuilder
);