-
Notifications
You must be signed in to change notification settings - Fork 5
/
explode.go
181 lines (152 loc) · 4.29 KB
/
explode.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
package sol
import (
"fmt"
"mvdan.cc/sh/v3/syntax"
)
// Apply various transformations to a shell program, and indent the result as
// needed (for use in recursion).
func ExplodeSh(src string, idt int, hang bool) (string, error) {
// First, we determine "insert" changes (namely, inserting line breaks).
chgsIns := []change{}
pp, err := parseProg(src)
if err != nil {
return "", fmt.Errorf("could not parse program: %w", err)
}
syntax.Walk(pp, func(node syntax.Node) bool {
switch x := node.(type) {
case *syntax.BinaryCmd:
if Cfg.BinCmd {
pos := int(x.Y.Position.Offset())
chgsIns = append(chgsIns, change{pos, pos, "\n"})
}
case *syntax.ProcSubst:
if Cfg.ProcSubst {
pos := int(x.OpPos.Offset())
chgsIns = append(chgsIns, change{pos, pos, "\\\n"})
chgsIns = append(chgsIns, change{pos + 2, pos + 2, "\n"})
}
case *syntax.CmdSubst:
if Cfg.CmdSubst {
pos := int(x.Left.Offset())
chgsIns = append(chgsIns, change{pos, pos, "\\\n"})
chgsIns = append(chgsIns, change{pos + 2, pos + 2, "\n"})
}
case *syntax.CallExpr:
if Cfg.Args {
for _, arg := range x.Args {
for _, part := range arg.Parts {
// It's hard to know if argument is simply a flag or an
// option that takes a value. Easiest thing to do is to
// break on hyphen. This won't catch all cases, (e.g.,
// positional arguments), but it'll handle 90% of work
// of sensibily breaking up arguments.
if string(src[part.Pos().Offset()]) == "-" {
pos := int(part.Pos().Offset())
chgsIns = append(chgsIns, change{pos, pos, "\\\n"})
}
}
}
}
case *syntax.ForClause:
if Cfg.Clause {
pos := int(x.DoPos.Offset()) + 2
chgsIns = append(chgsIns, change{pos, pos, "\n"})
}
case *syntax.WhileClause:
if Cfg.Clause {
pos := int(x.DoPos.Offset()) + 2
chgsIns = append(chgsIns, change{pos, pos, "\n"})
}
case *syntax.IfClause:
if Cfg.Clause {
var pos int
if x.ThenPos.IsValid() {
pos = int(x.ThenPos.Offset()) + 4
} else {
pos = int(x.Position.Offset()) + 4
}
chgsIns = append(chgsIns, change{pos, pos, "\n"})
}
case *syntax.CaseClause:
if Cfg.Clause {
pos := int(x.In.Offset()) + 2
chgsIns = append(chgsIns, change{pos, pos, "\n"})
}
case *syntax.CaseItem:
if Cfg.Clause {
pos := int(x.OpPos.Offset())
chgsIns = append(chgsIns, change{pos, pos, "\n"})
pos = pos + len(x.Op.String())
chgsIns = append(chgsIns, change{pos, pos, "\n"})
}
case *syntax.Redirect:
if Cfg.Redir {
pos := int(x.OpPos.Offset())
chgsIns = append(chgsIns, change{pos, pos, "\\\n"})
}
}
return true
})
// Apply _insert_ changes.
srcIns, err := fmtProg(modProg(src, chgsIns))
if err != nil {
return "", fmt.Errorf("could not format program: %w", err)
}
// Next, we determine "replace" changes (e.g., replace a inline command
// string with a line-broken version).
chgsRpl := []change{}
pp, err = parseProg(srcIns)
if err != nil {
return "", fmt.Errorf("could not parse program: %w", err)
}
var walkErr error
syntax.Walk(pp, func(node syntax.Node) bool {
switch x := node.(type) {
case *syntax.CallExpr:
if len(x.Args) > 0 {
if Cfg.Env {
_, err := getCmdTypes(getCmdVal(*x))
if err != nil {
walkErr = fmt.Errorf("could not get command types: %w", err)
return false
}
}
if Cfg.Jq {
chgsJq, err := fmtJq(x, false, srcIns)
if err != nil {
walkErr = fmt.Errorf("could not determine jq changes: %w", err)
return false
}
for _, chg := range chgsJq {
chgsRpl = append(chgsRpl, chg)
}
}
if Cfg.Sh {
chgsSh, err := fmtSh(x, false, srcIns)
if err != nil {
walkErr = fmt.Errorf("could not determine shell changes: %w", err)
return false
}
for _, chg := range chgsSh {
chgsRpl = append(chgsRpl, chg)
}
}
}
}
return true
})
if walkErr != nil {
return "", fmt.Errorf("could not walk program: %w", walkErr)
}
// Apply _replace_ changes.
srcRpl, err := fmtProg(modProg(srcIns, chgsRpl))
if err != nil {
return "", fmt.Errorf("could not format program: %w", err)
}
// Indent.
srcIdt, err := indent(srcRpl, idt, hang)
if err != nil {
return "", fmt.Errorf("could not indent program: %w", err)
}
return srcIdt, nil
}