-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunparse.ts
39 lines (35 loc) · 1.19 KB
/
unparse.ts
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
/**
* Helper functions for pretty printing the source code based on the AST
*/
import { AstNode } from './definitions/AstNode.js';
import { token } from './definitions/TokenNode.js';
import { indentation } from '../unparseParseTree/index.js';
import { RA } from '../utils/types.js';
export type PrintContext = {
readonly indent: number;
readonly mode: 'nameAnalysis' | 'pretty';
readonly debug: boolean;
// Whether expression needs to be wrapped in parentheses just to be safe
readonly needWrapping: boolean;
};
export function indent(printContext: PrintContext, node: AstNode): string {
const newContext = { ...printContext, indent: printContext.indent + 1 };
const content = node.print(newContext);
return [
token('LCURLY'),
'\n',
content,
content.length === 0 ? '' : '\n',
indentation.repeat(printContext.indent),
token('RCURLY'),
].join('');
}
export const wrapChild = (printContext: PrintContext, node: AstNode) =>
node.print({ ...printContext, needWrapping: true });
export const wrap = (
printContext: PrintContext,
output: RA<string>
): RA<string> | string =>
printContext.needWrapping
? [token('LPAREN'), ...output, token('RPAREN')]
: output;