Skip to content

Commit d0617b5

Browse files
committed
tools/fix: correct the ast walker so sub-expressions get fixed
Originally, the ast walker in fix was using the "before" function. But this would mean recursion would stop as soon as it does any replacement. This means it wouldn't work for nested expressions. By simply switching to the "after" function, we ensure we don't do any replacement until we've bottomed out in the AST. Signed-off-by: Matthew Sackman <[email protected]> Change-Id: I5372b319429f48c222b2a8b4a6507873f19250c1 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1200495 Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Daniel Martí <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent 5936450 commit d0617b5

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

cmd/cue/cmd/testdata/script/fix.txtar

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ exec cue fix ./...
22
# Make sure we fix all files in a directory, even if they're a mix of packages (or no packages).
33
cmp p/one.cue p/one.cue.fixed
44
cmp p/two.cue p/two.cue.fixed
5-
# TODO: the following cmp fails because of a bug in fix/fix.go
6-
! cmp p/three.cue p/three.cue.fixed
5+
cmp p/three.cue p/three.cue.fixed
76
# TODO: the added imports have unnecessary name-mangling. https://cuelang.org/issue/3408
87
-- p/one.cue --
98
package one

tools/fix/fix.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ func File(f *ast.File, o ...Option) *ast.File {
4444
f(&options)
4545
}
4646

47-
f = astutil.Apply(f, func(c astutil.Cursor) bool {
47+
// Make sure we use the "after" function, and not the "before",
48+
// because "before" will stop recursion early which creates
49+
// problems with nested expressions.
50+
f = astutil.Apply(f, nil, func(c astutil.Cursor) bool {
4851
n := c.Node()
4952
switch n := n.(type) {
5053
case *ast.BinaryExpr:
@@ -94,7 +97,7 @@ func File(f *ast.File, o ...Option) *ast.File {
9497
}
9598
}
9699
return true
97-
}, nil).(*ast.File)
100+
}).(*ast.File)
98101

99102
if options.simplify {
100103
f = simplify(f)

0 commit comments

Comments
 (0)