From 1440b9eae7fa9ea40133418df0e5bacdcca29cb4 Mon Sep 17 00:00:00 2001 From: Matthew Sackman Date: Mon, 2 Sep 2024 11:27:38 +0100 Subject: [PATCH] tools/fix: avoid name-mangled imports MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Change-Id: I9bfc7082b504ac7a7d3c0a61580aef5bc8c3cd18 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1200498 Reviewed-by: Daniel Martí TryBot-Result: CUEcueckoo --- cmd/cue/cmd/testdata/script/fix.txtar | 13 +++++----- tools/fix/fix.go | 34 +++++++++++++++++---------- tools/fix/fix_test.go | 20 ++++++++-------- 3 files changed, 38 insertions(+), 29 deletions(-) diff --git a/cmd/cue/cmd/testdata/script/fix.txtar b/cmd/cue/cmd/testdata/script/fix.txtar index 415e2d2d7..9221c2f64 100644 --- a/cmd/cue/cmd/testdata/script/fix.txtar +++ b/cmd/cue/cmd/testdata/script/fix.txtar @@ -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 @@ -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"]]))]) \ No newline at end of file +out: list.Concat([["a"], (list.Concat([(list.Repeat(["a"], 7)), ["gh"]]))]) \ No newline at end of file diff --git a/tools/fix/fix.go b/tools/fix/fix.go index a3def92dc..9790c3274 100644 --- a/tools/fix/fix.go +++ b/tools/fix/fix.go @@ -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), + ) } } } @@ -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 } diff --git a/tools/fix/fix_test.go b/tools/fix/fix_test.go index ae559489c..233bbe6aa 100644 --- a/tools/fix/fix_test.go +++ b/tools/fix/fix_test.go @@ -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]]) `, }, @@ -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) `, }, } @@ -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) }