this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

cue: allow and enforce parens for single postfix alias

Allow ~(X) as an alternative to ~X for the
single postfix alias form. The formatter now
always outputs the parenthesized form ~(X),
making it consistent with the dual form ~(K,V).

Signed-off-by: Marcel van Lohuizen <mpvl@gmail.com>
Change-Id: I191ebcc4faaf9ebf90059ecdc1525624737a3790
Reviewed-on: https://cue.gerrithub.io/c/cue-lang/cue/+/1236300
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>

+42 -18
+3 -1
cue/format/node.go
··· 426 426 f.expr(a.Field) 427 427 f.print(a.Rparen, token.RPAREN, noblank) 428 428 } else { 429 - // Simple form: ~X 429 + // Simple form: always output with parens ~(X) 430 + f.print(a.Lparen, token.LPAREN, noblank) 430 431 f.expr(a.Field) 432 + f.print(a.Rparen, token.RPAREN, noblank) 431 433 } 432 434 } 433 435
+13 -1
cue/parser/parser.go
··· 1519 1519 1520 1520 switch p.tok { 1521 1521 case token.LPAREN: 1522 - // Dual form: ~(K,V) 1522 + // Parenthesized form: ~(X) or ~(K,V) 1523 1523 lparen := p.pos 1524 1524 p.next() 1525 1525 ··· 1528 1528 return nil 1529 1529 } 1530 1530 k := p.parseIdent() 1531 + 1532 + if p.tok == token.RPAREN { 1533 + // Single form with parens: ~(X) 1534 + rparen := p.pos 1535 + p.next() 1536 + return &ast.PostfixAlias{ 1537 + Tilde: pos, 1538 + Lparen: lparen, 1539 + Field: k, 1540 + Rparen: rparen, 1541 + } 1542 + } 1531 1543 1532 1544 comma := p.expect(token.COMMA) 1533 1545 if !comma.IsValid() {
+12 -2
cue/testdata/references/alias.txtar
··· 7 7 } 8 8 b1: X1.foo + 2 @test(eq, 3) 9 9 10 + a1p~(X1p): { 11 + foo: 1 12 + } 13 + b1p: X1p.foo + 2 @test(eq, 3) 14 + 15 + 10 16 // Basic postfix alias - dual form 11 17 a2~(K2,V2): { 12 18 name: K2 ··· 207 213 usedef: X21.field @test(eq, 42) 208 214 -- out/errors.txt -- 209 215 [incomplete] test1: cannot reference optional field: constrained: 210 - ./in.cue:114:8 216 + ./in.cue:120:8 211 217 [incomplete] opt.b: cannot reference optional field: opt: 212 - ./in.cue:125:5 218 + ./in.cue:131:5 213 219 -- out/compile -- 214 220 --- in.cue 215 221 { ··· 217 223 foo: 1 218 224 } 219 225 b1: (〈0;a1〉.foo + 2) 226 + a1p: { 227 + foo: 1 228 + } 229 + b1p: (〈0;a1p〉.foo + 2) 220 230 a2: { 221 231 name: "a2" 222 232 val: 〈1;a2〉.foo
+14 -14
tools/fix/testdata/aliasv2.txtar
··· 201 201 package foo 202 202 203 203 // Simple field alias 204 - a~X: { 204 + a~(X): { 205 205 foo: 1 206 206 bar: X.foo + 2 207 207 } 208 208 209 209 // Multiple aliases 210 - b~Y: { 210 + b~(Y): { 211 211 data: 42 212 212 } 213 - c~Z: { 213 + c~(Z): { 214 214 val: Y.data 215 215 } 216 216 --- b.cue ··· 220 220 221 221 // Nested aliases 222 222 outer: { 223 - middle~Inner: { 224 - deep~Leaf: { 223 + middle~(Inner): { 224 + deep~(Leaf): { 225 225 value: 1 226 226 } 227 227 ref: Leaf.value ··· 234 234 package foo 235 235 236 236 // With constraints 237 - a~OptField?: { 237 + a~(OptField)?: { 238 238 value: 1 239 239 } 240 - b~ReqField!: { 240 + b~(ReqField)!: { 241 241 value: 2 242 242 } 243 243 --- d.cue ··· 246 246 247 247 package foo 248 248 249 - a~X: { 249 + a~(X): { 250 250 foo: 1 251 251 } 252 252 --- e.cue ··· 272 272 package foo 273 273 274 274 // Dynamic fields with old alias syntax 275 - (x)~DynAlias: { 275 + (x)~(DynAlias): { 276 276 field: 1 277 277 ref: DynAlias.field 278 278 } 279 279 280 280 // Dynamic field with string literal 281 - ("foo")~StrAlias: { 281 + ("foo")~(StrAlias): { 282 282 data: 2 283 283 ref: StrAlias.data 284 284 } 285 285 286 286 // Dynamic field with interpolation 287 - ("\(name)")~DynInterpAlias: { 287 + ("\(name)")~(DynInterpAlias): { 288 288 val: 3 289 289 ref: InterpAlias.val 290 290 } 291 291 // Just the interpolation 292 - "\(name)"~InterpAlias: { 292 + "\(name)"~(InterpAlias): { 293 293 val: 3 294 294 ref: InterpAlias.val 295 295 } ··· 376 376 377 377 // Field alias named self at same scope 378 378 aliassamescope: { 379 - bar~self: { 379 + bar~(self): { 380 380 data: 1 381 381 } 382 382 baz: self.data ··· 389 389 390 390 // Field alias named self in enclosing struct 391 391 aliasenclosing: { 392 - bar~self: { 392 + bar~(self): { 393 393 data: 1 394 394 } 395 395 baz: self.data