summaryrefslogtreecommitdiff
path: root/vendor/golang.org/x/tools/internal/imports/source.go
blob: cbe4f3c5ba1deeffa34e53b730f9f15bcf02f55a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package imports

import "context"

// These types document the APIs below.
//
// TODO(rfindley): consider making these defined types rather than aliases.
type (
	ImportPath  = string
	PackageName = string
	Symbol      = string

	// References is set of References found in a Go file. The first map key is the
	// left hand side of a selector expression, the second key is the right hand
	// side, and the value should always be true.
	References = map[PackageName]map[Symbol]bool
)

// A Result satisfies a missing import.
//
// The Import field describes the missing import spec, and the Package field
// summarizes the package exports.
type Result struct {
	Import  *ImportInfo
	Package *PackageInfo
}

// An ImportInfo represents a single import statement.
type ImportInfo struct {
	ImportPath string // import path, e.g. "crypto/rand".
	Name       string // import name, e.g. "crand", or "" if none.
}

// A PackageInfo represents what's known about a package.
type PackageInfo struct {
	Name    string          // package name in the package declaration, if known
	Exports map[string]bool // set of names of known package level sortSymbols
}

// A Source provides imports to satisfy unresolved references in the file being
// fixed.
type Source interface {
	// LoadPackageNames queries PackageName information for the requested import
	// paths, when operating from the provided srcDir.
	//
	// TODO(rfindley): try to refactor to remove this operation.
	LoadPackageNames(ctx context.Context, srcDir string, paths []ImportPath) (map[ImportPath]PackageName, error)

	// ResolveReferences asks the Source for the best package name to satisfy
	// each of the missing references, in the context of fixing the given
	// filename.
	//
	// Returns a map from package name to a [Result] for that package name that
	// provides the required symbols. Keys may be omitted in the map if no
	// candidates satisfy all missing references for that package name. It is up
	// to each data source to select the best result for each entry in the
	// missing map.
	ResolveReferences(ctx context.Context, filename string, missing References) ([]*Result, error)
}