forked from microsoft/typescript-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompilerhost.go
More file actions
256 lines (212 loc) · 7.06 KB
/
compilerhost.go
File metadata and controls
256 lines (212 loc) · 7.06 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
252
253
254
255
256
package project
import (
"time"
"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/collections"
"github.com/microsoft/typescript-go/internal/compiler"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/module"
"github.com/microsoft/typescript-go/internal/project/logging"
"github.com/microsoft/typescript-go/internal/tsoptions"
"github.com/microsoft/typescript-go/internal/tspath"
"github.com/microsoft/typescript-go/internal/vfs"
)
type ProjectHost interface {
compiler.CompilerHost
Builder() *ProjectCollectionBuilder
SessionOptions() *SessionOptions
SeenFiles() *collections.SyncSet[tspath.Path]
UpdateSeenFiles(*collections.SyncSet[tspath.Path])
Freeze(snapshotFS *SnapshotFS, configFileRegistry *ConfigFileRegistry)
CompilerFS() *CompilerFS
}
var (
_ compiler.CompilerHost = (*compilerHost)(nil)
_ ProjectHost = (*compilerHost)(nil)
)
type compilerHost struct {
configFilePath tspath.Path
currentDirectory string
sessionOptions *SessionOptions
fs *snapshotFSBuilder
compilerFS *CompilerFS
configFileRegistry *ConfigFileRegistry
seenFiles *collections.SyncSet[tspath.Path]
project *Project
builder *ProjectCollectionBuilder
logger *logging.LogTree
}
// TypesNodeIgnorableNames implements compiler.CompilerHost.
func (c *compilerHost) GetDenoForkContextInfo() ast.DenoForkContextInfo {
return ast.DenoForkContextInfo{}
}
// IsNodeSourceFile implements compiler.CompilerHost.
func (c *compilerHost) IsNodeSourceFile(path tspath.Path) bool {
return false
}
type builderFileSource struct {
seenFiles *collections.SyncSet[tspath.Path]
snapshotFSBuilder *snapshotFSBuilder
}
func (c *builderFileSource) GetFile(fileName string) FileHandle {
path := c.snapshotFSBuilder.toPath(fileName)
c.seenFiles.Add(path)
return c.snapshotFSBuilder.GetFileByPath(fileName, path)
}
func (c *builderFileSource) FS() vfs.FS {
return c.snapshotFSBuilder.FS()
}
func NewProjectHost(
currentDirectory string,
project *Project,
builder *ProjectCollectionBuilder,
logger *logging.LogTree,
) ProjectHost {
seenFiles := &collections.SyncSet[tspath.Path]{}
compilerFS := &CompilerFS{
source: &builderFileSource{
seenFiles: seenFiles,
snapshotFSBuilder: builder.fs,
},
}
return &compilerHost{
configFilePath: project.configFilePath,
currentDirectory: currentDirectory,
sessionOptions: builder.sessionOptions,
compilerFS: compilerFS,
seenFiles: seenFiles,
fs: builder.fs,
project: project,
builder: builder,
logger: logger,
}
}
// freeze clears references to mutable state to make the compilerHost safe for use
// after the snapshot has been finalized. See the usage in snapshot.go for more details.
func (c *compilerHost) freeze(snapshotFS *SnapshotFS, configFileRegistry *ConfigFileRegistry) {
if c.builder == nil {
panic("freeze can only be called once")
}
c.compilerFS.source = snapshotFS
c.configFileRegistry = configFileRegistry
c.fs = nil
c.builder = nil
c.project = nil
c.logger = nil
}
func (c *compilerHost) ensureAlive() {
if c.builder == nil || c.project == nil {
panic("method must not be called after snapshot initialization")
}
}
// DefaultLibraryPath implements compiler.CompilerHost.
func (c *compilerHost) DefaultLibraryPath() string {
return c.sessionOptions.DefaultLibraryPath
}
// FS implements compiler.CompilerHost.
func (c *compilerHost) FS() vfs.FS {
return c.compilerFS
}
// GetCurrentDirectory implements compiler.CompilerHost.
func (c *compilerHost) GetCurrentDirectory() string {
return c.currentDirectory
}
// GetResolvedProjectReference implements compiler.CompilerHost.
func (c *compilerHost) GetResolvedProjectReference(fileName string, path tspath.Path) *tsoptions.ParsedCommandLine {
if c.builder == nil {
return c.configFileRegistry.GetConfig(path)
} else {
c.seenFiles.Add(path)
return c.builder.configFileRegistryBuilder.acquireConfigForProject(fileName, path, c.project, c.logger)
}
}
// GetSourceFile implements compiler.CompilerHost. GetSourceFile increments
// the ref count of source files it acquires in the parseCache. There should
// be a corresponding release for each call made.
func (c *compilerHost) GetSourceFile(opts ast.SourceFileParseOptions) *ast.SourceFile {
c.ensureAlive()
c.seenFiles.Add(opts.Path)
if fh := c.fs.GetFileByPath(opts.FileName, opts.Path); fh != nil {
return c.builder.parseCache.Acquire(fh, opts, fh.Kind())
}
return nil
}
// Trace implements compiler.CompilerHost.
func (c *compilerHost) Trace(msg string) {
panic("unimplemented")
}
var _ vfs.FS = (*CompilerFS)(nil)
type CompilerFS struct {
source FileSource
}
// DirectoryExists implements vfs.FS.
func (fs *CompilerFS) DirectoryExists(path string) bool {
return fs.source.FS().DirectoryExists(path)
}
// FileExists implements vfs.FS.
func (fs *CompilerFS) FileExists(path string) bool {
if fh := fs.source.GetFile(path); fh != nil {
return true
}
return fs.source.FS().FileExists(path)
}
// GetAccessibleEntries implements vfs.FS.
func (fs *CompilerFS) GetAccessibleEntries(path string) vfs.Entries {
return fs.source.FS().GetAccessibleEntries(path)
}
// ReadFile implements vfs.FS.
func (fs *CompilerFS) ReadFile(path string) (contents string, ok bool) {
if fh := fs.source.GetFile(path); fh != nil {
return fh.Content(), true
}
return "", false
}
// Realpath implements vfs.FS.
func (fs *CompilerFS) Realpath(path string) string {
return fs.source.FS().Realpath(path)
}
// Stat implements vfs.FS.
func (fs *CompilerFS) Stat(path string) vfs.FileInfo {
return fs.source.FS().Stat(path)
}
// UseCaseSensitiveFileNames implements vfs.FS.
func (fs *CompilerFS) UseCaseSensitiveFileNames() bool {
return fs.source.FS().UseCaseSensitiveFileNames()
}
// WalkDir implements vfs.FS.
func (fs *CompilerFS) WalkDir(root string, walkFn vfs.WalkDirFunc) error {
panic("unimplemented")
}
// WriteFile implements vfs.FS.
func (fs *CompilerFS) WriteFile(path string, data string, writeByteOrderMark bool) error {
panic("unimplemented")
}
// Remove implements vfs.FS.
func (fs *CompilerFS) Remove(path string) error {
panic("unimplemented")
}
// Chtimes implements vfs.FS.
func (fs *CompilerFS) Chtimes(path string, atime time.Time, mtime time.Time) error {
panic("unimplemented")
}
func (c *compilerHost) MakeResolver(host module.ResolutionHost, options *core.CompilerOptions, typingsLocation string, projectName string) module.ResolverInterface {
return module.NewResolver(host, options, typingsLocation, projectName)
}
func (c *compilerHost) Builder() *ProjectCollectionBuilder {
return c.builder
}
func (c *compilerHost) SessionOptions() *SessionOptions {
return c.sessionOptions
}
func (c *compilerHost) SeenFiles() *collections.SyncSet[tspath.Path] {
return c.seenFiles
}
func (c *compilerHost) UpdateSeenFiles(seenFiles *collections.SyncSet[tspath.Path]) {
c.seenFiles = seenFiles
}
func (c *compilerHost) Freeze(snapshotFS *SnapshotFS, configFileRegistry *ConfigFileRegistry) {
c.freeze(snapshotFS, configFileRegistry)
}
func (c *compilerHost) CompilerFS() *CompilerFS {
return c.compilerFS
}