Skip to content

Commit

Permalink
tools/fix: avoid name-mangled imports
Browse files Browse the repository at this point in the history
By including a call to sanitize, and avoiding the ast cursor Import
method, we can avoid the name mangling of imports.

Fixes #3408

Signed-off-by: Matthew Sackman <matthew@cue.works>
Change-Id: I9bfc7082b504ac7a7d3c0a61580aef5bc8c3cd18
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1200498
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
  • Loading branch information
cuematthew committed Sep 2, 2024
1 parent d0617b5 commit 1440b9e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 29 deletions.
13 changes: 6 additions & 7 deletions cmd/cue/cmd/testdata/script/fix.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ exec cue fix ./...
cmp p/one.cue p/one.cue.fixed
cmp p/two.cue p/two.cue.fixed
cmp p/three.cue p/three.cue.fixed
# TODO: the added imports have unnecessary name-mangling. https://cuelang.org/issue/3408
-- p/one.cue --
package one

Expand All @@ -17,16 +16,16 @@ out: ["a"] + ((["a"]*7) + ["gh"])
-- p/one.cue.fixed --
package one

import list6c6973 "list"
import "list"

out: list6c6973.Concat([["foo"], ["bar"]])
out: list.Concat([["foo"], ["bar"]])
-- p/two.cue.fixed --
package two

import list6c6973 "list"
import "list"

out: list6c6973.Repeat(["baz"], 3)
out: list.Repeat(["baz"], 3)
-- p/three.cue.fixed --
import list6c6973 "list"
import "list"

out: list6c6973.Concat([["a"], (list6c6973.Concat([(list6c6973.Repeat(["a"], 7)), ["gh"]]))])
out: list.Concat([["a"], (list.Concat([(list.Repeat(["a"], 7)), ["gh"]]))])
34 changes: 22 additions & 12 deletions tools/fix/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,27 +72,27 @@ func File(f *ast.File, o ...Option) *ast.File {
if !(xIsList || yIsList) {
break
}
pkg := c.Import("list")
if pkg == nil {
break
}
if n.Op == token.ADD {
// Rewrite list addition to use list.Concat
ast.SetRelPos(x, token.NoSpace)
c.Replace(&ast.CallExpr{
Fun: ast.NewSel(pkg, "Concat"),
Args: []ast.Expr{ast.NewList(x, y)},
})
c.Replace(ast.NewCall(
ast.NewSel(&ast.Ident{
Name: "list",
Node: ast.NewImport(nil, "list"),
}, "Concat"), ast.NewList(x, y)),
)
} else {
// Rewrite list multiplication to use list.Repeat
if !xIsList {
x, y = y, x
}
ast.SetRelPos(x, token.NoSpace)
c.Replace(&ast.CallExpr{
Fun: ast.NewSel(pkg, "Repeat"),
Args: []ast.Expr{x, y},
})
c.Replace(ast.NewCall(
ast.NewSel(&ast.Ident{
Name: "list",
Node: ast.NewImport(nil, "list"),
}, "Repeat"), x, y),
)
}
}
}
Expand All @@ -103,5 +103,15 @@ func File(f *ast.File, o ...Option) *ast.File {
f = simplify(f)
}

err := astutil.Sanitize(f)
// TODO: this File method is public, and its signature was fixed
// before we started calling Sanitize. Ideally, we want to return
// this error, but that would require deprecating this File method,
// and creating a new one, which might happen in due course if we
// also discover that we need to be a bit more flexible than just
// accepting a File.
if err != nil {
panic(err)
}
return f
}
20 changes: 10 additions & 10 deletions tools/fix/fix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ c: a + [8]
d: [9] + a
e: [0] + [1]
`,
out: `import list6c6973 "list"
out: `import "list"
a: [7]
b: a + a
c: list6c6973.Concat([a, [8]])
d: list6c6973.Concat([[9], a])
e: list6c6973.Concat([[0], [1]])
c: list.Concat([a, [8]])
d: list.Concat([[9], a])
e: list.Concat([[0], [1]])
`,
},

Expand All @@ -93,14 +93,14 @@ d: [7] * c
e: c * [8]
f: [9] * 5
`,
out: `import list6c6973 "list"
out: `import "list"
a: [7]
b: a * 3
c: 4
d: list6c6973.Repeat([7], c)
e: list6c6973.Repeat([8], c)
f: list6c6973.Repeat([9], 5)
d: list.Repeat([7], c)
e: list.Repeat([8], c)
f: list.Repeat([9], 5)
`,
},
}
Expand All @@ -115,9 +115,9 @@ f: list6c6973.Repeat([9], 5)
if tc.simplify {
opts = append(opts, Simplify())
}
n := File(f, opts...)
File(f, opts...)

b, err := format.Node(n)
b, err := format.Node(f)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 1440b9e

Please sign in to comment.