Skip to content

Commit d70007c

Browse files
uhthomasmvdan
authored andcommitted
cmd/format: be consistent with whitespace in lists
Leading whitespace is not strictly enforced, but is incorrectly present when the formatting of a list is fixed. The behaviour can be made consistent by requesting no blanks at the start of a list. ```diff -[ 1] -[ for x in y {}] +[1] + [for x in y {}] ``` Fixes #1023 Change-Id: I8a57461d99cf6fd67105ab01283906e8ee60fac8 Signed-off-by: Thomas Way <[email protected]> Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1171302 Reviewed-by: Daniel Martí <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent 8c826bc commit d70007c

File tree

66 files changed

+164
-132
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+164
-132
lines changed

cmd/cue/cmd/eval.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ configuration file, instead of the entire configuration file itself.
4242
Examples:
4343
4444
$ cat <<EOF > foo.cue
45-
a: [ "a", "b", "c" ]
45+
a: ["a", "b", "c"]
4646
EOF
4747
4848
$ cue eval foo.cue -e a[0] -e a[2]

cmd/cue/cmd/root.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ Commands are defined in CUE as follows:
140140
command: deploy: {
141141
exec.Run
142142
cmd: "kubectl"
143-
args: [ "-f", "deploy" ]
143+
args: ["-f", "deploy"]
144144
in: json.Encode(userValue) // encode the emitted configuration.
145145
}
146146

cmd/cue/cmd/testdata/script/cmd_cycle.txtar

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ for _, input in inputs {
9999
}
100100

101101
command: print: cli.Print & {
102-
text: strings.Join([ for key, val in outputs { "key=\(key) val=\(val)" } ], "\n")
102+
text: strings.Join([for key, val in outputs { "key=\(key) val=\(val)" }], "\n")
103103
}
104104

105105
-- stdout2397.golden --

cmd/cue/cmd/testdata/script/cmd_func.txtar

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import "encoding/json"
88

99
x: y: 2
1010

11-
objects: [ for v in [ json.Marshal(x) ] {v} ]
11+
objects: [for v in [json.Marshal(x)] {v}]
1212

1313
-- k_tool.cue --
1414
package kube

cmd/cue/cmd/testdata/script/cmd_issue2060.txtar

+2-2
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ buildSource: {
118118
Bindings: [
119119
for _, strokeDef in strokeDefs {
120120
let bindingMap = strokeDef.Bindings["\(PlatformName)"]
121-
let bindingIds = [ for k, v in bindingMap {name: k, idx: v}]
121+
let bindingIds = [for k, v in bindingMap {name: k, idx: v}]
122122
let bindingIdsSorted = list.Sort(bindingIds, {x: {}, y: {}, less: x.idx < y.idx})
123-
let bindingKeys = [ for _, kv in bindingIdsSorted {kv.name}]
123+
let bindingKeys = [for _, kv in bindingIdsSorted {kv.name}]
124124
let bindingText = strings.Join(bindingKeys, "+")
125125
"DefText": "\(strokeDef.DefText)"
126126
"BindText": bindingText

cmd/cue/cmd/testdata/script/eval_mixedfiletypes.txtar

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ checks: {
4040
}
4141

4242
hasManager: {
43-
ok: len([ for m in team if list.Contains(m, "EM") {m}]) >= 1
43+
ok: len([for m in team if list.Contains(m, "EM") {m}]) >= 1
4444
}
4545
}
4646
-- y.cue --

cmd/cue/cmd/testdata/script/help.txtar

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Commands are defined in CUE as follows:
1919
command: deploy: {
2020
exec.Run
2121
cmd: "kubectl"
22-
args: [ "-f", "deploy" ]
22+
args: ["-f", "deploy"]
2323
in: json.Encode(userValue) // encode the emitted configuration.
2424
}
2525

cmd/cue/cmd/testdata/script/issue473.txtar

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ package x
1010

1111
Steps: [string]: #Step
1212

13-
#TerminalName: or([ for k, _ in Terminals {k}])
13+
#TerminalName: or([for k, _ in Terminals {k}])
1414

1515
#Step: {
1616
Terminal: #TerminalName
@@ -33,4 +33,4 @@ g: #Guide & {
3333
Cmd: "ls"
3434
}
3535
}
36-
}
36+
}

cmd/cue/cmd/testdata/script/load_pkg.txtar

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ foo: int
6161
bar: 3
6262
-- sort/print.cue --
6363
files: {}
64-
flat: [ for k, _ in files {k} ]
64+
flat: [for k, _ in files {k}]
6565
-- sort/root_1.cue --
6666
files: root_1: 0
6767
-- sort/root_2/child_1.cue --

cmd/cue/cmd/testdata/script/merge_interaction.txtar

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"tool/cli"
1717
)
1818

19-
objects: [ for x in map {x}]
19+
objects: [for x in map {x}]
2020

