-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathprint-function.ts
More file actions
44 lines (39 loc) · 1.12 KB
/
print-function.ts
File metadata and controls
44 lines (39 loc) · 1.12 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
import { NonterminalKind } from '@nomicfoundation/slang/cst';
import { doc } from 'prettier';
import { joinExisting } from '../slang-utils/join-existing.js';
import type { AstPath, Doc } from 'prettier';
import type { FunctionLike, FunctionWithBody } from '../slang-nodes/types.d.ts';
import type { PrintFunction } from '../types.d.ts';
const { dedent, group, indent, line } = doc.builders;
export function printFunction(
functionName: Doc,
node: FunctionLike,
path: AstPath<FunctionLike>,
print: PrintFunction
): Doc {
const body = (node as FunctionWithBody).body;
return group([
functionName,
path.call(print, 'parameters'),
indent(
group([
joinExisting(line, [
path.call(print, 'attributes'),
path.call(print, 'returns')
]),
body && body.kind === NonterminalKind.Block ? dedent(line) : ''
])
)
]);
}
export function printFunctionWithBody(
functionName: Doc,
node: FunctionLike,
path: AstPath<FunctionWithBody>,
print: PrintFunction
): Doc {
return [
printFunction(functionName, node, path, print),
path.call(print, 'body')
];
}