Repository navigation
Expand file tree
/
Copy pathcodegen_format.go
More file actions
253 lines (204 loc) · 6.12 KB
/
Copy pathcodegen_format.go
File metadata and controls
253 lines (204 loc) · 6.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
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
package vukem
import (
"regexp"
"slices"
"strings"
)
// The generator writes TypeScript that is already what Biome formats (the
// config vukem new writes: 120 columns, 2 spaces, double quotes), so a file it
// wrote stays stable under `biome format` and "codegen -check" does not flip
// after a format run. The generator emits one declaration per line, so the
// only lines that can pass the width are a type alias or a property; these
// functions break them the way Biome 2 does:
//
// - a union that does not fit goes one "| member" per line, after the "=" of
// an alias or the ":" of a property, except "Name | null" which stays whole;
// - an object alias whose "export type X = {" line is too long moves the
// object under the "=", indented;
// - any other alias that is too long breaks after the "=";
// - "Record<K, V>" that does not fit puts its arguments one per line.
//
// A single name longer than the width cannot be broken, and is left as it is.
// wrapLongUnions applies those rules to src for the given line width.
func wrapLongUnions(src string, width int) string {
lines := strings.Split(src, "\n")
lines = indentLongObjectAliases(lines, width)
out := make([]string, 0, len(lines))
for _, line := range lines {
out = append(out, wrapLine(line, width)...)
}
return strings.Join(out, "\n")
}
var objectAliasRe = regexp.MustCompile(`^export type \S+ = \{$`)
// indentLongObjectAliases moves the body of "export type X = {" under the "="
// when that first line is wider than width:
//
// export type X =
// {
// a: string;
// };
func indentLongObjectAliases(lines []string, width int) []string {
out := make([]string, 0, len(lines))
for i := 0; i < len(lines); i++ {
line := lines[i]
if !objectAliasRe.MatchString(line) || len([]rune(line)) <= width {
out = append(out, line)
continue
}
end := slices.Index(lines[i:], "};")
if end < 0 {
out = append(out, line)
continue
}
out = append(out, strings.TrimSuffix(line, " {"), " {")
for _, body := range lines[i+1 : i+end] {
if body == "" {
out = append(out, body)
} else {
out = append(out, " "+body)
}
}
out = append(out, " };")
i += end
}
return out
}
// simpleNameRe is a type reference or a keyword type: what Biome keeps next to
// "| null" on one line.
var simpleNameRe = regexp.MustCompile(`^[A-Za-z_$][A-Za-z0-9_$]*$`)
func wrapLine(line string, width int) []string {
if len([]rune(line)) <= width || !strings.HasSuffix(line, ";") {
return []string{line}
}
trimmed := strings.TrimLeft(line, " ")
indent := line[:len(line)-len(trimmed)]
var head, value string
alias := strings.HasPrefix(trimmed, "export type ")
switch {
case alias:
i := strings.Index(trimmed, " = ")
if i < 0 {
return []string{line}
}
head, value = trimmed[:i+2], trimmed[i+3:]
default:
i := strings.Index(trimmed, ": ")
if i < 0 {
return []string{line}
}
head, value = trimmed[:i+1], trimmed[i+2:]
}
value = strings.TrimSuffix(value, ";")
if members := splitTopLevelUnion(value); len(members) >= 2 {
// Biome keeps "Name | null" whole, whatever its width.
if len(members) == 2 && slices.Contains(members, "null") &&
simpleNameRe.MatchString(otherThan(members, "null")) {
return []string{line}
}
out := []string{indent + head}
for _, m := range members {
out = append(out, indent+" | "+m)
}
out[len(out)-1] += ";"
return out
}
if alias {
// Only the part after the "=" can still be broken: it moves to its own line.
return append([]string{indent + head}, wrapGeneric(indent+" ", "", value+";", width)...)
}
return wrapGeneric(indent, head+" ", value+";", width)
}
func otherThan(members []string, not string) string {
for _, m := range members {
if m != not {
return m
}
}
return ""
}
// wrapGeneric writes prefix+value (value ends in ";" or ","), breaking a
// "Name<A, B>" that does not fit into one argument per line, recursively.
func wrapGeneric(indent, prefix, value string, width int) []string {
line := indent + prefix + value
if len([]rune(line)) <= width {
return []string{line}
}
end := value[len(value)-1:]
body := strings.TrimSuffix(value, end)
open := strings.Index(body, "<")
if open <= 0 || !strings.HasSuffix(body, ">") {
return []string{line}
}
args := splitTopLevel(body[open+1:len(body)-1], ", ")
if len(args) < 2 {
return []string{line}
}
out := []string{indent + prefix + body[:open+1]}
for i, a := range args {
sep := ","
if i == len(args)-1 {
sep = ""
}
out = append(out, wrapArg(indent+" ", a, sep, width)...)
}
return append(out, indent+">"+end)
}
// wrapArg writes one type argument: whole when it fits, otherwise a union one
// member per line or a generic one argument per line.
func wrapArg(indent, arg, sep string, width int) []string {
if len([]rune(indent+arg+sep)) <= width {
return []string{indent + arg + sep}
}
if members := splitTopLevelUnion(arg); len(members) >= 2 {
out := make([]string, len(members))
for i, m := range members {
out[i] = indent + "| " + m
}
out[len(out)-1] += sep
return out
}
return wrapGeneric(indent, "", arg+sep, width)
}
// splitTopLevelUnion splits s at " | " outside brackets, angle brackets,
// parentheses and string literals.
func splitTopLevelUnion(s string) []string {
return splitTopLevel(s, " | ")
}
// splitTopLevel splits s at sep (a two or three character separator) outside
// brackets, angle brackets, parentheses and string literals.
func splitTopLevel(s, sep string) []string {
var (
parts []string
depth int
quote rune
escaped bool
start int
)
rs := []rune(s)
sr := []rune(sep)
for i := 0; i < len(rs); i++ {
r := rs[i]
switch {
case quote != 0:
switch {
case escaped:
escaped = false
case r == '\\':
escaped = true
case r == quote:
quote = 0
}
case r == '"' || r == '\'':
quote = r
case strings.ContainsRune("{[(<", r):
depth++
case strings.ContainsRune("}])>", r):
depth--
case depth == 0 && i+len(sr) <= len(rs) && string(rs[i:i+len(sr)]) == sep:
parts = append(parts, string(rs[start:i]))
start = i + len(sr)
i += len(sr) - 1
}
}
return append(parts, string(rs[start:]))
}