/
Writers.ts
133 lines (115 loc) · 4.71 KB
/
Writers.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* barrel:ignore */
import { CodeBlockWriter } from "../codeBlockWriter";
import * as errors from "../errors";
import { TypeElementMemberedNodeStructure } from "../structures";
import { StructurePrinterFactory } from "../factories/StructurePrinterFactory";
import { WriterFunction } from "../types";
export type WriterFunctionOrValue = string | number | WriterFunction;
const structurePrinterFactory = new StructurePrinterFactory(() => {
throw new errors.NotImplementedError("Not implemented scenario for getting code format settings when using a writer function. Please open an issue.");
});
/**
* Functions for writing code.
*/
export class Writers {
private constructor() {
}
/**
* Gets a writer function for writing the provided object as an object literal expression.
* @param obj - Object to write.
*/
static object(obj: { [key: string]: WriterFunctionOrValue | undefined; }): WriterFunction {
return (writer: CodeBlockWriter) => {
const keyNames = Object.keys(obj);
writer.write("{");
if (keyNames.length > 0) {
writer.indent(() => {
writeObject();
});
}
writer.write("}");
function writeObject() {
for (let i = 0; i < keyNames.length; i++) {
if (i > 0)
writer.write(",").newLine();
const keyName = keyNames[i];
const value = obj[keyName];
writer.write(keyName);
if (value != null) {
writer.write(": ");
writeValue(writer, value);
}
}
writer.newLine();
}
};
}
/** Gets a writer function for writing an object type. */
static objectType(structure: TypeElementMemberedNodeStructure): WriterFunction {
return (writer: CodeBlockWriter) => {
writer.write("{");
if (anyPropertyHasValue(structure)) {
writer.indent(() => {
structurePrinterFactory.forTypeElementMemberedNode().printText(writer, structure);
});
}
writer.write("}");
};
}
/** Gets a writer function for writing a union type (ex. `FirstType | SecondType`). */
static unionType(firstType: WriterFunctionOrValue, secondType: WriterFunctionOrValue, ...additionalTypes: WriterFunctionOrValue[]) {
return getWriteFunctionForUnionOrIntersectionType("|", [firstType, secondType, ...additionalTypes]);
}
/** Gets a writer function for writing an intersection type (ex. `FirstType & SecondType`). */
static intersectionType(firstType: WriterFunctionOrValue, secondType: WriterFunctionOrValue, ...additionalTypes: WriterFunctionOrValue[]) {
return getWriteFunctionForUnionOrIntersectionType("&", [firstType, secondType, ...additionalTypes]);
}
/** Gets a writer function for writing a type assertion (ex. `type as assertionType`). */
static assertion(type: WriterFunctionOrValue, assertionType: WriterFunctionOrValue) {
return (writer: CodeBlockWriter) => {
writeValue(writer, type);
writer.spaceIfLastNot().write("as ");
writeValue(writer, assertionType);
};
}
/**
* Gets a writer function for writing a return statement returning the provided value (ex. `return value;`).
* @param value - Value to be returned.
*/
static returnStatement(value: WriterFunctionOrValue): WriterFunction {
return (writer: CodeBlockWriter) => {
writer.write("return ");
writer.hangingIndentUnlessBlock(() => {
writeValue(writer, value);
writer.write(";");
});
};
}
}
function getWriteFunctionForUnionOrIntersectionType(separator: "|" | "&", args: WriterFunctionOrValue[]) {
return (writer: CodeBlockWriter) => {
writeSeparatedByString(writer, ` ${separator} `, args);
};
}
function anyPropertyHasValue(obj: any) {
for (const key of Object.keys(obj)) {
if (obj[key] == null)
continue;
if (obj[key] instanceof Array && obj[key].length === 0)
continue;
return true;
}
return false;
}
function writeSeparatedByString(writer: CodeBlockWriter, separator: string, values: WriterFunctionOrValue[]) {
for (let i = 0; i < values.length; i++) {
writer.conditionalWrite(i > 0, separator);
writeValue(writer, values[i]);
}
}
function writeValue(writer: CodeBlockWriter, value: WriterFunctionOrValue) {
if (value instanceof Function)
value(writer);
else
writer.write(value.toString());
}