this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

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 <matthew@cue.works>
Change-Id: I2adc92f93198e4988af7d9277b894262d0a5a8ce
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1200507
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>

+16 -12
+5 -6
cmd/cue/cmd/import.go
··· 551 551 return false 552 552 } 553 553 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), 557 557 } 558 558 559 559 // found a replaceable string 560 560 dataField := h.uniqueName(name, "_", "cue_") 561 561 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)) 565 563 566 564 // TODO: use definitions instead 567 565 c.InsertAfter(astutil.ApplyRecursively(&ast.LetClause{ ··· 571 569 } 572 570 return true 573 571 }) 572 + astutil.Sanitize(f) 574 573 } 575 574 576 575 func tryParse(str string) (s ast.Expr, pkg string) {
+6 -6
cmd/cue/cmd/testdata/script/import_hoiststr.txtar
··· 2 2 cmp stdout expect-stdout 3 3 -- expect-stdout -- 4 4 import ( 5 - json656e63 "encoding/json" 6 - yaml656e63 "encoding/yaml" 5 + "encoding/json" 6 + "encoding/yaml" 7 7 ) 8 8 9 9 foo: { 10 10 name: "something" 11 - json1: json656e63.Marshal(_cue_json1) 11 + json1: json.Marshal(_cue_json1) 12 12 let _cue_json1 = [1, 2] 13 - json2: json656e63.Marshal(_cue_json2) 13 + json2: json.Marshal(_cue_json2) 14 14 let _cue_json2 = { 15 15 key: "value" 16 16 } 17 - yaml1: yaml656e63.Marshal(_cue_yaml1) 17 + yaml1: yaml.Marshal(_cue_yaml1) 18 18 let _cue_yaml1 = { 19 19 a: "b" 20 20 c: "d" 21 21 } 22 - yaml2: yaml656e63.Marshal(_cue_yaml2) 22 + yaml2: yaml.Marshal(_cue_yaml2) 23 23 let _cue_yaml2 = [ 24 24 "one", 25 25 "two",
+5
cue/ast/astutil/apply.go
··· 46 46 // Import reports an opaque identifier that refers to the given package. It 47 47 // may only be called if the input to apply was an ast.File. If the import 48 48 // does not exist, it will be added. 49 + // 50 + // Deprecated: use [ast.NewImport] as an [ast.Ident.Node], and then 51 + // [Sanitize]. 49 52 Import(path string) *ast.Ident 50 53 51 54 // Replace replaces the current Node with n. ··· 120 123 func (c *cursor) Index() int { return c.index } 121 124 func (c *cursor) Node() ast.Node { return c.node } 122 125 126 + // Deprecated: use [ast.NewImport] as an [ast.Ident.Node], and then 127 + // [Sanitize]. 123 128 func (c *cursor) Import(importPath string) *ast.Ident { 124 129 info := fileInfo(c) 125 130 if info == nil {