Skip to content

Commit 691ee74

Browse files
committed
ast/astutil: deprecate Cursor.Import
Cursor.Import always creates name-mangled imports to avoid collisions. It was created before astutil.Sanitize, which does a much better job of detecting whether name manglation is required or not. Remove the one remaining use of Cursor.Import, in cmd/import.go; Mark the Cursor.Import API as deprecated. Sadly, it's a public API so we can't just remove it. Signed-off-by: Matthew Sackman <[email protected]> Change-Id: I2adc92f93198e4988af7d9277b894262d0a5a8ce Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1200507 Reviewed-by: Daniel Martí <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent 1e65cee commit 691ee74

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

cmd/cue/cmd/import.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -551,17 +551,15 @@ func (h *hoister) hoist(f *ast.File) {
551551
return false
552552
}
553553

554-
pkg := c.Import("encoding/" + enc)
555-
if pkg == nil {
556-
return false
554+
importIdent := &ast.Ident{
555+
Name: enc,
556+
Node: ast.NewImport(nil, "encoding/"+enc),
557557
}
558558

559559
// found a replaceable string
560560
dataField := h.uniqueName(name, "_", "cue_")
561561

562-
f.Value = ast.NewCall(
563-
ast.NewSel(pkg, "Marshal"),
564-
ast.NewIdent(dataField))
562+
f.Value = ast.NewCall(ast.NewSel(importIdent, "Marshal"), ast.NewIdent(dataField))
565563

566564
// TODO: use definitions instead
567565
c.InsertAfter(astutil.ApplyRecursively(&ast.LetClause{
@@ -571,6 +569,7 @@ func (h *hoister) hoist(f *ast.File) {
571569
}
572570
return true
573571
})
572+
astutil.Sanitize(f)
574573
}
575574

576575
func tryParse(str string) (s ast.Expr, pkg string) {

cmd/cue/cmd/testdata/script/import_hoiststr.txtar

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@ exec cue import -o - --recursive ./import/data.json
22
cmp stdout expect-stdout
33
-- expect-stdout --
44
import (
5-
json656e63 "encoding/json"
6-
yaml656e63 "encoding/yaml"
5+
"encoding/json"
6+
"encoding/yaml"
77
)
88

99
foo: {
1010
name: "something"
11-
json1: json656e63.Marshal(_cue_json1)
11+
json1: json.Marshal(_cue_json1)
1212
let _cue_json1 = [1, 2]
13-
json2: json656e63.Marshal(_cue_json2)
13+
json2: json.Marshal(_cue_json2)
1414
let _cue_json2 = {
1515
key: "value"
1616
}
17-
yaml1: yaml656e63.Marshal(_cue_yaml1)
17+
yaml1: yaml.Marshal(_cue_yaml1)
1818
let _cue_yaml1 = {
1919
a: "b"
2020
c: "d"
2121
}
22-
yaml2: yaml656e63.Marshal(_cue_yaml2)
22+
yaml2: yaml.Marshal(_cue_yaml2)
2323
let _cue_yaml2 = [
2424
"one",
2525
"two",

cue/ast/astutil/apply.go

+5
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ type Cursor interface {
4646
// Import reports an opaque identifier that refers to the given package. It
4747
// may only be called if the input to apply was an ast.File. If the import
4848
// does not exist, it will be added.
49+
//
50+
// Deprecated: use [ast.NewImport] as an [ast.Ident.Node], and then
51+
// [Sanitize].
4952
Import(path string) *ast.Ident
5053

5154
// Replace replaces the current Node with n.
@@ -120,6 +123,8 @@ func (c *cursor) Parent() Cursor { return c.parent }
120123
func (c *cursor) Index() int { return c.index }
121124
func (c *cursor) Node() ast.Node { return c.node }
122125

126+
// Deprecated: use [ast.NewImport] as an [ast.Ident.Node], and then
127+
// [Sanitize].
123128
func (c *cursor) Import(importPath string) *ast.Ident {
124129
info := fileInfo(c)
125130
if info == nil {

0 commit comments

Comments
 (0)