@@ -5,15 +5,15 @@ import (
5
5
6
6
"github.com/microsoft/typescript-go/internal/core"
7
7
"github.com/microsoft/typescript-go/internal/module"
8
+ "github.com/microsoft/typescript-go/internal/modulespecifiers"
8
9
"github.com/microsoft/typescript-go/internal/tspath"
9
10
"github.com/microsoft/typescript-go/internal/vfs"
10
11
)
11
12
12
13
type ProjectReferenceDtsFakingHost struct {
13
- host CompilerHost
14
14
projectReferenceFileMapper * ProjectReferenceFileMapper
15
15
dtsDirectories core.Set [tspath.Path ]
16
- symlinkCache module .SymlinkCache
16
+ symlinkCache modulespecifiers .SymlinkCache
17
17
}
18
18
19
19
var _ module.ResolutionHost = (* ProjectReferenceDtsFakingHost )(nil )
@@ -23,21 +23,21 @@ func (h *ProjectReferenceDtsFakingHost) FS() vfs.FS {
23
23
}
24
24
25
25
func (h * ProjectReferenceDtsFakingHost ) GetCurrentDirectory () string {
26
- return h .host .GetCurrentDirectory ()
26
+ return h .projectReferenceFileMapper . opts . Host .GetCurrentDirectory ()
27
27
}
28
28
29
29
func (h * ProjectReferenceDtsFakingHost ) Trace (msg string ) {
30
- h .host .Trace (msg )
30
+ h .projectReferenceFileMapper . opts . Host .Trace (msg )
31
31
}
32
32
33
33
// UseCaseSensitiveFileNames returns true if the file system is case-sensitive.
34
34
func (h * ProjectReferenceDtsFakingHost ) UseCaseSensitiveFileNames () bool {
35
- return h .host .FS ().UseCaseSensitiveFileNames ()
35
+ return h .projectReferenceFileMapper . opts . Host .FS ().UseCaseSensitiveFileNames ()
36
36
}
37
37
38
38
// FileExists returns true if the file exists.
39
39
func (h * ProjectReferenceDtsFakingHost ) FileExists (path string ) bool {
40
- if h .host .FS ().FileExists (path ) {
40
+ if h .projectReferenceFileMapper . opts . Host .FS ().FileExists (path ) {
41
41
return true
42
42
}
43
43
if ! tspath .IsDeclarationFileName (path ) {
@@ -49,7 +49,7 @@ func (h *ProjectReferenceDtsFakingHost) FileExists(path string) bool {
49
49
50
50
func (h * ProjectReferenceDtsFakingHost ) ReadFile (path string ) (contents string , ok bool ) {
51
51
// Dont need to override as we cannot mimick read file
52
- return h .host .FS ().ReadFile (path )
52
+ return h .projectReferenceFileMapper . opts . Host .FS ().ReadFile (path )
53
53
}
54
54
55
55
func (h * ProjectReferenceDtsFakingHost ) WriteFile (path string , data string , writeByteOrderMark bool ) error {
@@ -63,7 +63,7 @@ func (h *ProjectReferenceDtsFakingHost) Remove(path string) error {
63
63
64
64
// DirectoryExists returns true if the path is a directory.
65
65
func (h * ProjectReferenceDtsFakingHost ) DirectoryExists (path string ) bool {
66
- if h .host .FS ().DirectoryExists (path ) {
66
+ if h .projectReferenceFileMapper . opts . Host .FS ().DirectoryExists (path ) {
67
67
h .handleDirectoryCouldBeSymlink (path )
68
68
return true
69
69
}
@@ -89,11 +89,11 @@ func (h *ProjectReferenceDtsFakingHost) WalkDir(root string, walkFn vfs.WalkDirF
89
89
// Realpath returns the "real path" of the specified path,
90
90
// following symlinks and correcting filename casing.
91
91
func (h * ProjectReferenceDtsFakingHost ) Realpath (path string ) string {
92
- result , ok := h .symlinkCache .SymlinkedFiles ()[ h .toPath (path )]
92
+ result , ok := h .symlinkCache .SymlinkedFiles (). Load ( h .toPath (path ))
93
93
if ok {
94
94
return result
95
95
}
96
- return h .host .FS ().Realpath (path )
96
+ return h .projectReferenceFileMapper . opts . Host .FS ().Realpath (path )
97
97
}
98
98
99
99
func (h * ProjectReferenceDtsFakingHost ) toPath (path string ) tspath.Path {
@@ -110,27 +110,26 @@ func (h *ProjectReferenceDtsFakingHost) handleDirectoryCouldBeSymlink(directory
110
110
return
111
111
}
112
112
directoryPath := tspath .Path (tspath .EnsureTrailingDirectorySeparator (string (h .toPath (directory ))))
113
- if _ , ok := h .symlinkCache .SymlinkedDirectories ()[ directoryPath ] ; ok {
113
+ if _ , ok := h .symlinkCache .SymlinkedDirectories (). Load ( directoryPath ) ; ok {
114
114
return
115
115
}
116
116
117
- realDirectory := h .host .FS ().Realpath (directory )
117
+ realDirectory := h .projectReferenceFileMapper . opts . Host .FS ().Realpath (directory )
118
118
var realPath tspath.Path
119
+ var symlinkedDirectory * modulespecifiers.SymlinkedDirectory
119
120
if realDirectory == directory {
120
121
// not symlinked
121
- h .symlinkCache .SetSymlinkedDirectory (directory , directoryPath , nil )
122
- return
123
- }
124
- if realPath = tspath .Path (tspath .EnsureTrailingDirectorySeparator (string (h .toPath (realDirectory )))); realPath == directoryPath {
122
+ symlinkedDirectory = nil
123
+ } else if realPath = tspath .Path (tspath .EnsureTrailingDirectorySeparator (string (h .toPath (realDirectory )))); realPath == directoryPath {
125
124
// not symlinked
126
- h .symlinkCache .SetSymlinkedDirectory (directory , directoryPath , nil )
127
- return
125
+ symlinkedDirectory = nil
126
+ } else {
127
+ symlinkedDirectory = & modulespecifiers.SymlinkedDirectory {
128
+ Real : tspath .EnsureTrailingDirectorySeparator (realDirectory ),
129
+ RealPath : realPath ,
130
+ }
128
131
}
129
-
130
- h .symlinkCache .SetSymlinkedDirectory (directory , directoryPath , & module.SymlinkedDirectory {
131
- Real : tspath .EnsureTrailingDirectorySeparator (realDirectory ),
132
- RealPath : realPath ,
133
- })
132
+ h .symlinkCache .SetSymlinkedDirectory (directory , directoryPath , symlinkedDirectory )
134
133
}
135
134
136
135
func (h * ProjectReferenceDtsFakingHost ) fileOrDirectoryExistsUsingSource (fileOrDirectory string , isFile bool ) bool {
@@ -141,33 +140,33 @@ func (h *ProjectReferenceDtsFakingHost) fileOrDirectoryExistsUsingSource(fileOrD
141
140
return result == core .TSTrue
142
141
}
143
142
144
- // !!! sheetal this needs to be thread safe !!
145
143
symlinkedDirectories := h .symlinkCache .SymlinkedDirectories ()
146
- if symlinkedDirectories == nil {
144
+ if symlinkedDirectories . Size () == 0 {
147
145
return false
148
146
}
149
147
fileOrDirectoryPath := h .toPath (fileOrDirectory )
150
148
if ! strings .Contains (string (fileOrDirectoryPath ), "/node_modules/" ) {
151
149
return false
152
150
}
153
151
if isFile {
154
- _ , ok := h .symlinkCache .SymlinkedFiles ()[ fileOrDirectoryPath ]
152
+ _ , ok := h .symlinkCache .SymlinkedFiles (). Load ( fileOrDirectoryPath )
155
153
if ok {
156
154
return true
157
155
}
158
156
}
159
157
160
158
// If it contains node_modules check if its one of the symlinked path we know of
161
- for directoryPath , symlinkedDirectory := range symlinkedDirectories {
159
+ var exists bool
160
+ symlinkedDirectories .Range (func (directoryPath tspath.Path , symlinkedDirectory * modulespecifiers.SymlinkedDirectory ) bool {
162
161
if symlinkedDirectory == nil {
163
- continue
162
+ return true
164
163
}
165
164
166
165
relative , hasPrefix := strings .CutPrefix (string (fileOrDirectoryPath ), string (directoryPath ))
167
166
if ! hasPrefix {
168
- continue
167
+ return true
169
168
}
170
- if fileOrDirectoryExistsUsingSource (string (symlinkedDirectory .RealPath ) + relative ).IsTrue () {
169
+ if exists = fileOrDirectoryExistsUsingSource (string (symlinkedDirectory .RealPath ) + relative ).IsTrue (); exists {
171
170
if isFile {
172
171
// Store the real path for the file
173
172
absolutePath := tspath .GetNormalizedAbsolutePath (fileOrDirectory , h .GetCurrentDirectory ())
@@ -176,16 +175,17 @@ func (h *ProjectReferenceDtsFakingHost) fileOrDirectoryExistsUsingSource(fileOrD
176
175
symlinkedDirectory .Real + absolutePath [len (directoryPath ):],
177
176
)
178
177
}
179
- return true
178
+ return false
180
179
}
181
- }
182
- return false
180
+ return true
181
+ })
182
+ return exists
183
183
}
184
184
185
185
func (h * ProjectReferenceDtsFakingHost ) fileExistsIfProjectReferenceDts (file string ) core.Tristate {
186
186
source := h .projectReferenceFileMapper .getSourceAndProjectReference (h .toPath (file ))
187
187
if source != nil {
188
- return core .IfElse (h .host .FS ().FileExists (source .Source ), core .TSTrue , core .TSFalse )
188
+ return core .IfElse (h .projectReferenceFileMapper . opts . Host .FS ().FileExists (source .Source ), core .TSTrue , core .TSFalse )
189
189
}
190
190
return core .TSUnknown
191
191
}
0 commit comments