-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathlogical.js
More file actions
27 lines (24 loc) · 1.05 KB
/
logical.js
File metadata and controls
27 lines (24 loc) · 1.05 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
import { doc } from 'prettier';
import { createBinaryOperationPrinter } from './printers/create-binary-operation-printer.js';
import { createGroupIfNecessaryBuilder } from './printers/create-group-if-necessary-builder.js';
import { notIndentParentTypes } from './printers/create-indent-if-necessary-builder.js';
import { shouldGroupOrIndent } from './utils/should-group-or-indent.js';
const { indent } = doc.builders;
const indentIfNecessaryBuilder = (path) => (document) => {
for (let i = 0, { node } = path; ; i += 1) {
const parentNode = path.getParentNode(i);
if (notIndentParentTypes.includes(parentNode.type)) return document;
if (parentNode.type === 'Conditional' && parentNode.condition === node)
return document;
if (shouldGroupOrIndent(parentNode, [])) return indent(document);
if (node === parentNode.right) return document;
node = parentNode;
}
};
export const logical = {
match: (op) => ['&&', '||'].includes(op),
print: createBinaryOperationPrinter(
createGroupIfNecessaryBuilder([]),
indentIfNecessaryBuilder
)
};