-
Notifications
You must be signed in to change notification settings - Fork 5
/
transform.go
430 lines (373 loc) · 10.9 KB
/
transform.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
package sol
import (
"bytes"
"fmt"
"regexp"
"sort"
"strconv"
"strings"
"github.com/noperator/jqfmt"
// log "github.com/sirupsen/logrus"
"mvdan.cc/sh/v3/syntax"
)
type change struct {
Pos int
End int
Str string
}
// Returns the modified source after applying specified changes.
func modProg(src string, chgs []change) string {
// Iterate over positions in reverse order. This is important because going
// the other direction (forward) would change the length of the source
// after every change, which would break the Pos/End offsets in each
// subsequent change we need to make.
posToIdx := map[int]int{}
chgPos := []int{}
for chgIdx, chg := range chgs {
posToIdx[chg.Pos] = chgIdx
chgPos = append(chgPos, chg.Pos)
}
sort.Ints(chgPos)
for pos := len(chgPos) - 1; pos >= 0; pos-- {
idx := posToIdx[chgPos[pos]]
chg := chgs[idx]
src = src[:chg.Pos] + chg.Str + src[chg.End:]
}
return src
}
// Returns a parsed program (i.e., a syntax tree) that can be walked, etc.
func parseProg(src string) (*syntax.File, error) {
in := strings.NewReader(src)
pp, err := syntax.NewParser().Parse(in, "")
if err != nil {
return nil, fmt.Errorf("could not parse program: %w", err)
}
return pp, nil
}
// TODO: handle multiple options
// opt ... syntax.PrinterOption
// cannot use opt (variable of type []"mvdan.cc/sh/v3/syntax".PrinterOption) as
// "mvdan.cc/sh/v3/syntax".PrinterOption value in argument to syntax.NewPrinter
func treeToStr(pp *syntax.File, opt syntax.PrinterOption) string {
prtByt := new(bytes.Buffer)
syntax.NewPrinter(opt).Print(prtByt, pp)
prtStr := prtByt.String()
// Remove trailing newline that's printed when a *File is used.
// - https://pkg.go.dev/mvdan.cc/sh/v3/syntax#Printer.Print
prtStr = prtStr[:len(prtStr)-1]
return prtStr
}
// Returns a prettified version of the input program.
func fmtProg(src string) (string, error) {
pp, err := parseProg(src)
if err != nil {
return "", fmt.Errorf("could not parse program: %w", err)
}
return treeToStr(pp, syntax.Indent(4)), nil
}
func enforceMaxWidth(src string) (string, error) {
srcLines := strings.Split(src, "\n")
srcWide := ""
for sl := 0; sl < len(srcLines); sl++ {
// When we grab the next line to work, we want to make sure it's
// properly indented with regard to the lines we've already adjusted.
srcLn := srcLines[sl]
tmpIdt, err := normalizeIndents(fmt.Sprintf("%s\n%s", srcWide, srcLn))
if err != nil {
return "", fmt.Errorf("could not normalize indents: %w", err)
}
tmpIdtLines := strings.Split(tmpIdt, "\n")
wkLn := tmpIdtLines[len(tmpIdtLines)-1]
wkLnTrm := ""
// In this loop, we'll try to add as many fragments as possible to the
// current working line, as long as the total width doesn't exceed the
// maximum width.
for len(wkLn) < Cfg.MaxWidth && sl < len(srcLines)-1 {
// Trim the right side of the working line to prepare the next
// fragment to be appended to it.
wkLnTrm = strings.TrimRight(wkLn, " ")
wkLnTrm = strings.TrimSuffix(wkLnTrm, "\\")
wkLnTrm = strings.TrimRight(wkLnTrm, " ")
// Grab the next fragment that we'll attempt to add to the current
// working line, hoping that it doesn't exceed the maximum width.
frag := strings.TrimSpace(srcLines[sl+1])
wkLnTrmFrag := fmt.Sprintf("%s %s", wkLnTrm, frag)
if len(wkLnTrmFrag) <= Cfg.MaxWidth {
wkLn = wkLnTrmFrag
sl++
} else {
break
}
}
srcWide += fmt.Sprintf("%s\n", wkLn)
}
if len(srcWide) <= 1 {
return srcWide, nil
} else {
return srcWide[:len(srcWide)-1], nil
}
}
// Make sure that the final line-broken command string has sensible
// indentation. This means that there shouldn't be a "jump" in indentation
// where things are indented at 4 spaces, then 12 spaces (without something
// also indented at 8 spaces).
func normalizeIndents(src string) (string, error) {
srcLines := strings.Split(src, "\n")
if len(srcLines) == 1 {
return src, nil
}
// Count the number of leading spaces in each line.
lineSpaces := map[int]int{}
for l, line := range srcLines {
spaceCount := 0
for _, char := range line {
if char == ' ' {
spaceCount++
} else {
break
}
}
lineSpaces[l] = spaceCount
}
// We'll want to walk back over the command string, starting with the
// least-indented lines first.
linesBySpaces := make([]int, 0, len(lineSpaces))
for l := range lineSpaces {
linesBySpaces = append(linesBySpaces, l)
}
sort.Slice(linesBySpaces, func(i, j int) bool {
return lineSpaces[linesBySpaces[i]] < lineSpaces[linesBySpaces[j]]
})
// We build a map of lines that need to be decremented by 4 spaces...
last := 0
dec := map[int]int{}
for _, l := range linesBySpaces {
if lineSpaces[l] != last && lineSpaces[l] != last+4 {
dec[l] = lineSpaces[l] - (last + 4)
} else {
last = lineSpaces[l]
}
}
// ...and then apply the decrements.
srcCln := ""
for l, ln := range strings.Split(src, "\n") {
rm := dec[l]
srcCln += fmt.Sprintf("%s\n", ln[rm:])
}
if len(srcCln) <= 1 {
return srcCln, nil
} else {
return srcCln[:len(srcCln)-1], nil
}
}
func indent(src string, idt int, hang bool) (string, error) {
// Build indent string.
idtStr := ""
for i := 0; i < idt; i++ {
idtStr += " "
}
srcIdt := ""
first := true
for _, srcLn := range strings.Split(src, "\n") {
// Drop blank lines if they made their way in somehow.
m, err := regexp.MatchString("^ *$", srcLn)
if err != nil {
return "", fmt.Errorf("could not match line: %w", err)
} else if m {
continue
}
// Leave the first line with no added indentation.
if first && hang {
srcIdt += fmt.Sprintf("%s\n", srcLn)
first = false
} else {
// Indent all other lines.
srcIdt += fmt.Sprintf("%s%s\n", idtStr, srcLn)
}
}
if len(srcIdt) <= 1 {
return srcIdt, nil
} else {
return srcIdt[:len(srcIdt)-1], nil
}
}
func getCmdVal(x syntax.CallExpr) string {
cmdPart := x.Args[0].Parts[0]
cmd := ""
switch c := cmdPart.(type) {
case *syntax.Lit:
cmd = c.Value
case *syntax.SglQuoted:
cmd = c.Value
case *syntax.DblQuoted:
switch c := c.Parts[0].(type) {
case *syntax.Lit:
cmd = c.Value
case *syntax.CmdSubst:
y := c.Stmts[0].Cmd.(*syntax.CallExpr)
cmd = getCmdVal(*y)
}
}
return cmd
}
func fmtSh(x *syntax.CallExpr, implode bool, src string) ([]change, error) {
cmd := getCmdVal(*x)
atCmdStr := false
chgs := []change{}
shArgIdx := 0
cmdStrArgIdx := 0
if cmd == "xargs" || cmd == "parallel" {
// First, try looking for a shell being explicitly invoked.
// e.g., `xargs bash -c 'echo {}'`
for a, arg := range x.Args {
if shArgIdx > 0 {
break
}
part := arg.Parts[0]
if fmt.Sprintf("%T", part) == "*syntax.Lit" && strings.HasSuffix(part.(*syntax.Lit).Value, "sh") {
shArgIdx = a
}
}
// Next, try looking for a probable command string.
// e.g., `xargs 'echo {}'`
if shArgIdx == 0 {
for a := len(x.Args) - 1; a >= 0; a-- {
if cmdStrArgIdx > 0 {
break
}
part := x.Args[a].Parts[0]
if fmt.Sprintf("%T", part) == "*syntax.SglQuoted" || fmt.Sprintf("%T", part) == "*syntax.DblQuoted" {
cmdStrArgIdx = a
atCmdStr = true
}
}
}
}
// A non-zero `shArgIdx` indicates that a shell is invoked down the line by
// another program. For example, in the line below, `shArgIdx == 3` because
// `bash` is the 3rd argument to `xargs`.
// ```
// 0 1 2 3 4 5
// xargs -0n 2 bash -c 'echo $1'
// ```
// TODO: Build a list of common shells instead of matching on suffix. e.g.,
// bash, csh, dash, ksh, sh, tcsh, zsh
// TODO: Clean this up.
if strings.HasSuffix(cmd, "sh") || shArgIdx > 0 || cmdStrArgIdx > 0 {
var startIdx int
if shArgIdx > 0 {
startIdx = shArgIdx + 1
} else if cmdStrArgIdx > 0 {
startIdx = cmdStrArgIdx
}
for _, arg := range x.Args[startIdx:] {
// TODO: Do I need to check parts? Does an arg ever have more than
// one part, or can I just check the first part?
for _, part := range arg.Parts {
// If we find `-c`, then we know the next argument is a shell command.
// TODO: Can we handle something like `-ic` where two args are
// joined together?
if fmt.Sprintf("%T", part) == "*syntax.Lit" && part.(*syntax.Lit).Value == "-c" {
atCmdStr = true
} else if atCmdStr {
cmdStr := ""
lineNumStr := ""
// TODO: What to do about dollar? Just ignore it?
syntax.Walk(part, func(node syntax.Node) bool {
switch x := node.(type) {
case *syntax.SglQuoted:
cmdStr = x.Value
lineNumStr = strings.Split(x.Pos().String(), ":")[0]
case *syntax.DblQuoted:
cmdStr = x.Parts[0].(*syntax.Lit).Value
lineNumStr = strings.Split(x.Parts[0].Pos().String(), ":")[0]
}
return true
})
lineNum, err := strconv.Atoi(lineNumStr)
if err != nil {
err = fmt.Errorf("could not get column value: %w", err)
return chgs, fmt.Errorf("could not get column value: %w", err)
}
line := strings.Split(src, "\n")[lineNum-1]
spaceCount := 0
for _, char := range line {
if char == ' ' {
spaceCount++
} else {
break
}
}
cmdStrMod := ""
if implode {
cmdStrMod, err = ImplodeSh(cmdStr)
} else {
cmdStrMod, err = ExplodeSh(cmdStr, spaceCount, true)
}
if err != nil {
return chgs, fmt.Errorf("could not format shell: %w", err)
}
pos := int(part.Pos().Offset())
end := int(part.End().Offset())
chgs = append(chgs, change{pos + 1, end - 1, cmdStrMod})
break
}
}
}
}
return chgs, nil
}
func fmtJq(x *syntax.CallExpr, implode bool, src string) ([]change, error) {
cmd := getCmdVal(*x)
chgs := []change{}
if cmd == "jq" || cmd == "gojq" {
found := false
for a := len(x.Args) - 1; a > 0; a-- {
part := x.Args[a].Parts[0]
jqStr := ""
lineNumStr := ""
switch x := part.(type) {
case *syntax.SglQuoted:
found = true
jqStr = x.Value
lineNumStr = strings.Split(x.Pos().String(), ":")[0]
case *syntax.DblQuoted:
found = true
jqStr = x.Parts[0].(*syntax.Lit).Value
lineNumStr = strings.Split(x.Parts[0].Pos().String(), ":")[0]
}
if found {
lineNum, err := strconv.Atoi(lineNumStr)
if err != nil {
err = fmt.Errorf("could not get column value: %w", err)
return chgs, fmt.Errorf("could not get column value: %w", err)
}
line := strings.Split(src, "\n")[lineNum-1]
spaceCount := 0
for _, char := range line {
if char == ' ' {
spaceCount++
} else {
break
}
}
jqStrMod, err := jqfmt.DoThing(jqStr, Cfg.JqFmtCfg)
if err != nil {
return chgs, fmt.Errorf("could not parse jq: %w", err)
}
if !implode {
jqStrMod, err = indent(jqStrMod, spaceCount+4, true)
if err != nil {
return chgs, fmt.Errorf("could not indent query: %w", err)
}
}
pos := int(part.Pos().Offset())
end := int(part.End().Offset())
chgs = append(chgs, change{pos + 1, end - 1, jqStrMod})
// TODO: Parse with gojq and report errors.
break
}
}
}
return chgs, nil
}