Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
tools/fix: allow fixing non-essential simplification
Browse files Browse the repository at this point in the history
First implementation is rewriting `_ | x` to `_`.

Change-Id: Idda745f5493dc092dd0a3677c52e7e79eee74aa3
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/8231
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Paul Jolly <paul@myitcv.org.uk>
  • Loading branch information
mpvl committed Jan 18, 2021
1 parent 28cfa74 commit f03928d
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 8 deletions.
7 changes: 6 additions & 1 deletion cmd/cue/cmd/fix.go
Expand Up @@ -54,6 +54,11 @@ func runFixAll(cmd *Command, args []string) error {
return err
}

var opts []fix.Option
if flagSimplify.Bool(cmd) {
opts = append(opts, fix.Simplify())
}

if len(args) == 0 {
args = []string{"./..."}

Expand Down Expand Up @@ -81,7 +86,7 @@ func runFixAll(cmd *Command, args []string) error {
Tools: true,
})

errs := fix.Instances(instances)
errs := fix.Instances(instances, opts...)

if errs != nil && flagForce.Bool(cmd) {
return errs
Expand Down
52 changes: 51 additions & 1 deletion tools/fix/fix.go
Expand Up @@ -28,8 +28,25 @@ import (
"cuelang.org/go/cue/token"
)

type Option func(*options)

type options struct {
simplify bool
}

// Simplify enables fixes that simplify the code, but are not strictly
// necessary.
func Simplify() Option {
return func(o *options) { o.simplify = true }
}

// File applies fixes to f and returns it. It alters the original f.
func File(f *ast.File) *ast.File {
func File(f *ast.File, o ...Option) *ast.File {
var options options
for _, f := range o {
f(&options)
}

// Rewrite integer division operations to use builtins.
f = astutil.Apply(f, func(c astutil.Cursor) bool {
n := c.Node()
Expand Down Expand Up @@ -248,5 +265,38 @@ func File(f *ast.File) *ast.File {
// return true
// }, nil).(*ast.File)

if !options.simplify {
return f
}

// Rewrite disjunctions with _ to _.
f = astutil.Apply(f, func(c astutil.Cursor) bool {
if x := findTop(c.Node()); x != nil {
c.Replace(x)
}
return true
}, nil).(*ast.File)

return f
}

func findTop(x ast.Node) ast.Expr {
switch x := x.(type) {
case *ast.BinaryExpr:
if x.Op != token.OR {
break
}
if v := findTop(x.X); v != nil {
return v
}
if v := findTop(x.Y); v != nil {
return v
}

case *ast.Ident:
if x.Name == "_" {
return x
}
}
return nil
}
23 changes: 19 additions & 4 deletions tools/fix/fix_test.go
Expand Up @@ -23,9 +23,10 @@ import (

func TestFile(t *testing.T) {
testCases := []struct {
name string
in string
out string
name string
in string
out string
simplify bool
}{{
name: "rewrite integer division",
in: `package foo
Expand Down Expand Up @@ -85,6 +86,14 @@ a: [ for x in y { x } ]
out: `
let y = foo
`,
}, {
simplify: true,
in: `
y: _ | {[string]: int}
`,
out: `y: _
`,

// }, {
// name: "slice",
// in: `package foo
Expand Down Expand Up @@ -135,7 +144,13 @@ let y = foo
if err != nil {
t.Fatal(err)
}
n := File(f)

var opts []Option
if tc.simplify {
opts = append(opts, Simplify())
}
n := File(f, opts...)

b, err := format.Node(n)
if err != nil {
t.Fatal(err)
Expand Down
4 changes: 2 additions & 2 deletions tools/fix/fixall.go
Expand Up @@ -25,7 +25,7 @@ import (
// Instances modifies all files contained in the given build instances at once.
//
// It also applies fix.File.
func Instances(a []*build.Instance) errors.Error {
func Instances(a []*build.Instance, o ...Option) errors.Error {
cwd, _ := os.Getwd()

// Collect all
Expand All @@ -34,7 +34,7 @@ func Instances(a []*build.Instance) errors.Error {
cwd: cwd,
}

p.visitAll(func(f *ast.File) { File(f) })
p.visitAll(func(f *ast.File) { File(f, o...) })

return p.err
}
Expand Down

0 comments on commit f03928d

Please sign in to comment.