Skip to content

Commit 1440b9e

Browse files
committed
tools/fix: avoid name-mangled imports
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 <[email protected]> Change-Id: I9bfc7082b504ac7a7d3c0a61580aef5bc8c3cd18 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1200498 Reviewed-by: Daniel Martí <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent d0617b5 commit 1440b9e

File tree

3 files changed

+38
-29
lines changed

3 files changed

+38
-29
lines changed

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ exec cue fix ./...
33
cmp p/one.cue p/one.cue.fixed
44
cmp p/two.cue p/two.cue.fixed
55
cmp p/three.cue p/three.cue.fixed
6-
# TODO: the added imports have unnecessary name-mangling. https://cuelang.org/issue/3408
76
-- p/one.cue --
87
package one
98

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

20-
import list6c6973 "list"
19+
import "list"
2120

22-
out: list6c6973.Concat([["foo"], ["bar"]])
21+
out: list.Concat([["foo"], ["bar"]])
2322
-- p/two.cue.fixed --
2423
package two
2524

26-
import list6c6973 "list"
25+
import "list"
2726

28-
out: list6c6973.Repeat(["baz"], 3)
27+
out: list.Repeat(["baz"], 3)
2928
-- p/three.cue.fixed --
30-
import list6c6973 "list"
29+
import "list"
3130

32-
out: list6c6973.Concat([["a"], (list6c6973.Concat([(list6c6973.Repeat(["a"], 7)), ["gh"]]))])
31+
out: list.Concat([["a"], (list.Concat([(list.Repeat(["a"], 7)), ["gh"]]))])

tools/fix/fix.go

+22-12
Original file line numberDiff line numberDiff line change
@@ -72,27 +72,27 @@ func File(f *ast.File, o ...Option) *ast.File {
7272
if !(xIsList || yIsList) {
7373
break
7474
}
75-
pkg := c.Import("list")
76-
if pkg == nil {
77-
break
78-
}
7975
if n.Op == token.ADD {
8076
// Rewrite list addition to use list.Concat
8177
ast.SetRelPos(x, token.NoSpace)
82-
c.Replace(&ast.CallExpr{
83-
Fun: ast.NewSel(pkg, "Concat"),
84-
Args: []ast.Expr{ast.NewList(x, y)},
85-
})
78+
c.Replace(ast.NewCall(
79+
ast.NewSel(&ast.Ident{
80+
Name: "list",
81+
Node: ast.NewImport(nil, "list"),
82+
}, "Concat"), ast.NewList(x, y)),
83+
)
8684
} else {
8785
// Rewrite list multiplication to use list.Repeat
8886
if !xIsList {
8987
x, y = y, x
9088
}
9189
ast.SetRelPos(x, token.NoSpace)
92-
c.Replace(&ast.CallExpr{
93-
Fun: ast.NewSel(pkg, "Repeat"),
94-
Args: []ast.Expr{x, y},
95-
})
90+
c.Replace(ast.NewCall(
91+
ast.NewSel(&ast.Ident{
92+
Name: "list",
93+
Node: ast.NewImport(nil, "list"),
94+
}, "Repeat"), x, y),
95+
)
9696
}
9797
}
9898
}
@@ -103,5 +103,15 @@ func File(f *ast.File, o ...Option) *ast.File {
103103
f = simplify(f)
104104
}
105105

106+
err := astutil.Sanitize(f)
107+
// TODO: this File method is public, and its signature was fixed
108+
// before we started calling Sanitize. Ideally, we want to return
109+
// this error, but that would require deprecating this File method,
110+
// and creating a new one, which might happen in due course if we
111+
// also discover that we need to be a bit more flexible than just
112+
// accepting a File.
113+
if err != nil {
114+
panic(err)
115+
}
106116
return f
107117
}

tools/fix/fix_test.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ c: a + [8]
7474
d: [9] + a
7575
e: [0] + [1]
7676
`,
77-
out: `import list6c6973 "list"
77+
out: `import "list"
7878
7979
a: [7]
8080
b: a + a
81-
c: list6c6973.Concat([a, [8]])
82-
d: list6c6973.Concat([[9], a])
83-
e: list6c6973.Concat([[0], [1]])
81+
c: list.Concat([a, [8]])
82+
d: list.Concat([[9], a])
83+
e: list.Concat([[0], [1]])
8484
`,
8585
},
8686

@@ -93,14 +93,14 @@ d: [7] * c
9393
e: c * [8]
9494
f: [9] * 5
9595
`,
96-
out: `import list6c6973 "list"
96+
out: `import "list"
9797
9898
a: [7]
9999
b: a * 3
100100
c: 4
101-
d: list6c6973.Repeat([7], c)
102-
e: list6c6973.Repeat([8], c)
103-
f: list6c6973.Repeat([9], 5)
101+
d: list.Repeat([7], c)
102+
e: list.Repeat([8], c)
103+
f: list.Repeat([9], 5)
104104
`,
105105
},
106106
}
@@ -115,9 +115,9 @@ f: list6c6973.Repeat([9], 5)
115115
if tc.simplify {
116116
opts = append(opts, Simplify())
117117
}
118-
n := File(f, opts...)
118+
File(f, opts...)
119119

120-
b, err := format.Node(n)
120+
b, err := format.Node(f)
121121
if err != nil {
122122
t.Fatal(err)
123123
}

0 commit comments

Comments
 (0)