src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

source.go (2307B)


      1 // Copyright 2024 The Go Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style
      3 // license that can be found in the LICENSE file.
      4 
      5 package imports
      6 
      7 import "context"
      8 
      9 // These types document the APIs below.
     10 //
     11 // TODO(rfindley): consider making these defined types rather than aliases.
     12 type (
     13 	ImportPath  = string
     14 	PackageName = string
     15 	Symbol      = string
     16 
     17 	// References is set of References found in a Go file. The first map key is the
     18 	// left hand side of a selector expression, the second key is the right hand
     19 	// side, and the value should always be true.
     20 	References = map[PackageName]map[Symbol]bool
     21 )
     22 
     23 // A Result satisfies a missing import.
     24 //
     25 // The Import field describes the missing import spec, and the Package field
     26 // summarizes the package exports.
     27 type Result struct {
     28 	Import  *ImportInfo
     29 	Package *PackageInfo
     30 }
     31 
     32 // An ImportInfo represents a single import statement.
     33 type ImportInfo struct {
     34 	ImportPath string // import path, e.g. "crypto/rand".
     35 	Name       string // import name, e.g. "crand", or "" if none.
     36 }
     37 
     38 // A PackageInfo represents what's known about a package.
     39 type PackageInfo struct {
     40 	Name    string          // package name in the package declaration, if known
     41 	Exports map[string]bool // set of names of known package level sortSymbols
     42 }
     43 
     44 // A Source provides imports to satisfy unresolved references in the file being
     45 // fixed.
     46 type Source interface {
     47 	// LoadPackageNames queries PackageName information for the requested import
     48 	// paths, when operating from the provided srcDir.
     49 	//
     50 	// TODO(rfindley): try to refactor to remove this operation.
     51 	LoadPackageNames(ctx context.Context, srcDir string, paths []ImportPath) (map[ImportPath]PackageName, error)
     52 
     53 	// ResolveReferences asks the Source for the best package name to satisfy
     54 	// each of the missing references, in the context of fixing the given
     55 	// filename.
     56 	//
     57 	// Returns a map from package name to a [Result] for that package name that
     58 	// provides the required symbols. Keys may be omitted in the map if no
     59 	// candidates satisfy all missing references for that package name. It is up
     60 	// to each data source to select the best result for each entry in the
     61 	// missing map.
     62 	ResolveReferences(ctx context.Context, filename string, missing References) ([]*Result, error)
     63 }