From 691ee7440454ca3754c25360d294135cd9c6807f Mon Sep 17 00:00:00 2001 From: Matthew Sackman Date: Mon, 2 Sep 2024 13:11:56 +0100 Subject: [PATCH] ast/astutil: deprecate Cursor.Import MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Change-Id: I2adc92f93198e4988af7d9277b894262d0a5a8ce Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1200507 Reviewed-by: Daniel Martí TryBot-Result: CUEcueckoo Unity-Result: CUE porcuepine --- cmd/cue/cmd/import.go | 11 +++++------ cmd/cue/cmd/testdata/script/import_hoiststr.txtar | 12 ++++++------ cue/ast/astutil/apply.go | 5 +++++ 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/cmd/cue/cmd/import.go b/cmd/cue/cmd/import.go index 938d268f21e..e74227b900c 100644 --- a/cmd/cue/cmd/import.go +++ b/cmd/cue/cmd/import.go @@ -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{ @@ -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) { diff --git a/cmd/cue/cmd/testdata/script/import_hoiststr.txtar b/cmd/cue/cmd/testdata/script/import_hoiststr.txtar index af43faf98ec..ba7a9364c5b 100644 --- a/cmd/cue/cmd/testdata/script/import_hoiststr.txtar +++ b/cmd/cue/cmd/testdata/script/import_hoiststr.txtar @@ -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", diff --git a/cue/ast/astutil/apply.go b/cue/ast/astutil/apply.go index c9de28fc809..1f71faf106a 100644 --- a/cue/ast/astutil/apply.go +++ b/cue/ast/astutil/apply.go @@ -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. @@ -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 {