-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.go
449 lines (387 loc) · 9.42 KB
/
ast.go
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package typescript
import (
"fmt"
"regexp"
"strconv"
"strings"
"github.com/inngest/event-schemas/events/marshalling"
)
const (
indent = " "
)
var (
_ AstKind = Local{}
_ AstKind = Binding{}
constType = "const"
alphanumeric = regexp.MustCompile(`^\w+$`)
)
func FormatAST(expr ...*Expr) (string, error) {
str := strings.Builder{}
for _, e := range expr {
if _, err := str.WriteString(e.String()); err != nil {
return "", err
}
}
return str.String(), nil
}
// Expr represents a single TS expression, such as a type defintion.
//
// TODO (tonyhb): refactor. We probably dont need Expr wrappers, and it makes
// typescript code gen function signatures a little ugly (see generateStructBinding).
// We can probably work with Local being the single top-level identifier.
type Expr struct {
Data AstKind
}
func (e Expr) String() string {
if _, ok := e.Data.(Lit); ok {
return e.Data.String()
}
// This is code; always add a semicolon after each expression
str := e.Data.String()
if strings.HasSuffix(str, ";") {
return str
}
return str + ";"
}
// ASTKind is a generalization of all elements in our AST
type AstKind interface {
isAST()
String() string
}
func (Lit) isAST() {}
func (Local) isAST() {}
func (Binding) isAST() {}
func (Enum) isAST() {}
func (Scalar) isAST() {}
func (Type) isAST() {}
func (KeyValue) isAST() {}
// Lit represents literal text, such as newlines, comments, spaces, etc.
type Lit struct {
Value string
}
func (l Lit) String() string { return l.Value }
// Scalar represents a scalar value such as a string, number, boolean,
// etc.
type Scalar struct {
Value interface{}
}
func (s Scalar) String() string {
switch t := s.Value.(type) {
case string:
return strconv.Quote(t)
default:
return fmt.Sprintf("%v", s.Value)
}
}
func (s Scalar) Unquoted() string {
return fmt.Sprintf("%v", s.Value)
}
type Type struct {
Value string
}
func (t Type) String() string {
return t.Value
}
// LocalKind represents a local variable or type definition
type LocalKind uint8
const (
LocalConst LocalKind = iota
LocalType
LocalInterface
)
func (l LocalKind) String() string {
switch l {
case LocalConst:
return "const"
case LocalType:
return "type"
case LocalInterface:
return "interface"
}
return ""
}
// Local is a concrete AST expr which represents a definition
// for a variable or type.
type Local struct {
// Kind is the type of definition for the variable.
Kind LocalKind
// Name represents the name for the identifier
Name string
// Type represents the optional type definition for the identifier.
// This is valid for const definitions only.
Type *string
// IsExport defines whether this identifier should be exported.
IsExport bool
// Value is the value that this identifier refers to. This could be
// a scalar, a type, a binding, etc.
Value marshalling.Expr
// AsType records the "as T" suffix for an identifier, eg:
// "const Foo = 1 as int;
AsType *string
}
func (l Local) String() string {
var def string
// TODO: If this is a type or an interface, capitalize the name.
name := strings.ReplaceAll(l.Name, "#", "")
switch l.Kind {
case LocalConst, LocalType:
if l.Type == nil {
def = fmt.Sprintf("%s %s = %s", l.Kind, name, l.Value)
} else {
def = fmt.Sprintf("%s %s: %s = %s", l.Kind, name, *l.Type, l.Value)
}
case LocalInterface:
def = fmt.Sprintf("interface %s %s", name, l.Value)
}
if l.AsType != nil {
def = fmt.Sprintf("%s as %s", def, *l.AsType)
}
if l.IsExport {
return fmt.Sprintf("export %s;", def)
}
return def
}
type BindingKind uint8
const (
// BindingArray represents a plain ol' javascript array with contents:
// [1, 2, 3, 4].
BindingArray BindingKind = iota
// BindingTypedArray represents a TS Array definition: Array<T>. When used
// within a Binding, any members are automatically assumed to be bound using
// a disjunction. However, the Members field can also contain a single
// BindingDisjunction with many values - it does the same thing.
//
// Examples:
//
// Binding{
// Kind: BindingTypedArray,
// Members: []AstKind{
// Type{"string"}, // automatically a disjunction.
// Type{"number"},
// },
// }
//
// is equivalent to:
//
// Binding{
// Kind: BindingTypedArray,
// Members: []AstKind{
// Binding{
// Kind: BindingDisjunction,
// Members: []AstKind{
// Type{"string"},
// Type{"number"},
// },
// },
// },
// }
//
BindingTypedArray
// BindingObject represents a pojo.
BindingObject
// BindingType represents an object used for a type. These use
// semi-colons as their field terminators.
BindingType
// BindingDisjunction represents an ADT enum - values combined
// with " | "
BindingDisjunction
)
// Binding represents a complex type: an array, enum, object, etc.
type Binding struct {
IndentLevel int
Kind BindingKind
Members []marshalling.Expr
}
func (b Binding) String() string {
switch b.Kind {
case BindingArray:
if len(b.Members) == 0 {
return "[]"
}
str := strings.Builder{}
_, _ = str.WriteString("[")
for n, v := range b.Members {
_, _ = str.WriteString(v.String())
if n < len(b.Members)-1 {
_, _ = str.WriteString(", ")
}
}
_, _ = str.WriteString("]")
return str.String()
case BindingTypedArray:
if len(b.Members) == 0 {
return "Array<unknown>"
}
str := strings.Builder{}
_, _ = str.WriteString("Array<")
for n, v := range b.Members {
_, _ = str.WriteString(v.String())
if n < len(b.Members)-1 {
_, _ = str.WriteString(" | ")
}
}
_, _ = str.WriteString(">")
return str.String()
case BindingDisjunction:
if len(b.Members) == 0 {
return ""
}
str := strings.Builder{}
for n, v := range b.Members {
if v == nil {
continue
}
_, _ = str.WriteString(v.String())
if n < len(b.Members)-1 {
_, _ = str.WriteString(" | ")
}
}
return str.String()
case BindingObject, BindingType:
if len(b.Members) == 0 {
return "{}"
}
term := ","
if b.Kind == BindingType {
term = ";"
}
str := strings.Builder{}
_, _ = str.WriteString("{\n")
for _, v := range b.Members {
for i := 0; i <= b.IndentLevel; i++ {
_, _ = str.WriteString(indent)
}
_, _ = str.WriteString(fmt.Sprintf("%s%s\n", v.String(), term))
}
// Add indents to the terminator, eg.
// {
// foo: {
// bar: true,
// } // this indent
// }
for i := 0; i < b.IndentLevel; i++ {
_, _ = str.WriteString(indent)
}
_, _ = str.WriteString("}")
return str.String()
}
return ""
}
// KeyValue represents a key and value within a BindingObject or Enum
type KeyValue struct {
Key string
Value marshalling.Expr
Optional bool
}
func (kv KeyValue) String() string {
key := kv.Key
if !alphanumeric.MatchString(kv.Key) {
key = strconv.Quote(key)
}
if kv.Optional {
return fmt.Sprintf("%s?: %s", key, kv.Value.String())
}
return fmt.Sprintf("%s: %s", key, kv.Value.String())
}
// An Enum is an ADT - a union type within Cue. We special-case enums because
// of typescript limitations. A pure `enum Foo {...}` in typescript isn't that
// fun to use; it's recommended by many to create an Object containing the enum
// values, then use `typeof EnumName[keyof typeof EnumName]` to define the enum.
//
// In effect, this enum is a higher-kind helper for generating Typescript. It
// creates concrete AST for enums depending on the members that it contains.
type Enum struct {
Name string
Simple bool
Members []marshalling.Expr
}
func (e Enum) AST() ([]marshalling.Expr, error) {
// Create a key/value AST mapping for each member of the enum.
//
// This allows us to dedupe values in the enum (eg. cue's int / float types
// are just 'number'; we only want this printed once).
kv := map[string]marshalling.Expr{}
members := []marshalling.Expr{}
for _, m := range e.Members {
str := m.String()
if _, ok := kv[str]; !ok {
kv[str] = m
members = append(members, m)
}
}
if e.Simple {
// This is a simple union, with no local consts.
return []marshalling.Expr{
Binding{
Kind: BindingDisjunction,
Members: members,
},
}, nil
}
scalars := make([]marshalling.Expr, len(members))
for n, m := range members {
switch member := m.(type) {
case Scalar:
scalars[n] = KeyValue{
Key: strings.ToUpper(member.Unquoted()),
Value: member,
}
default:
// Immediately return a disjunction of complex types.
return []marshalling.Expr{
&Expr{
Data: Local{
Kind: LocalType,
Name: e.Name,
IsExport: true,
Value: Binding{
Kind: BindingDisjunction,
// Add all members of the enum as an object.
Members: members,
},
},
},
}, nil
}
}
return []marshalling.Expr{
&Expr{
Data: Local{
Kind: LocalConst,
Name: e.Name,
IsExport: true,
AsType: &constType,
Value: Binding{
Kind: BindingObject,
// Add all members of the enum as an object.
Members: scalars,
},
},
},
&Expr{Data: Lit{Value: "\n"}},
&Expr{
Data: Local{
Kind: LocalType,
Name: e.Name,
IsExport: true,
Value: Type{
Value: fmt.Sprintf("typeof %s[keyof typeof %s]", e.Name, e.Name),
},
},
},
}, nil
}
func (e Enum) String() string {
ast, err := e.AST()
if err != nil {
return err.Error()
}
str := strings.Builder{}
for _, item := range ast {
str.WriteString(item.String())
}
if e.Simple {
return strings.TrimSuffix(str.String(), ";")
}
return str.String()
}