Skip to content

Commit

Permalink
ast/astutil: deprecate Cursor.Import
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
cuematthew committed Sep 3, 2024
1 parent 1e65cee commit 691ee74
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
11 changes: 5 additions & 6 deletions cmd/cue/cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,17 +551,15 @@ func (h *hoister) hoist(f *ast.File) {
return false
}

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

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

f.Value = ast.NewCall(
ast.NewSel(pkg, "Marshal"),
ast.NewIdent(dataField))
f.Value = ast.NewCall(ast.NewSel(importIdent, "Marshal"), ast.NewIdent(dataField))

// TODO: use definitions instead
c.InsertAfter(astutil.ApplyRecursively(&ast.LetClause{
Expand All @@ -571,6 +569,7 @@ func (h *hoister) hoist(f *ast.File) {
}
return true
})
astutil.Sanitize(f)
}

func tryParse(str string) (s ast.Expr, pkg string) {
Expand Down
12 changes: 6 additions & 6 deletions cmd/cue/cmd/testdata/script/import_hoiststr.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ exec cue import -o - --recursive ./import/data.json
cmp stdout expect-stdout
-- expect-stdout --
import (
json656e63 "encoding/json"
yaml656e63 "encoding/yaml"
"encoding/json"
"encoding/yaml"
)

foo: {
name: "something"
json1: json656e63.Marshal(_cue_json1)
json1: json.Marshal(_cue_json1)
let _cue_json1 = [1, 2]
json2: json656e63.Marshal(_cue_json2)
json2: json.Marshal(_cue_json2)
let _cue_json2 = {
key: "value"
}
yaml1: yaml656e63.Marshal(_cue_yaml1)
yaml1: yaml.Marshal(_cue_yaml1)
let _cue_yaml1 = {
a: "b"
c: "d"
}
yaml2: yaml656e63.Marshal(_cue_yaml2)
yaml2: yaml.Marshal(_cue_yaml2)
let _cue_yaml2 = [
"one",
"two",
Expand Down
5 changes: 5 additions & 0 deletions cue/ast/astutil/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ type Cursor interface {
// Import reports an opaque identifier that refers to the given package. It
// may only be called if the input to apply was an ast.File. If the import
// does not exist, it will be added.
//
// Deprecated: use [ast.NewImport] as an [ast.Ident.Node], and then
// [Sanitize].
Import(path string) *ast.Ident

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

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

0 comments on commit 691ee74

Please sign in to comment.