Skip to content

Commit

Permalink
cmd/cue/cmd: fmt rewrites alias to let clauses
Browse files Browse the repository at this point in the history
Issue #339

Change-Id: I0bcb634251e54300f8d84f8a822c2081902a85dd
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/5685
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Apr 23, 2020
1 parent 5114891 commit 50bcd97
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
23 changes: 23 additions & 0 deletions cmd/cue/cmd/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,29 @@ import (
)

func fix(f *ast.File) *ast.File {
// Rewrite an old-style alias to a let clause.
ast.Walk(f, func(n ast.Node) bool {
var decls []ast.Decl
switch x := n.(type) {
case *ast.StructLit:
decls = x.Elts
case *ast.File:
decls = x.Decls
}
for i, d := range decls {
if a, ok := d.(*ast.Alias); ok {
x := &ast.LetClause{
Ident: a.Ident,
Equal: a.Equal,
Expr: a.Expr,
}
astutil.CopyMeta(x, a)
decls[i] = x
}
}
return true
}, nil)

// Rewrite block comments to regular comments.
ast.Walk(f, func(n ast.Node) bool {
switch x := n.(type) {
Expand Down
7 changes: 7 additions & 0 deletions cmd/cue/cmd/fix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ a: {
`,
out: `y: [1, 2, 3, 4]
a: [ for x in y { x } ]
`,
}, {
in: `
y = foo
`,
out: `
let y = foo
`,
// }, {
// name: "slice",
Expand Down
4 changes: 4 additions & 0 deletions cmd/cue/cmd/testdata/script/legacy.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ cmp foo.cue bar.cue
-- foo.cue --
a: [1]
b: [ x for x in a ]

X = foo
-- bar.cue --
a: [1]
b: [ for x in a { x } ]

let X = foo

0 comments on commit 50bcd97

Please sign in to comment.