-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathprint-preserving-empty-lines.ts
More file actions
53 lines (49 loc) · 1.81 KB
/
print-preserving-empty-lines.ts
File metadata and controls
53 lines (49 loc) · 1.81 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { NonterminalKind } from '@nomicfoundation/slang/cst';
import { doc, util } from 'prettier';
import { locEnd } from '../slang-utils/loc.js';
import { printComments } from './print-comments.js';
import { printSeparatedItem } from './print-separated-item.js';
import type { AstPath, Doc, ParserOptions } from 'prettier';
import type { LineCollection, PrintableNode } from '../slang-nodes/types.d.ts';
import type { PrintFunction } from '../types.d.ts';
const { hardline } = doc.builders;
export function printPreservingEmptyLines(
node: LineCollection,
path: AstPath<LineCollection>,
print: PrintFunction,
options: ParserOptions<PrintableNode>
): Doc {
return node.items.length > 0
? path.map(({ node, isFirst, isLast }) => {
return [
// Only attempt to prepend an empty line if `node` is not the first item
!isFirst &&
// YulLabel adds a dedented line so we don't have to prepend a hardline.
node.kind !== NonterminalKind.YulLabel
? hardline
: '',
print(),
// Only attempt to append an empty line if `node` is not the last item
!isLast &&
// Append an empty line if the original text already had an one after the
// current `node`
util.isNextLineEmpty(options.originalText, locEnd(node))
? hardline
: ''
];
}, 'items')
: printComments(node, path, options);
}
export function printIndentedPreservingEmptyLines(
node: LineCollection,
path: AstPath<LineCollection>,
print: PrintFunction,
options: ParserOptions<PrintableNode>
): Doc {
return node.items.length > 0 || (node.comments?.length ?? 0) > 0
? printSeparatedItem(
printPreservingEmptyLines(node, path, print, options),
{ firstSeparator: hardline }
)
: '';
}