-
Notifications
You must be signed in to change notification settings - Fork 2
/
import.go
147 lines (130 loc) · 4 KB
/
import.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package resolver
import (
"fmt"
"path/filepath"
"strings"
"github.com/bazelbuild/buildtools/build"
sppb "github.com/stackb/scala-gazelle/build/stack/gazelle/scala/parse"
)
// TODO(pcj): rename this to 'Requirement' or something. Not all Imports are
// actually from an import statement.
// Import is used to trace import provenance.
type Import struct {
// Kind is the import type
Kind sppb.ImportKind
// Imp is the name of the import
Imp string
// File is the source file (when this is a direct import).
Source *sppb.File
// Src is the name of the parent import (when this is an implicit import)
Src string
// Symbol is the resolved symbol of the import, or nil if not resolved.
Symbol *Symbol
// Error is assiged if there is a resolution error.
Error error
}
// NewDirectImport creates a new direct import from the given file.
func NewDirectImport(imp string, source *sppb.File) *Import {
return &Import{
Kind: sppb.ImportKind_DIRECT,
Imp: imp,
Source: source,
}
}
// NewResolvedNameImport creates a new resolved import from the given file,
// name, and symbol. The 'name' is the token that resolved in the file scope.
func NewResolvedNameImport(imp string, source *sppb.File, name string, symbol *Symbol) *Import {
return &Import{
Kind: sppb.ImportKind_RESOLVED_NAME,
Imp: imp,
Source: source,
Src: name,
Symbol: symbol,
}
}
// NewTransitiveImport creates a new resolved import from the given file,
// name, and symbol. The 'name' is the token that resolved in the file scope.
func NewTransitiveImport(imp string, name string, symbol *Symbol) *Import {
return &Import{
Kind: sppb.ImportKind_TRANSITIVE,
Imp: imp,
Src: name,
Symbol: symbol,
}
}
// NewError creates a new err import from the given file,
// name, and symbol.
func NewErrorImport(imp string, source *sppb.File, src string, err error) *Import {
return &Import{
Kind: sppb.ImportKind_IMPORT_KIND_UNKNOWN,
Imp: imp,
Source: source,
Src: src,
Error: err,
}
}
// NewImplicitImport creates a new implicit import from the given parent src.
func NewImplicitImport(imp, src string) *Import {
return &Import{
Kind: sppb.ImportKind_IMPLICIT,
Imp: imp,
Src: src,
}
}
// NewExtendsImport creates a new extends import from the given requiring type.
func NewExtendsImport(imp string, source *sppb.File, src string, symbol *Symbol) *Import {
return &Import{
Kind: sppb.ImportKind_EXTENDS,
Imp: imp,
Source: source,
Src: src,
Symbol: symbol,
}
}
// NewMainClassImport creates a new main_class import.
func NewMainClassImport(imp string) *Import {
return &Import{
Kind: sppb.ImportKind_MAIN_CLASS,
Imp: imp,
}
}
func (imp *Import) Comment() build.Comment {
return build.Comment{Token: "# " + imp.String()}
}
func (imp *Import) String() string {
var impType string
emoji := "✅"
if imp.Symbol != nil {
impType = fmt.Sprintf("%v", imp.Symbol.Type)
} else if imp.Error != nil {
impType = "ERROR"
emoji = "❌"
}
parts := []string{
fmt.Sprintf("%s %s<%s>", emoji, imp.Imp, impType),
}
if imp.Symbol != nil {
to := imp.Symbol.Label.String()
if to == "//:" {
to = "NO-LABEL"
}
parts = append(parts, fmt.Sprintf("%s<%s>", to, imp.Symbol.Provider))
} else if imp.Error != nil {
parts = append(parts, fmt.Sprintf("%v", imp.Error))
}
switch imp.Kind {
case sppb.ImportKind_DIRECT:
parts = append(parts, fmt.Sprintf("(%v of %s)", imp.Kind, filepath.Base(imp.Source.Filename)))
case sppb.ImportKind_IMPLICIT:
parts = append(parts, fmt.Sprintf("(%v via %q)", imp.Kind, imp.Src))
case sppb.ImportKind_EXTENDS:
parts = append(parts, fmt.Sprintf("(%v of %s via %q)", imp.Kind, filepath.Base(imp.Source.Filename), imp.Src))
case sppb.ImportKind_RESOLVED_NAME:
parts = append(parts, fmt.Sprintf("(%v of %s via %q)", imp.Kind, filepath.Base(imp.Source.Filename), imp.Src))
case sppb.ImportKind_TRANSITIVE:
parts = append(parts, fmt.Sprintf("(%v of %s)", imp.Kind, imp.Src))
default:
parts = append(parts, fmt.Sprintf("(%v)", imp.Kind))
}
return strings.Join(parts, " ")
}