2121
command: dump: {
2222
cli.Print & {

cue/format/node.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ func (f *formatter) exprRaw(expr ast.Expr, prec1, depth int) {
659659
f.print(ws, x.Rbrace, token.RBRACE)
660660

661661
case *ast.ListLit:
662-
f.print(x.Lbrack, token.LBRACK, indent)
662+
f.print(x.Lbrack, token.LBRACK, noblank, indent)
663663
f.walkListElems(x.Elts)
664664
f.print(trailcomma, noblank)
665665
f.visitComments(f.current.pos)

cue/format/testdata/expressions.golden

+18-2
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@ import "list"
144144
e: [...int]
145145
e: [...int]
146146
e: [...int | float]
147-
e: [ for x in someObject if x > 9 {
147+
e: [for x in someObject if x > 9 {
148148
x
149149
}]
150-
e: [ for x in someObject if x > 9 {x}]
150+
e: [for x in someObject if x > 9 {x}]
151151
e: [
152152
for x in someObject
153153
if x > 9 {x}]
@@ -253,4 +253,20 @@ import "list"
253253

254254
2,
255255
])
256+
257+
m: [1, 2, 3]
258+
m: [1, 2, 3]
259+
m: [1, 2, 3]
260+
m: [1, 2, 3]
261+
m: [1, 2, 3]
262+
m: [1, 2, 3]
263+
m: [1, 2, 3]
264+
m: [1, 2, 3]
265+
m: [1, 2, 3]
266+
m: [if true {1}, 2, 3]
267+
n: [1]
268+
o: [{}]
269+
o: [{}]
270+
o: [{}]
271+
o: [{}]
256272
}

cue/format/testdata/expressions.input

+18-2
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,10 @@ import "list"
142142
e: [...int]
143143
e: [...int,]
144144
e: [...int | float]
145-
e: [ for x in someObject if x > 9 {
145+
e: [for x in someObject if x > 9 {
146146
x
147147
}]
148-
e: [ for x in someObject if x > 9 {x}]
148+
e: [for x in someObject if x > 9 {x}]
149149
e: [
150150
for x in someObject
151151
if x > 9 {x}]
@@ -250,4 +250,20 @@ import "list"
250250

251251
2,
252252
])
253+
254+
m: [1, 2, 3]
255+
m: [1, 2, 3,]
256+
m: [ 1, 2, 3, ]
257+
m: [ 1, 2, 3,]
258+
m: [ 1, 2, 3,]
259+
m: [ 1, 2, 3]
260+
m: [ 1, 2, 3,]
261+
m: [ 1, 2, 3, ]
262+
m: [ 1, 2, 3 ]
263+
m: [ if true { 1 }, 2, 3]
264+
n: [ 1]
265+
o: [{}]
266+
o: [ {}]
267+
o: [{} ]
268+
o: [ {} ]
253269
}

cue/parser/parser_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ func TestParse(t *testing.T) {
300300
"list types",
301301
`{
302302
a: 4*[int]
303-
b: <=5*[ {a: 5} ]
303+
b: <=5*[{a: 5}]
304304
c1: [...int]
305305
c2: [...]
306306
c3: [1, 2, ...int,]
@@ -310,7 +310,7 @@ func TestParse(t *testing.T) {
310310
"list comprehensions",
311311
`{
312312
y: [1,2,3]
313-
b: [ for x in y if x == 1 { x } ],
313+
b: [for x in y if x == 1 { x }],
314314
}`,
315315
`{y: [1, 2, 3], b: [for x in y if x==1 {x}]}`,
316316
}, {

cue/testdata/benchmarks/issue2176.txtar

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import (
3737
}
3838
}
3939
}
40-
[ for i, _ in unique_combinations {i}][0]
40+
[for i, _ in unique_combinations {i}][0]
4141
}
4242
}
4343
}

cue/testdata/builtins/incomplete.txtar

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ list1: {
1414

1515
// This evaluates to a list with an incomplete element.
1616
Top: [
17-
[ for _, F in _Sub {F}],
17+
[for _, F in _Sub {F}],
1818
]
1919

2020
_Sub: a.b

cue/testdata/compile/labels.txtar

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ ok9: [name=dis2]: string
2222
ok10: [{foo: "bar"}]: string // disallowed in evaluator
2323
ok11: [list.FlattenN([string], 1)]: string // disallowed in evaluator
2424

25-
bad1: [ for x in [1, 2, 3] {x}]: string
25+
bad1: [for x in [1, 2, 3] {x}]: string
2626

2727
saneReferencesInComprehensions: {
2828
for _ in [1] {
@@ -31,7 +31,7 @@ saneReferencesInComprehensions: {
3131
}
3232
-- out/compile --
3333
bad1: comprehension values not allowed in this position:
34-
./in.cue:24:9
34+
./in.cue:24:8
3535
--- in.cue
3636
{
3737
dis1: ("dev"|"prd")
@@ -91,4 +91,4 @@ bad1: comprehension values not allowed in this position:
9191
}
9292
-- out/eval --
9393
bad1: comprehension values not allowed in this position:
94-
./in.cue:24:9
94+
./in.cue:24:8

cue/testdata/comprehensions/015_list_comprehension.txtar

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
#name: list comprehension
44
#evalFull
55
-- in.cue --
6-
a: [ for k, v in b if k < "d" if v > b.a {k}]
6+
a: [for k, v in b if k < "d" if v > b.a {k}]
77
b: {
88
a: 1
99
b: 2
1010
c: 3
1111
d: 4
1212
}
13-
c: [ for _, x in b for _, y in b if x < y {x}]
14-
d: [ for x, _ in a {x}]
13+
c: [for _, x in b for _, y in b if x < y {x}]
14+
d: [for x, _ in a {x}]
1515
-- out/def --
1616
a: ["b", "c"]
1717
b: {

cue/testdata/comprehensions/checkdefined.txtar

+6-6
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,31 @@ okc1: { if xc.undefined != _|_ {a: 1} }
1717
oko1: { if xo.undefined != _|_ {a: 1} }
1818

1919
okc2: {
20-
if ({} & {s: [ for y in xc.undefined {}]}) != _|_ {a: 1}
20+
if ({} & {s: [for y in xc.undefined {}]}) != _|_ {a: 1}
2121
}
2222

2323
oko2: {
24-
if ({} & {s: [ for y in xo.undefined {}]}) != _|_ {a: 1}
24+
if ({} & {s: [for y in xo.undefined {}]}) != _|_ {a: 1}
2525
}
2626

2727
okc3: {
28-
if ({s: [ for y in xc.undefined {}]}) != _|_ {a: 1}
28+
if ({s: [for y in xc.undefined {}]}) != _|_ {a: 1}
2929
}
3030

3131
oko3: {
32-
if ({s: [ for y in xo.undefined {}]}) != _|_ {a: 1}
32+
if ({s: [for y in xo.undefined {}]}) != _|_ {a: 1}
3333
}
3434

3535
issue1969: okc: {
3636
let X = xc.undefined
37-
let Y = {} & {s: [ for y in list.Range(0, X, 1) {}]}
37+
let Y = {} & {s: [for y in list.Range(0, X, 1) {}]}
3838

3939
if Y != _|_ {Y}
4040
}
4141

4242
issue1969: oko: {
4343
let X = xo.undefined
44-
let Y = {} & {s: [ for y in list.Range(0, X, 1) {}]}
44+
let Y = {} & {s: [for y in list.Range(0, X, 1) {}]}
4545

4646
if Y != _|_ {Y}
4747
}

cue/testdata/comprehensions/incomplete.txtar

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ cond: bool
33
src: {}
44
top: _
55
a: [ if cond {}]
6-
b: [ for x in src.foo {}]
6+
b: [for x in src.foo {}]
77
c: {for x in top {}}
88
-- out/eval/stats --
99
Leaks: 0
@@ -27,7 +27,7 @@ Disjuncts: 7
2727
}
2828
b: (_|_){
2929
// [incomplete] b: undefined field: foo:
30-
// ./in.cue:5:19
30+
// ./in.cue:5:18
3131
}
3232
c: (_|_){
3333
// [incomplete] c: cannot range over top (incomplete type _):

cue/testdata/comprehensions/lists.txtar

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- in.cue --
22
a: [{a: 1}, {b: 2 & 3}]
33

4-
b: [ for x in a {x}]
4+
b: [for x in a {x}]
55
-- out/eval/stats --
66
Leaks: 0
77
Freed: 7

cue/testdata/cycle/chain.txtar

+3-3
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ issue2052: t1: {
100100
out: {
101101
if (#in & #basic) != _|_ {1}
102102
if (#in & #basic) == _|_ {
103-
list.Max([ for k, v in #in {(#next & {#in: v}).out}]) + 1
103+
list.Max([for k, v in #in {(#next & {#in: v}).out}]) + 1
104104
}
105105
}
106106
}
@@ -139,7 +139,7 @@ issue2052: t2: {
139139
out: {
140140
if (#in & #basic) != _|_ {1}
141141
if (#in & #basic) == _|_ {
142-
list.Max([ for k, v in #in {(#next & {#in: v}).out}]) + 1
142+
list.Max([for k, v in #in {(#next & {#in: v}).out}]) + 1
143143
}
144144
}
145145
}
@@ -188,7 +188,7 @@ issue2052: full: {
188188
// if we are not a basic type, then we are 1 + the max of children
189189
if (#in & #basic) == _|_ {
190190
// this is our "recursion" for each child
191-
let depths = [ for k, v in #in {(#next & {#in: v}).out}]
191+
let depths = [for k, v in #in {(#next & {#in: v}).out}]
192192
list.Max(depths) + 1
193193
}
194194
}

0 commit comments

Comments
 (0)