diff --git a/cue/ast/ast.go b/cue/ast/ast.go index d24b83550..a23671edd 100644 --- a/cue/ast/ast.go +++ b/cue/ast/ast.go @@ -1093,10 +1093,6 @@ type File struct { Filename string Decls []Decl // top-level declarations; or nil - // Deprecated: use [File.ImportSpecs]. - // TODO(mvdan): remove in mid 2026. - Imports []*ImportSpec // imports in this file - Unresolved []*Ident // unresolved identifiers in this file // TODO remove this field: it's here as a temporary diff --git a/cue/ast/astutil/sanitize.go b/cue/ast/astutil/sanitize.go index ae407fdd7..84538601e 100644 --- a/cue/ast/astutil/sanitize.go +++ b/cue/ast/astutil/sanitize.go @@ -17,6 +17,7 @@ package astutil import ( "fmt" "math/rand/v2" + "slices" "strings" "cuelang.org/go/cue/ast" @@ -171,19 +172,12 @@ func (z *sanitizer) markUsed(s *scope, n *ast.Ident) bool { } func (z *sanitizer) cleanImports() { - var fileImports []*ast.ImportSpec for decl := range z.file.ImportDecls() { - newLen := 0 - for _, spec := range decl.Specs { - if _, ok := z.referenced[spec]; ok { - fileImports = append(fileImports, spec) - decl.Specs[newLen] = spec - newLen++ - } - } - decl.Specs = decl.Specs[:newLen] + decl.Specs = slices.DeleteFunc(decl.Specs, func(spec *ast.ImportSpec) bool { + _, ok := z.referenced[spec] + return !ok + }) } - z.file.Imports = fileImports // Ensure that the first import always starts a new section // so that if the file has a comment, it won't be associated with // the import comment rather than the file. diff --git a/cue/build/instance_test.go b/cue/build/instance_test.go index 9d046969c..4e63a7a6b 100644 --- a/cue/build/instance_test.go +++ b/cue/build/instance_test.go @@ -44,7 +44,6 @@ func TestPartiallyResolved(t *testing.T) { Expr: &ast.Ident{Name: "bar", Node: spec2}, }, }, - Imports: []*ast.ImportSpec{spec1, spec2}, } p := &Instance{ diff --git a/cue/parser/parser.go b/cue/parser/parser.go index d344bdc33..e53b11412 100644 --- a/cue/parser/parser.go +++ b/cue/parser/parser.go @@ -72,8 +72,6 @@ type parser struct { // Non-syntactic parser control exprLev int // < 0: in control clause, >= 0: in expression - - imports []*ast.ImportSpec // list of imports } func (p *parser) init(filename string, src []byte, opts []Option) { @@ -1860,7 +1858,6 @@ func (p *parser) parseImportSpec(_ int) *ast.ImportSpec { Path: &ast.BasicLit{ValuePos: pos, Kind: token.STRING, Value: path}, } c.closeNode(p, spec) - p.imports = append(p.imports, spec) return spec } @@ -1978,7 +1975,6 @@ func (p *parser) parseFile() *ast.File { p.closeList() f := &ast.File{ - Imports: p.imports, Decls: decls, LanguageVersion: p.cfg.Version, } diff --git a/internal/astinternal/testdata/debugprint/file.txtar b/internal/astinternal/testdata/debugprint/file.txtar index 0abdcbcd6..5901f0ec2 100644 --- a/internal/astinternal/testdata/debugprint/file.txtar +++ b/internal/astinternal/testdata/debugprint/file.txtar @@ -100,33 +100,6 @@ import ( Rparen: token.Pos("imports.cue:8:1", newline) } } - Imports: []*ast.ImportSpec{ - { - Path: *ast.BasicLit{ - ValuePos: token.Pos("imports.cue:3:8", blank) - Kind: token.Token("STRING") - Value: "\"foo\"" - } - } - { - Path: *ast.BasicLit{ - ValuePos: token.Pos("imports.cue:6:2", newline) - Kind: token.Token("STRING") - Value: "\"bar\"" - } - } - { - Name: *ast.Ident{ - NamePos: token.Pos("imports.cue:7:2", newline) - Name: "name" - } - Path: *ast.BasicLit{ - ValuePos: token.Pos("imports.cue:7:7", blank) - Kind: token.Token("STRING") - Value: "\"baz\"" - } - } - } LanguageVersion: "v0.13.0" } -- out/debugprint/empty.cue/omitempty-strings -- @@ -199,25 +172,5 @@ import ( } } } - Imports: []*ast.ImportSpec{ - { - Path: *ast.BasicLit{ - Value: "\"foo\"" - } - } - { - Path: *ast.BasicLit{ - Value: "\"bar\"" - } - } - { - Name: *ast.Ident{ - Name: "name" - } - Path: *ast.BasicLit{ - Value: "\"baz\"" - } - } - } LanguageVersion: "v0.13.0" }