forked from microsoft/typescript-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
375 lines (340 loc) · 13.9 KB
/
cache.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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package module
import (
"strings"
"sync"
"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/compiler/packagejson"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/tspath"
)
type ModeAwareCache[T any] map[ModeAwareCacheKey]T
type caches struct {
moduleNameCache *resolutionCache[*ResolvedModule]
typeReferenceDirectiveCache *resolutionCache[*ResolvedTypeReferenceDirective]
packageJsonInfoCache *packagejson.InfoCache
resolvedTypeReferenceDirectiveLookupLocations map[*ResolvedTypeReferenceDirective]*LookupLocations
// Cached representation for `core.CompilerOptions.paths`.
// Doesn't handle other path patterns like in `typesVersions`.
parsedPatternsForPathsOnce sync.Once
parsedPatternsForPaths *parsedPatterns
}
func newCaches(
currentDirectory string,
useCaseSensitiveFileNames bool,
options *core.CompilerOptions,
) caches {
optionsToRedirectsKey := make(map[*core.CompilerOptions]string)
getOriginalOrResolvedModuleFileName := func(result *ResolvedModule) tspath.Path {
if result.OriginalPath != "" {
return tspath.ToPath(result.OriginalPath, currentDirectory, useCaseSensitiveFileNames)
}
return tspath.ToPath(result.ResolvedFileName, currentDirectory, useCaseSensitiveFileNames)
}
getOriginalOrResolvedTypeReferenceFileName := func(result *ResolvedTypeReferenceDirective) tspath.Path {
if result.OriginalPath != "" {
return tspath.ToPath(result.OriginalPath, currentDirectory, useCaseSensitiveFileNames)
}
return tspath.ToPath(result.ResolvedFileName, currentDirectory, useCaseSensitiveFileNames)
}
return caches{
moduleNameCache: newResolutionCache(currentDirectory, useCaseSensitiveFileNames, options, getOriginalOrResolvedModuleFileName, optionsToRedirectsKey, false /*isReadonly*/),
typeReferenceDirectiveCache: newResolutionCache(currentDirectory, useCaseSensitiveFileNames, options, getOriginalOrResolvedTypeReferenceFileName, optionsToRedirectsKey, false /*isReadonly*/),
packageJsonInfoCache: packagejson.NewInfoCache(currentDirectory, useCaseSensitiveFileNames),
}
}
type resolutionCache[T comparable] struct {
perDirectoryResolutionCache[T]
nonRelativeNameResolutionCache[T]
mu sync.RWMutex
lookupLocations map[T]*LookupLocations
isReadonly bool
}
func newResolutionCache[T comparable](
currentDirectory string,
useCaseSensitiveFileNames bool,
options *core.CompilerOptions,
getResolvedFileName func(result T) tspath.Path,
optionsToRedirectsKey map[*core.CompilerOptions]string,
isReadonly bool,
) *resolutionCache[T] {
return &resolutionCache[T]{
perDirectoryResolutionCache: newPerDirectoryResolutionCache[T](currentDirectory, useCaseSensitiveFileNames, options, optionsToRedirectsKey),
nonRelativeNameResolutionCache: newNonRelativeNameResolutionCache[T](currentDirectory, useCaseSensitiveFileNames, options, getResolvedFileName, optionsToRedirectsKey),
isReadonly: isReadonly,
}
}
func (c *resolutionCache[T]) getLookupLocations(resolved T) *LookupLocations {
c.mu.RLock()
defer c.mu.RUnlock()
return c.lookupLocations[resolved]
}
func (c *resolutionCache[T]) setLookupLocations(resolved T, failedLookupLocations []string, affectingLocations []string, resolutionDiagnostics []ast.Diagnostic) {
c.mu.Lock()
defer c.mu.Unlock()
if c.lookupLocations == nil {
c.lookupLocations = make(map[T]*LookupLocations)
}
c.lookupLocations[resolved] = &LookupLocations{
FailedLookupLocations: failedLookupLocations,
AffectingLocations: affectingLocations,
ResolutionDiagnostics: resolutionDiagnostics,
}
}
func (c *resolutionCache[T]) appendLookupLocations(resolved T, failedLookupLocations []string, affectingLocations []string, resolutionDiagnostics []ast.Diagnostic) {
c.mu.Lock()
defer c.mu.Unlock()
lookupLocations := c.lookupLocations[resolved]
lookupLocations.FailedLookupLocations = append(lookupLocations.FailedLookupLocations, failedLookupLocations...)
lookupLocations.AffectingLocations = append(lookupLocations.AffectingLocations, affectingLocations...)
lookupLocations.ResolutionDiagnostics = append(lookupLocations.ResolutionDiagnostics, resolutionDiagnostics...)
}
type perDirectoryResolutionCache[T any] struct {
mu sync.RWMutex
currentDirectory string
useCaseSensitiveFileNames bool
options *core.CompilerOptions
directoryToModuleNameMap *cacheWithRedirects[tspath.Path, ModeAwareCache[T]]
}
func newPerDirectoryResolutionCache[T any](
currentDirectory string,
useCaseSensitiveFileNames bool,
options *core.CompilerOptions,
optionsToRedirectsKey map[*core.CompilerOptions]string,
) perDirectoryResolutionCache[T] {
return perDirectoryResolutionCache[T]{
currentDirectory: currentDirectory,
useCaseSensitiveFileNames: useCaseSensitiveFileNames,
options: options,
directoryToModuleNameMap: newCacheWithRedirects[tspath.Path, ModeAwareCache[T]](options, optionsToRedirectsKey),
}
}
func (c *perDirectoryResolutionCache[T]) getFromDirectoryCache(nameAndMode ModeAwareCacheKey, directory string, redirectedReference *ResolvedProjectReference) (T, bool) {
c.mu.RLock()
defer c.mu.RUnlock()
result, ok := c.directoryToModuleNameMap.getMapOfCacheRedirects(redirectedReference)[tspath.ToPath(directory, c.currentDirectory, c.useCaseSensitiveFileNames)][nameAndMode]
return result, ok
}
func (c *perDirectoryResolutionCache[T]) setInDirectoryCache(nameAndMode ModeAwareCacheKey, directory string, value T, redirectedReference *ResolvedProjectReference) {
cache := c.getOrCreateCacheForDirectory(directory, redirectedReference)
c.mu.Lock()
defer c.mu.Unlock()
cache[nameAndMode] = value
}
func (c *perDirectoryResolutionCache[T]) getOrCreateCacheForDirectory(directory string, redirectedReference *ResolvedProjectReference) ModeAwareCache[T] {
c.mu.RLock()
cache := c.directoryToModuleNameMap.getOrCreateMapOfCacheRedirects(redirectedReference)
key := tspath.ToPath(directory, c.currentDirectory, c.useCaseSensitiveFileNames)
result, ok := cache[key]
c.mu.RUnlock()
if ok {
return result
}
c.mu.Lock()
defer c.mu.Unlock()
result = make(ModeAwareCache[T])
cache[key] = result
return result
}
type nonRelativeNameResolutionCache[T any] struct {
mu sync.RWMutex
currentDirectory string
useCaseSensitiveFileNames bool
options *core.CompilerOptions
getResolvedFileName func(result T) tspath.Path
moduleNameToDirectoryMap *cacheWithRedirects[ModeAwareCacheKey, *perNonRelativeNameCache[T]]
}
func newNonRelativeNameResolutionCache[T any](
currentDirectory string,
useCaseSensitiveFileNames bool,
options *core.CompilerOptions,
getResolvedFileName func(result T) tspath.Path,
optionsToRedirectsKey map[*core.CompilerOptions]string,
) nonRelativeNameResolutionCache[T] {
return nonRelativeNameResolutionCache[T]{
currentDirectory: currentDirectory,
useCaseSensitiveFileNames: useCaseSensitiveFileNames,
options: options,
getResolvedFileName: getResolvedFileName,
moduleNameToDirectoryMap: newCacheWithRedirects[ModeAwareCacheKey, *perNonRelativeNameCache[T]](options, optionsToRedirectsKey),
}
}
func (c *nonRelativeNameResolutionCache[T]) getFromNonRelativeNameCache(nameAndMode ModeAwareCacheKey, directoryName string, redirectedReference *ResolvedProjectReference) (T, bool) {
if tspath.IsExternalModuleNameRelative(nameAndMode.Name) {
panic("module name must be non-relative")
}
c.mu.RLock()
defer c.mu.RUnlock()
return c.moduleNameToDirectoryMap.getMapOfCacheRedirects(redirectedReference)[nameAndMode].get(tspath.ToPath(directoryName, c.currentDirectory, c.useCaseSensitiveFileNames))
}
func (c *nonRelativeNameResolutionCache[T]) setInNonRelativeNameCache(nameAndMode ModeAwareCacheKey, directoryName string, value T, redirectedReference *ResolvedProjectReference) {
if tspath.IsExternalModuleNameRelative(nameAndMode.Name) {
panic("module name must be non-relative")
}
cache := c.getOrCreateCacheForNonRelativeName(nameAndMode, redirectedReference)
c.mu.Lock()
defer c.mu.Unlock()
cache.set(tspath.ToPath(directoryName, c.currentDirectory, c.useCaseSensitiveFileNames), value, c.getResolvedFileName)
}
func (c *nonRelativeNameResolutionCache[T]) getOrCreateCacheForNonRelativeName(nameAndMode ModeAwareCacheKey, redirectedReference *ResolvedProjectReference) *perNonRelativeNameCache[T] {
if tspath.IsExternalModuleNameRelative(nameAndMode.Name) {
panic("module name must be non-relative")
}
c.mu.RLock()
cache := c.moduleNameToDirectoryMap.getOrCreateMapOfCacheRedirects(redirectedReference)
result, ok := cache[nameAndMode]
c.mu.RUnlock()
if ok {
return result
}
c.mu.Lock()
defer c.mu.Unlock()
result = &perNonRelativeNameCache[T]{}
cache[nameAndMode] = result
return result
}
type perNonRelativeNameCache[T any] struct {
directoryPathMap map[tspath.Path]T
}
func (c *perNonRelativeNameCache[T]) get(directory tspath.Path) (result T, ok bool) {
if c == nil {
return result, ok
}
result, ok = c.directoryPathMap[directory]
return result, ok
}
// At first this function add entry directory -> module resolution result to the table.
// Then it computes the set of parent folders for 'directory' that should have the same module resolution result
// and for every parent folder in set it adds entry: parent -> module resolution. .
// Lets say we first directory name: `/a/b/c/d/e` and resolution result is: `/a/b/bar.ts`.
// Set of parent folders that should have the same result will be:
//
// [
// /a/b/c/d, /a/b/c, /a/b
// ]
//
// this means that request for module resolution from file in any of these folder will be immediately found in cache.
func (c *perNonRelativeNameCache[T]) set(directory tspath.Path, value T, getResolvedFileName func(result T) tspath.Path) {
// if entry is already in cache do nothing
if _, ok := c.directoryPathMap[directory]; ok {
return
}
if c.directoryPathMap == nil {
c.directoryPathMap = make(map[tspath.Path]T)
}
c.directoryPathMap[directory] = value
resolvedFileName := getResolvedFileName(value)
// find common prefix between directory and resolved file name
// this common prefix should be the shortest path that has the same resolution
// directory: /a/b/c/d/e
// resolvedFileName: /a/b/foo.d.ts
// commonPrefix: /a/b
// for failed lookups cache the result for every directory up to root
commonPrefix := getCommonPrefix(directory, resolvedFileName)
current := directory
for current != commonPrefix {
parent := current.GetDirectoryPath()
if parent == current {
break
}
if _, ok := c.directoryPathMap[parent]; ok {
break
}
c.directoryPathMap[parent] = value
current = parent
}
}
type cacheWithRedirects[K comparable, V any] struct {
redirectsMap map[*core.CompilerOptions]map[K]V
redirectsKeyToMap map[string]map[K]V
optionsToRedirectsKey map[*core.CompilerOptions]string
ownOptions *core.CompilerOptions
ownMap map[K]V
}
func newCacheWithRedirects[K comparable, V any](ownOptions *core.CompilerOptions, optionsToRedirectsKey map[*core.CompilerOptions]string) *cacheWithRedirects[K, V] {
cache := &cacheWithRedirects[K, V]{
redirectsMap: make(map[*core.CompilerOptions]map[K]V),
redirectsKeyToMap: make(map[string]map[K]V),
optionsToRedirectsKey: optionsToRedirectsKey,
ownOptions: ownOptions,
ownMap: make(map[K]V),
}
if ownOptions != nil {
cache.redirectsMap[ownOptions] = cache.ownMap
}
return cache
}
func (c *cacheWithRedirects[K, V]) getMapOfCacheRedirects(redirectedReference *ResolvedProjectReference) map[K]V {
if redirectedReference == nil {
return c.ownMap
}
return c.getOrCreateMap(redirectedReference.CommandLine.CompilerOptions, false /*create*/)
}
func (c *cacheWithRedirects[K, V]) getOrCreateMapOfCacheRedirects(redirectedReference *ResolvedProjectReference) map[K]V {
if redirectedReference == nil {
return c.ownMap
}
return c.getOrCreateMap(redirectedReference.CommandLine.CompilerOptions, true /*create*/)
}
func (c *cacheWithRedirects[K, V]) getOrCreateMap(redirectOptions *core.CompilerOptions, create bool) map[K]V {
if result, ok := c.redirectsMap[redirectOptions]; ok {
return result
}
key := c.getRedirectsCacheKey(redirectOptions)
var result map[K]V
if result = c.redirectsKeyToMap[key]; result == nil {
if c.ownOptions != nil {
ownKey := c.getRedirectsCacheKey(c.ownOptions)
if ownKey == key {
result = c.ownMap
} else if _, ok := c.redirectsKeyToMap[ownKey]; !ok {
c.redirectsKeyToMap[ownKey] = c.ownMap
}
}
if result == nil && create {
result = make(map[K]V)
}
if result != nil {
c.redirectsKeyToMap[key] = result
}
}
if result != nil {
c.redirectsMap[redirectOptions] = result
}
return result
}
func (c *cacheWithRedirects[K, V]) getRedirectsCacheKey(options *core.CompilerOptions) string {
if key, ok := c.optionsToRedirectsKey[options]; ok {
return key
}
result := getModuleResolutionAffectingOptionsKey(options)
c.optionsToRedirectsKey[options] = result
return result
}
func getModuleResolutionAffectingOptionsKey(options *core.CompilerOptions) string {
// !!! TODO(andrewbranch) Real implementation depends on command line parser
return string(options.ModuleResolution)
}
func getCommonPrefix(directory tspath.Path, resolution tspath.Path) tspath.Path {
if resolution == "" {
return resolution
}
resolutionDirectory := resolution.GetDirectoryPath()
// find first position where directory and resolution differs
i := 0
limit := min(len(directory), len(resolutionDirectory))
for i < limit && directory[i] == resolutionDirectory[i] {
i++
}
if i == len(directory) && (len(resolutionDirectory) == i || resolutionDirectory[i] == '/') {
return directory
}
rootLength := tspath.GetRootLength(string(directory))
if i < rootLength {
return tspath.Path("")
}
sep := strings.LastIndexByte(string(directory[:i]), '/')
if sep == -1 {
return tspath.Path("")
}
return directory[:max(sep, rootLength)]
}