-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathlogical.js
More file actions
32 lines (29 loc) · 1.13 KB
/
logical.js
File metadata and controls
32 lines (29 loc) · 1.13 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
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, options) => (document) => {
let node = path.getNode();
for (let i = 0; ; i += 1) {
const parentNode = path.getParentNode(i);
if (notIndentParentTypes.includes(parentNode.type)) return document;
if (
options.experimentalTernaries &&
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
)
};