forked from microsoft/typescript-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreferenceresolver.go
More file actions
251 lines (225 loc) · 9.1 KB
/
referenceresolver.go
File metadata and controls
251 lines (225 loc) · 9.1 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package binder
import (
"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/diagnostics"
)
type ReferenceResolver interface {
GetReferencedExportContainer(node *ast.IdentifierNode, prefixLocals bool) *ast.Node
GetReferencedImportDeclaration(node *ast.IdentifierNode) *ast.Declaration
GetReferencedValueDeclaration(node *ast.IdentifierNode) *ast.Declaration
GetReferencedValueDeclarations(node *ast.IdentifierNode) []*ast.Declaration
GetElementAccessExpressionName(expression *ast.ElementAccessExpression) string
}
type ReferenceResolverHooks struct {
ResolveName func(location *ast.Node, name string, meaning ast.SymbolFlags, nameNotFoundMessage *diagnostics.Message, isUse bool, excludeGlobals bool) *ast.Symbol
GetResolvedSymbol func(*ast.Node) *ast.Symbol
GetMergedSymbol func(*ast.Symbol) *ast.Symbol
GetParentOfSymbol func(*ast.Symbol) *ast.Symbol
GetSymbolOfDeclaration func(*ast.Declaration) *ast.Symbol
GetTypeOnlyAliasDeclaration func(symbol *ast.Symbol, include ast.SymbolFlags) *ast.Declaration
GetExportSymbolOfValueSymbolIfExported func(*ast.Symbol) *ast.Symbol
GetElementAccessExpressionName func(*ast.ElementAccessExpression) (string, bool)
}
var _ ReferenceResolver = &referenceResolver{}
type referenceResolver struct {
resolver *NameResolver
options *core.CompilerOptions
hooks ReferenceResolverHooks
}
func NewReferenceResolver(options *core.CompilerOptions, hooks ReferenceResolverHooks) ReferenceResolver {
return &referenceResolver{
options: options,
hooks: hooks,
}
}
func (r *referenceResolver) getResolvedSymbol(node *ast.Node) *ast.Symbol {
if node != nil {
if r.hooks.GetResolvedSymbol != nil {
return r.hooks.GetResolvedSymbol(node)
}
}
return nil
}
func (r *referenceResolver) getMergedSymbol(symbol *ast.Symbol) *ast.Symbol {
if symbol != nil {
if r.hooks.GetMergedSymbol != nil {
return r.hooks.GetMergedSymbol(symbol)
}
return symbol
}
return nil
}
func (r *referenceResolver) getParentOfSymbol(symbol *ast.Symbol) *ast.Symbol {
if symbol != nil {
if r.hooks.GetParentOfSymbol != nil {
return r.hooks.GetParentOfSymbol(symbol)
}
return symbol.Parent
}
return nil
}
func (r *referenceResolver) getSymbolOfDeclaration(declaration *ast.Declaration) *ast.Symbol {
if declaration != nil {
if r.hooks.GetSymbolOfDeclaration != nil {
return r.hooks.GetSymbolOfDeclaration(declaration)
}
return declaration.Symbol()
}
return nil
}
func (r *referenceResolver) getReferencedValueSymbol(reference *ast.IdentifierNode, startInDeclarationContainer bool) *ast.Symbol {
resolvedSymbol := r.getResolvedSymbol(reference)
if resolvedSymbol != nil {
return resolvedSymbol
}
location := reference
if startInDeclarationContainer && reference.Parent != nil && ast.IsDeclaration(reference.Parent) && reference.Parent.Name() == reference {
location = ast.GetDeclarationContainer(reference.Parent)
}
if r.hooks.ResolveName != nil {
return r.hooks.ResolveName(location, reference.Text(), ast.SymbolFlagsExportValue|ast.SymbolFlagsValue|ast.SymbolFlagsAlias, nil /*nameNotFoundMessage*/, false /*isUse*/, false /*excludeGlobals*/)
}
if r.resolver == nil {
r.resolver = &NameResolver{
CompilerOptions: r.options,
}
}
return r.resolver.Resolve(location, reference.Text(), ast.SymbolFlagsExportValue|ast.SymbolFlagsValue|ast.SymbolFlagsAlias, nil /*nameNotFoundMessage*/, false /*isUse*/, false /*excludeGlobals*/)
}
func (r *referenceResolver) isTypeOnlyAliasDeclaration(symbol *ast.Symbol) bool {
if symbol != nil {
if r.hooks.GetTypeOnlyAliasDeclaration != nil {
return r.hooks.GetTypeOnlyAliasDeclaration(symbol, ast.SymbolFlagsValue) != nil
}
node := r.getDeclarationOfAliasSymbol(symbol)
for node != nil {
switch node.Kind {
case ast.KindImportEqualsDeclaration, ast.KindExportDeclaration:
return node.IsTypeOnly()
case ast.KindImportClause, ast.KindImportSpecifier, ast.KindExportSpecifier:
if node.IsTypeOnly() {
return true
}
node = node.Parent
continue
case ast.KindNamedImports, ast.KindNamedExports:
node = node.Parent
continue
}
break
}
}
return false
}
func (r *referenceResolver) getDeclarationOfAliasSymbol(symbol *ast.Symbol) *ast.Declaration {
return core.FindLast(symbol.Declarations, ast.IsAliasSymbolDeclaration)
}
func (r *referenceResolver) getExportSymbolOfValueSymbolIfExported(symbol *ast.Symbol) *ast.Symbol {
if symbol != nil {
if r.hooks.GetExportSymbolOfValueSymbolIfExported != nil {
return r.hooks.GetExportSymbolOfValueSymbolIfExported(symbol)
}
if symbol.Flags&ast.SymbolFlagsExportValue != 0 && symbol.ExportSymbol != nil {
symbol = symbol.ExportSymbol
}
return r.getMergedSymbol(symbol)
}
return nil
}
func (r *referenceResolver) GetReferencedExportContainer(node *ast.IdentifierNode, prefixLocals bool) *ast.Node /*SourceFile|ModuleDeclaration|EnumDeclaration*/ {
// When resolving the export for the name of a module or enum
// declaration, we need to start resolution at the declaration's container.
// Otherwise, we could incorrectly resolve the export as the
// declaration if it contains an exported member with the same name.
startInDeclarationContainer := node.Parent != nil && (node.Parent.Kind == ast.KindModuleDeclaration || node.Parent.Kind == ast.KindEnumDeclaration) && node == node.Parent.Name()
if symbol := r.getReferencedValueSymbol(node, startInDeclarationContainer); symbol != nil {
if symbol.Flags&ast.SymbolFlagsExportValue != 0 {
// If we reference an exported entity within the same module declaration, then whether
// we prefix depends on the kind of entity. SymbolFlags.ExportHasLocal encompasses all the
// kinds that we do NOT prefix.
exportSymbol := r.getMergedSymbol(symbol.ExportSymbol)
if !prefixLocals && exportSymbol.Flags&ast.SymbolFlagsExportHasLocal != 0 && exportSymbol.Flags&ast.SymbolFlagsVariable == 0 {
return nil
}
symbol = exportSymbol
}
parentSymbol := r.getParentOfSymbol(symbol)
if parentSymbol != nil {
if parentSymbol.Flags&ast.SymbolFlagsValueModule != 0 && parentSymbol.ValueDeclaration != nil && parentSymbol.ValueDeclaration.Kind == ast.KindSourceFile {
symbolFile := parentSymbol.ValueDeclaration.AsSourceFile()
referenceFile := ast.GetSourceFileOfNode(node)
// If `node` accesses an export and that export isn't in the same file, then symbol is a namespace export, so return nil.
symbolIsUmdExport := symbolFile != referenceFile
if symbolIsUmdExport {
return nil
}
return symbolFile.AsNode()
}
isMatchingContainer := func(n *ast.Node) bool {
return (n.Kind == ast.KindModuleDeclaration || n.Kind == ast.KindEnumDeclaration) && r.getSymbolOfDeclaration(n) == parentSymbol
}
if container := ast.FindAncestor(symbol.ValueDeclaration, isMatchingContainer); container != nil {
return container
}
return ast.FindAncestor(node.Parent, isMatchingContainer)
}
}
return nil
}
func (r *referenceResolver) GetReferencedImportDeclaration(node *ast.IdentifierNode) *ast.Declaration {
if symbol := r.getReferencedValueSymbol(node, false /*startInDeclarationContainer*/); symbol != nil {
// We should only get the declaration of an alias if there isn't a local value
// declaration for the symbol
if ast.IsNonLocalAlias(symbol, ast.SymbolFlagsValue /*excludes*/) && !r.isTypeOnlyAliasDeclaration(symbol) {
return r.getDeclarationOfAliasSymbol(symbol)
}
}
return nil
}
func (r *referenceResolver) GetReferencedValueDeclaration(node *ast.IdentifierNode) *ast.Declaration {
if symbol := r.getReferencedValueSymbol(node, false /*startInDeclarationContainer*/); symbol != nil {
return r.getExportSymbolOfValueSymbolIfExported(symbol).ValueDeclaration
}
return nil
}
func (r *referenceResolver) GetReferencedValueDeclarations(node *ast.IdentifierNode) []*ast.Declaration {
var declarations []*ast.Declaration
if symbol := r.getReferencedValueSymbol(node, false /*startInDeclarationContainer*/); symbol != nil {
symbol = r.getExportSymbolOfValueSymbolIfExported(symbol)
for _, declaration := range symbol.Declarations {
switch declaration.Kind {
case ast.KindVariableDeclaration,
ast.KindParameter,
ast.KindBindingElement,
ast.KindPropertyDeclaration,
ast.KindPropertyAssignment,
ast.KindShorthandPropertyAssignment,
ast.KindEnumMember,
ast.KindObjectLiteralExpression,
ast.KindFunctionDeclaration,
ast.KindFunctionExpression,
ast.KindArrowFunction,
ast.KindClassDeclaration,
ast.KindClassExpression,
ast.KindEnumDeclaration,
ast.KindMethodDeclaration,
ast.KindGetAccessor,
ast.KindSetAccessor,
ast.KindModuleDeclaration:
declarations = append(declarations, declaration)
}
}
}
return declarations
}
func (r *referenceResolver) GetElementAccessExpressionName(expression *ast.ElementAccessExpression) string {
if expression != nil {
if r.hooks.GetElementAccessExpressionName != nil {
if name, ok := r.hooks.GetElementAccessExpressionName(expression); ok {
return name
}
}
}
return ""
}