-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypescript.cc
More file actions
328 lines (280 loc) · 10.9 KB
/
typescript.cc
File metadata and controls
328 lines (280 loc) · 10.9 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include <sourcemeta/codegen/generator.h>
#include <algorithm> // std::ranges::any_of
#include <iomanip> // std::hex, std::setfill, std::setw
#include <sstream> // std::ostringstream
namespace {
// TODO: Move to Core
auto escape_string(const std::string &input) -> std::string {
std::ostringstream result;
for (const auto character : input) {
switch (character) {
case '\\':
result << "\\\\";
break;
case '"':
result << "\\\"";
break;
case '\b':
result << "\\b";
break;
case '\f':
result << "\\f";
break;
case '\n':
result << "\\n";
break;
case '\r':
result << "\\r";
break;
case '\t':
result << "\\t";
break;
default:
// Escape other control characters (< 0x20) using \uXXXX format
if (static_cast<unsigned char>(character) < 0x20) {
result << "\\u" << std::hex << std::setfill('0') << std::setw(4)
<< static_cast<int>(static_cast<unsigned char>(character));
} else {
result << character;
}
break;
}
}
return result.str();
}
} // namespace
namespace sourcemeta::codegen {
TypeScript::TypeScript(std::ostream &stream, const std::string_view type_prefix)
: output{stream}, prefix{type_prefix} {}
auto TypeScript::operator()(const IRScalar &entry) -> void {
this->output << "export type "
<< mangle(this->prefix, entry.pointer, entry.symbol, this->cache)
<< " = ";
switch (entry.value) {
case IRScalarType::String:
this->output << "string";
break;
case IRScalarType::Number:
case IRScalarType::Integer:
this->output << "number";
break;
case IRScalarType::Boolean:
this->output << "boolean";
break;
case IRScalarType::Null:
this->output << "null";
break;
}
this->output << ";\n";
}
auto TypeScript::operator()(const IREnumeration &entry) -> void {
this->output << "export type "
<< mangle(this->prefix, entry.pointer, entry.symbol, this->cache)
<< " = ";
const char *separator{""};
for (const auto &value : entry.values) {
this->output << separator;
sourcemeta::core::prettify(value, this->output);
separator = " | ";
}
this->output << ";\n";
}
auto TypeScript::operator()(const IRObject &entry) -> void {
const auto type_name{
mangle(this->prefix, entry.pointer, entry.symbol, this->cache)};
const auto has_typed_additional{
std::holds_alternative<IRType>(entry.additional)};
const auto allows_any_additional{
std::holds_alternative<bool>(entry.additional) &&
std::get<bool>(entry.additional)};
if (has_typed_additional && entry.members.empty() && entry.pattern.empty()) {
const auto &additional_type{std::get<IRType>(entry.additional)};
this->output << "export type " << type_name << " = Record<string, "
<< mangle(this->prefix, additional_type.pointer,
additional_type.symbol, this->cache)
<< ">;\n";
return;
}
if (allows_any_additional && entry.members.empty() && entry.pattern.empty()) {
this->output << "export type " << type_name
<< " = Record<string, unknown>;\n";
return;
}
this->output << "export interface " << type_name << " {\n";
// We always quote property names for safety. JSON Schema allows any string
// as a property name, but unquoted TypeScript/ECMAScript property names
// must be valid IdentifierName productions (see ECMA-262 section 12.7).
// Quoting allows any string to be used as a property name.
// See: https://tc39.es/ecma262/#sec-names-and-keywords
// See: https://mathiasbynens.be/notes/javascript-properties
for (const auto &[member_name, member_value] : entry.members) {
const auto optional_marker{member_value.required ? "" : "?"};
const auto readonly_marker{member_value.immutable ? "readonly " : ""};
this->output << " " << readonly_marker << "\""
<< escape_string(member_name) << "\"" << optional_marker
<< ": "
<< mangle(this->prefix, member_value.pointer,
member_value.symbol, this->cache)
<< ";\n";
}
for (const auto &pattern_property : entry.pattern) {
if (!pattern_property.prefix.has_value()) {
continue;
}
this->output << " [key: `" << pattern_property.prefix.value()
<< "${string}`]: "
<< mangle(this->prefix, pattern_property.pointer,
pattern_property.symbol, this->cache);
// TypeScript requires that a more specific index signature type is
// assignable to any less specific one that overlaps it. When a prefix
// is a sub-prefix of another (i.e. "x-data-" starts with "x-"),
// intersect the types so the constraint is satisfied
for (const auto &other : entry.pattern) {
if (&other == &pattern_property || !other.prefix.has_value()) {
continue;
}
if (pattern_property.prefix.value().starts_with(other.prefix.value())) {
this->output << " & "
<< mangle(this->prefix, other.pointer, other.symbol,
this->cache);
}
}
this->output << ";\n";
}
const auto has_non_prefix_pattern{
std::ranges::any_of(entry.pattern, [](const auto &pattern_property) {
return !pattern_property.prefix.has_value();
})};
if (allows_any_additional) {
this->output << " [key: string]: unknown | undefined;\n";
} else if (has_typed_additional || has_non_prefix_pattern) {
// TypeScript index signatures must be a supertype of all property value
// types. We use a union of all member types plus the additional properties
// type plus undefined (for optional properties).
this->output << " [key: string]:\n";
this->output << " // As a notable limitation, TypeScript requires index "
"signatures\n";
this->output << " // to also include the types of all of its "
"properties, so we must\n";
this->output << " // match a superset of what JSON Schema allows\n";
for (const auto &[member_name, member_value] : entry.members) {
this->output << " "
<< mangle(this->prefix, member_value.pointer,
member_value.symbol, this->cache)
<< " |\n";
}
for (const auto &pattern_property : entry.pattern) {
this->output << " "
<< mangle(this->prefix, pattern_property.pointer,
pattern_property.symbol, this->cache)
<< " |\n";
}
if (has_typed_additional) {
const auto &additional_type{std::get<IRType>(entry.additional)};
this->output << " "
<< mangle(this->prefix, additional_type.pointer,
additional_type.symbol, this->cache)
<< " |\n";
}
this->output << " undefined;\n";
}
this->output << "}\n";
}
auto TypeScript::operator()(const IRImpossible &entry) -> void {
this->output << "export type "
<< mangle(this->prefix, entry.pointer, entry.symbol, this->cache)
<< " = never;\n";
}
auto TypeScript::operator()(const IRAny &entry) -> void {
this->output << "export type "
<< mangle(this->prefix, entry.pointer, entry.symbol, this->cache)
<< " = unknown;\n";
}
auto TypeScript::operator()(const IRArray &entry) -> void {
this->output << "export type "
<< mangle(this->prefix, entry.pointer, entry.symbol, this->cache)
<< " = ";
if (entry.items.has_value()) {
this->output << mangle(this->prefix, entry.items->pointer,
entry.items->symbol, this->cache)
<< "[]";
} else {
this->output << "unknown[]";
}
this->output << ";\n";
}
auto TypeScript::operator()(const IRReference &entry) -> void {
this->output << "export type "
<< mangle(this->prefix, entry.pointer, entry.symbol, this->cache)
<< " = "
<< mangle(this->prefix, entry.target.pointer,
entry.target.symbol, this->cache)
<< ";\n";
}
auto TypeScript::operator()(const IRTuple &entry) -> void {
this->output << "export type "
<< mangle(this->prefix, entry.pointer, entry.symbol, this->cache)
<< " = [";
const char *separator{""};
for (const auto &item : entry.items) {
this->output << separator
<< mangle(this->prefix, item.pointer, item.symbol,
this->cache);
separator = ", ";
}
if (entry.additional.has_value()) {
this->output << separator << "..."
<< mangle(this->prefix, entry.additional->pointer,
entry.additional->symbol, this->cache)
<< "[]";
}
this->output << "];\n";
}
auto TypeScript::operator()(const IRUnion &entry) -> void {
this->output << "export type "
<< mangle(this->prefix, entry.pointer, entry.symbol, this->cache)
<< " =\n";
const char *separator{""};
for (const auto &value : entry.values) {
this->output << separator << " "
<< mangle(this->prefix, value.pointer, value.symbol,
this->cache);
separator = " |\n";
}
this->output << ";\n";
}
auto TypeScript::operator()(const IRIntersection &entry) -> void {
this->output << "export type "
<< mangle(this->prefix, entry.pointer, entry.symbol, this->cache)
<< " =\n";
const char *separator{""};
for (const auto &value : entry.values) {
this->output << separator << " "
<< mangle(this->prefix, value.pointer, value.symbol,
this->cache);
separator = " &\n";
}
this->output << ";\n";
}
auto TypeScript::operator()(const IRConditional &entry) -> void {
// As a notable limitation, TypeScript cannot express the negation of an
// if/then/else condition, so the else branch is wider than what JSON
// Schema allows
this->output << "// (if & then) | else approximation: the else branch is "
"wider than what\n";
this->output << "// JSON Schema allows, as TypeScript cannot express type "
"negation\n";
this->output << "export type "
<< mangle(this->prefix, entry.pointer, entry.symbol, this->cache)
<< " =\n ("
<< mangle(this->prefix, entry.condition.pointer,
entry.condition.symbol, this->cache)
<< " & "
<< mangle(this->prefix, entry.consequent.pointer,
entry.consequent.symbol, this->cache)
<< ") | "
<< mangle(this->prefix, entry.alternative.pointer,
entry.alternative.symbol, this->cache)
<< ";\n";
}
} // namespace sourcemeta::codegen