-
Notifications
You must be signed in to change notification settings - Fork 583
/
Copy pathcache.go
149 lines (132 loc) · 3.93 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
package packagejson
import (
"sync"
"github.com/microsoft/typescript-go/internal/collections"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/diagnostics"
"github.com/microsoft/typescript-go/internal/semver"
"github.com/microsoft/typescript-go/internal/tspath"
)
var typeScriptVersion = semver.MustParse(core.Version)
type PackageJson struct {
Fields
versionPaths VersionPaths
once sync.Once
}
func (p *PackageJson) GetVersionPaths(trace func(string)) VersionPaths {
p.once.Do(func() {
if p.TypesVersions.Type == JSONValueTypeNotPresent {
if trace != nil {
trace(diagnostics.X_package_json_does_not_have_a_0_field.Format("typesVersions"))
}
return
}
if p.TypesVersions.Type != JSONValueTypeObject {
if trace != nil {
trace(diagnostics.Expected_type_of_0_field_in_package_json_to_be_1_got_2.Format("typesVersions", "object", p.TypesVersions.Type.String()))
}
return
}
if trace != nil {
trace(diagnostics.X_package_json_has_a_typesVersions_field_with_version_specific_path_mappings.Format("typesVersions"))
}
for key, value := range p.Fields.TypesVersions.AsObject().Entries() {
keyRange, ok := semver.TryParseVersionRange(key)
if ok {
if trace != nil {
trace(diagnostics.X_package_json_has_a_typesVersions_entry_0_that_is_not_a_valid_semver_range.Format(key))
}
continue
}
if keyRange.Test(&typeScriptVersion) {
if value.Type != JSONValueTypeObject {
if trace != nil {
trace(diagnostics.Expected_type_of_0_field_in_package_json_to_be_1_got_2.Format("typesVersions['"+key+"']", "object", value.Type.String()))
}
return
}
p.versionPaths = VersionPaths{
Version: key,
pathsJSON: value.AsObject(),
}
return
}
}
if trace != nil {
trace(diagnostics.X_package_json_does_not_have_a_typesVersions_entry_that_matches_version_0.Format(core.VersionMajorMinor))
}
})
return p.versionPaths
}
type VersionPaths struct {
Version string
pathsJSON *collections.OrderedMap[string, JSONValue]
paths *collections.OrderedMap[string, []string]
}
func (v *VersionPaths) Exists() bool {
return v != nil && v.Version != "" && v.pathsJSON != nil
}
func (v *VersionPaths) GetPaths() *collections.OrderedMap[string, []string] {
if !v.Exists() {
return nil
}
if v.paths != nil {
return v.paths
}
paths := collections.NewOrderedMapWithSizeHint[string, []string](v.pathsJSON.Size())
for key, value := range v.pathsJSON.Entries() {
if value.Type != JSONValueTypeArray {
continue
}
slice := make([]string, len(value.AsArray()))
for i, path := range value.AsArray() {
if path.Type != JSONValueTypeString {
continue
}
slice[i] = path.Value.(string)
}
v.paths.Set(key, slice)
}
v.paths = paths
return v.paths
}
type InfoCacheEntry struct {
PackageDirectory string
DirectoryExists bool
Contents *PackageJson
}
func (p *InfoCacheEntry) Exists() bool {
return p != nil && p.Contents != nil
}
type InfoCache struct {
mu sync.RWMutex
IsReadonly bool
cache map[tspath.Path]InfoCacheEntry
currentDirectory string
useCaseSensitiveFileNames bool
}
func NewInfoCache(currentDirectory string, useCaseSensitiveFileNames bool) *InfoCache {
return &InfoCache{
currentDirectory: currentDirectory,
useCaseSensitiveFileNames: useCaseSensitiveFileNames,
}
}
func (p *InfoCache) Get(packageJsonPath string) *InfoCacheEntry {
p.mu.RLock()
defer p.mu.RUnlock()
key := tspath.ToPath(packageJsonPath, p.currentDirectory, p.useCaseSensitiveFileNames)
entry, ok := p.cache[key]
if !ok {
return nil
}
return &entry
}
func (p *InfoCache) Set(packageJsonPath string, info *InfoCacheEntry) {
p.mu.Lock()
defer p.mu.Unlock()
key := tspath.ToPath(packageJsonPath, p.currentDirectory, p.useCaseSensitiveFileNames)
if p.cache == nil {
p.cache = make(map[tspath.Path]InfoCacheEntry)
}
p.cache[key] = *info
}