Skip to content

Commit 8b4401a

Browse files
committed
Try to adjust to latest auto-import changes
1 parent d60f281 commit 8b4401a

File tree

10 files changed

+84
-42
lines changed

10 files changed

+84
-42
lines changed

internal/api/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func (api *API) GetDiagnostics(ctx context.Context, projectId Handle[project.Pro
278278
return nil, errors.New("project not found")
279279
}
280280

281-
languageService := ls.NewLanguageService(project.GetProgram(), snapshot)
281+
languageService := ls.NewLanguageService(projectPath, project.GetProgram(), snapshot)
282282
diagnostics := languageService.GetDiagnostics(ctx, fileNames)
283283

284284
api.symbolsMu.Lock()

internal/api/server.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ func (h *hostWrapper) SessionOptions() *project.SessionOptions {
178178
return h.inner.SessionOptions()
179179
}
180180

181+
// SourceFS implements project.ProjectHost.
182+
func (h *hostWrapper) SourceFS() *project.SourceFS {
183+
return h.inner.SourceFS()
184+
}
185+
181186
// TypesNodeIgnorableNames implements project.ProjectHost.
182187
func (h *hostWrapper) GetDenoForkContextInfo() ast.DenoForkContextInfo {
183188
return h.server.forkContextInfo

internal/ls/autoimport/aliasresolver.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,16 @@ func (r *aliasResolver) GetModeForUsageLocation(file ast.HasFileName, moduleSpec
108108
}
109109

110110
// GetResolvedModule implements checker.Program.
111-
func (r *aliasResolver) GetResolvedModule(currentSourceFile ast.HasFileName, moduleReference string, mode core.ResolutionMode) *module.ResolvedModule {
111+
func (r *aliasResolver) GetResolvedModule(currentSourceFile ast.HasFileName, moduleReference string, mode core.ResolutionMode, importAttributeType string) *module.ResolvedModule {
112112
cache, _ := r.resolvedModules.LoadOrStore(currentSourceFile.Path(), &collections.SyncMap[module.ModeAwareCacheKey, *module.ResolvedModule]{})
113113
if resolved, ok := cache.Load(module.ModeAwareCacheKey{Name: moduleReference, Mode: mode}); ok {
114114
return resolved
115115
}
116-
resolved, _ := r.moduleResolver.ResolveModuleName(moduleReference, currentSourceFile.FileName(), mode, nil)
116+
var attrPtr *string
117+
if importAttributeType != "" {
118+
attrPtr = &importAttributeType
119+
}
120+
resolved, _ := r.moduleResolver.ResolveModuleName(moduleReference, currentSourceFile.FileName(), attrPtr, mode, nil)
117121
resolved, _ = cache.LoadOrStore(module.ModeAwareCacheKey{Name: moduleReference, Mode: mode}, resolved)
118122
if !resolved.IsResolved() && !tspath.PathIsRelative(moduleReference) {
119123
r.onFailedAmbientModuleLookup(currentSourceFile, moduleReference)
@@ -224,4 +228,19 @@ func (r *aliasResolver) SourceFileMayBeEmitted(sourceFile *ast.SourceFile, force
224228
panic("unimplemented")
225229
}
226230

231+
// GetDenoForkContextInfo implements Host.
232+
func (r *aliasResolver) GetDenoForkContextInfo() ast.DenoForkContextInfo {
233+
return ast.DenoForkContextInfo{}
234+
}
235+
236+
// GetModuleLiteralImportAttributeType implements Program.
237+
func (r *aliasResolver) GetModuleLiteralImportAttributeType(node *ast.StringLiteralLike) string {
238+
return ""
239+
}
240+
241+
// IsNodeSourceFile implements Host.
242+
func (r *aliasResolver) IsNodeSourceFile(path tspath.Path) bool {
243+
return false
244+
}
245+
227246
var _ checker.Program = (*aliasResolver)(nil)

internal/ls/autoimport/extract.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func (e *exportExtractor) extractFromFile(file *ast.SourceFile) []*Export {
110110
moduleDeclarations := core.Filter(file.Statements.Nodes, ast.IsModuleWithStringLiteralName)
111111
var exportCount int
112112
for _, decl := range moduleDeclarations {
113-
exportCount += len(decl.AsModuleDeclaration().Symbol.Exports)
113+
exportCount += decl.AsModuleDeclaration().Symbol.Exports.Len()
114114
}
115115
exports := make([]*Export, 0, exportCount)
116116
for _, decl := range moduleDeclarations {
@@ -131,19 +131,19 @@ func (e *exportExtractor) extractFromModule(file *ast.SourceFile) []*Export {
131131
})
132132
var augmentationExportCount int
133133
for _, decl := range moduleAugmentations {
134-
augmentationExportCount += len(decl.Symbol.Exports)
134+
augmentationExportCount += decl.Symbol.Exports.Len()
135135
}
136136
moduleID := e.getModuleID(file)
137-
exports := make([]*Export, 0, len(file.Symbol.Exports)+augmentationExportCount)
138-
for name, symbol := range file.Symbol.Exports {
137+
exports := make([]*Export, 0, file.Symbol.Exports.Len()+augmentationExportCount)
138+
for name, symbol := range file.Symbol.Exports.Iter() {
139139
e.extractFromSymbol(name, symbol, moduleID, file.FileName(), file, &exports)
140140
}
141141
for _, decl := range moduleAugmentations {
142142
name := decl.Name().AsStringLiteral().Text
143143
moduleID := ModuleID(name)
144144
var moduleFileName string
145145
if tspath.IsExternalModuleNameRelative(name) {
146-
if resolved, _ := e.moduleResolver.ResolveModuleName(name, file.FileName(), core.ModuleKindCommonJS, nil); resolved.IsResolved() {
146+
if resolved, _ := e.moduleResolver.ResolveModuleName(name, file.FileName(), nil, core.ModuleKindCommonJS, nil); resolved.IsResolved() {
147147
moduleFileName = resolved.ResolvedFileName
148148
moduleID = ModuleID(e.toPath(moduleFileName))
149149
} else {
@@ -158,7 +158,7 @@ func (e *exportExtractor) extractFromModule(file *ast.SourceFile) []*Export {
158158
}
159159

160160
func (e *exportExtractor) extractFromModuleDeclaration(decl *ast.ModuleDeclaration, file *ast.SourceFile, moduleID ModuleID, moduleFileName string, exports *[]*Export) {
161-
for name, symbol := range decl.Symbol.Exports {
161+
for name, symbol := range decl.Symbol.Exports.Iter() {
162162
e.extractFromSymbol(name, symbol, moduleID, moduleFileName, file, exports)
163163
}
164164
}
@@ -173,7 +173,7 @@ func (e *symbolExtractor) extractFromSymbol(name string, symbol *ast.Symbol, mod
173173
allExports := e.checker.GetExportsOfModule(symbol.Parent)
174174
// allExports includes named exports from the file that will be processed separately;
175175
// we want to add only the ones that come from the star
176-
for name, namedExport := range symbol.Parent.Exports {
176+
for name, namedExport := range symbol.Parent.Exports.Iter() {
177177
if name != ast.InternalSymbolNameExportStar {
178178
idx := slices.Index(allExports, namedExport)
179179
if idx >= 0 || shouldIgnoreSymbol(namedExport) {
@@ -237,8 +237,8 @@ func (e *symbolExtractor) extractFromSymbol(name string, symbol *ast.Symbol, mod
237237

238238
if target != nil {
239239
if syntax == ExportSyntaxEquals && target.Flags&ast.SymbolFlagsNamespace != 0 {
240-
*exports = slices.Grow(*exports, len(target.Exports))
241-
for _, namedExport := range target.Exports {
240+
*exports = slices.Grow(*exports, target.Exports.Len())
241+
for namedExport := range target.Exports.Values() {
242242
export, _ := e.createExport(namedExport, moduleID, moduleFileName, syntax, file, checkerLease)
243243
if export != nil {
244244
export.through = name
@@ -255,10 +255,13 @@ func (e *symbolExtractor) extractFromSymbol(name string, symbol *ast.Symbol, mod
255255
*exports = slices.Grow(*exports, len(expression.AsObjectLiteralExpression().Properties.Nodes))
256256
for _, prop := range expression.AsObjectLiteralExpression().Properties.Nodes {
257257
if ast.IsShorthandPropertyAssignment(prop) || ast.IsPropertyAssignment(prop) && prop.AsPropertyAssignment().Name().Kind == ast.KindIdentifier {
258-
export, _ := e.createExport(expression.Symbol().Members[prop.Name().Text()], moduleID, moduleFileName, syntax, file, checkerLease)
259-
if export != nil {
260-
export.through = name
261-
*exports = append(*exports, export)
258+
memberSymbol := expression.Symbol().Members.Get(prop.Name().Text())
259+
if memberSymbol != nil {
260+
export, _ := e.createExport(memberSymbol, moduleID, moduleFileName, syntax, file, checkerLease)
261+
if export != nil {
262+
export.through = name
263+
*exports = append(*exports, export)
264+
}
262265
}
263266
}
264267
}

internal/project/autoimport.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (a *autoImportBuilderFS) GetFileByPath(fileName string, path tspath.Path) F
5959
type autoImportRegistryCloneHost struct {
6060
projectCollection *ProjectCollection
6161
parseCache *ParseCache
62-
fs *sourceFS
62+
fs *SourceFS
6363
currentDirectory string
6464

6565
filesMu sync.Mutex

internal/project/compilerhost.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package project
22

33
import (
4+
"time"
5+
46
"github.com/microsoft/typescript-go/internal/ast"
7+
"github.com/microsoft/typescript-go/internal/collections"
58
"github.com/microsoft/typescript-go/internal/compiler"
69
"github.com/microsoft/typescript-go/internal/core"
710
"github.com/microsoft/typescript-go/internal/diagnostics"
@@ -20,6 +23,7 @@ type ProjectHost interface {
2023
UpdateSeenFiles(*collections.SyncSet[tspath.Path])
2124
Freeze(snapshotFS *SnapshotFS, configFileRegistry *ConfigFileRegistry)
2225
CompilerFS() *CompilerFS
26+
SourceFS() *SourceFS
2327
}
2428

2529
var (
@@ -32,7 +36,7 @@ type compilerHost struct {
3236
currentDirectory string
3337
sessionOptions *SessionOptions
3438

35-
fs *snapshotFSBuilder
39+
sourceFS *SourceFS
3640
compilerFS *CompilerFS
3741
configFileRegistry *ConfigFileRegistry
3842

@@ -62,6 +66,11 @@ func (c *builderFileSource) GetFile(fileName string) FileHandle {
6266
return c.snapshotFSBuilder.GetFileByPath(fileName, path)
6367
}
6468

69+
func (c *builderFileSource) GetFileByPath(fileName string, path tspath.Path) FileHandle {
70+
c.seenFiles.Add(path)
71+
return c.snapshotFSBuilder.GetFileByPath(fileName, path)
72+
}
73+
6574
func (c *builderFileSource) FS() vfs.FS {
6675
return c.snapshotFSBuilder.FS()
6776
}
@@ -85,7 +94,8 @@ func NewProjectHost(
8594
currentDirectory: currentDirectory,
8695
sessionOptions: builder.sessionOptions,
8796

88-
sourceFS: newSourceFS(true, builder.fs, builder.toPath),
97+
sourceFS: newSourceFS(true, builder.fs, builder.toPath),
98+
compilerFS: compilerFS,
8999

90100
project: project,
91101
builder: builder,
@@ -235,11 +245,11 @@ func (c *compilerHost) SessionOptions() *SessionOptions {
235245
}
236246

237247
func (c *compilerHost) SeenFiles() *collections.SyncSet[tspath.Path] {
238-
return c.seenFiles
248+
return c.sourceFS.seenFiles
239249
}
240250

241251
func (c *compilerHost) UpdateSeenFiles(seenFiles *collections.SyncSet[tspath.Path]) {
242-
c.seenFiles = seenFiles
252+
c.sourceFS.seenFiles = seenFiles
243253
}
244254

245255
func (c *compilerHost) Freeze(snapshotFS *SnapshotFS, configFileRegistry *ConfigFileRegistry) {
@@ -249,3 +259,7 @@ func (c *compilerHost) Freeze(snapshotFS *SnapshotFS, configFileRegistry *Config
249259
func (c *compilerHost) CompilerFS() *CompilerFS {
250260
return c.compilerFS
251261
}
262+
263+
func (c *compilerHost) SourceFS() *SourceFS {
264+
return c.sourceFS
265+
}

internal/project/projectcollectionbuilder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ func (b *ProjectCollectionBuilder) updateProgram(entry dirty.Value[*Project], lo
990990
project.ProgramUpdateKind = result.UpdateKind
991991
project.ProgramLastUpdate = b.newSnapshotID
992992
if result.UpdateKind == ProgramUpdateKindCloned {
993-
project.host.sourceFS.seenFiles = oldHost.sourceFS.seenFiles
993+
project.host.SourceFS().seenFiles = oldHost.SourceFS().seenFiles
994994
}
995995
if result.UpdateKind == ProgramUpdateKindNewFiles {
996996
filesChanged = true

internal/project/snapshot.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ func (s *Snapshot) Clone(ctx context.Context, change SnapshotChange, overlays ma
346346
removedFiles := 0
347347
fs.diskFiles.Range(func(entry *dirty.SyncMapEntry[tspath.Path, *diskFile]) bool {
348348
for _, project := range projectCollection.Projects() {
349-
if project.host != nil && project.host.sourceFS.Seen(entry.Key()) {
349+
if project.host != nil && project.host.SourceFS().Seen(entry.Key()) {
350350
return true
351351
}
352352
}

internal/project/snapshotfs.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -199,16 +199,16 @@ func (s *snapshotFSBuilder) markDirtyFiles(change FileChangeSummary) {
199199
}
200200
}
201201

202-
// sourceFS is a vfs.FS that sources files from a FileSource and tracks seen files.
203-
type sourceFS struct {
202+
// SourceFS is a vfs.FS that sources files from a FileSource and tracks seen files.
203+
type SourceFS struct {
204204
tracking bool
205205
toPath func(fileName string) tspath.Path
206206
seenFiles *collections.SyncSet[tspath.Path]
207207
source FileSource
208208
}
209209

210-
func newSourceFS(tracking bool, source FileSource, toPath func(fileName string) tspath.Path) *sourceFS {
211-
fs := &sourceFS{
210+
func newSourceFS(tracking bool, source FileSource, toPath func(fileName string) tspath.Path) *SourceFS {
211+
fs := &SourceFS{
212212
tracking: tracking,
213213
toPath: toPath,
214214
source: source,
@@ -219,79 +219,79 @@ func newSourceFS(tracking bool, source FileSource, toPath func(fileName string)
219219
return fs
220220
}
221221

222-
var _ vfs.FS = (*sourceFS)(nil)
222+
var _ vfs.FS = (*SourceFS)(nil)
223223

224-
func (fs *sourceFS) DisableTracking() {
224+
func (fs *SourceFS) DisableTracking() {
225225
fs.tracking = false
226226
}
227227

228-
func (fs *sourceFS) Track(fileName string) {
228+
func (fs *SourceFS) Track(fileName string) {
229229
if !fs.tracking {
230230
return
231231
}
232232
fs.seenFiles.Add(fs.toPath(fileName))
233233
}
234234

235-
func (fs *sourceFS) Seen(path tspath.Path) bool {
235+
func (fs *SourceFS) Seen(path tspath.Path) bool {
236236
if fs.seenFiles == nil {
237237
return false
238238
}
239239
return fs.seenFiles.Has(path)
240240
}
241241

242-
func (fs *sourceFS) GetFile(fileName string) FileHandle {
242+
func (fs *SourceFS) GetFile(fileName string) FileHandle {
243243
fs.Track(fileName)
244244
return fs.source.GetFile(fileName)
245245
}
246246

247-
func (fs *sourceFS) GetFileByPath(fileName string, path tspath.Path) FileHandle {
247+
func (fs *SourceFS) GetFileByPath(fileName string, path tspath.Path) FileHandle {
248248
fs.Track(fileName)
249249
return fs.source.GetFileByPath(fileName, path)
250250
}
251251

252252
// DirectoryExists implements vfs.FS.
253-
func (fs *sourceFS) DirectoryExists(path string) bool {
253+
func (fs *SourceFS) DirectoryExists(path string) bool {
254254
return fs.source.FS().DirectoryExists(path)
255255
}
256256

257257
// FileExists implements vfs.FS.
258-
func (fs *sourceFS) FileExists(path string) bool {
258+
func (fs *SourceFS) FileExists(path string) bool {
259259
if fh := fs.GetFile(path); fh != nil {
260260
return true
261261
}
262262
return fs.source.FS().FileExists(path)
263263
}
264264

265265
// GetAccessibleEntries implements vfs.FS.
266-
func (fs *sourceFS) GetAccessibleEntries(path string) vfs.Entries {
266+
func (fs *SourceFS) GetAccessibleEntries(path string) vfs.Entries {
267267
return fs.source.FS().GetAccessibleEntries(path)
268268
}
269269

270270
// ReadFile implements vfs.FS.
271-
func (fs *sourceFS) ReadFile(path string) (contents string, ok bool) {
271+
func (fs *SourceFS) ReadFile(path string) (contents string, ok bool) {
272272
if fh := fs.GetFile(path); fh != nil {
273273
return fh.Content(), true
274274
}
275275
return "", false
276276
}
277277

278278
// Realpath implements vfs.FS.
279-
func (fs *sourceFS) Realpath(path string) string {
279+
func (fs *SourceFS) Realpath(path string) string {
280280
return fs.source.FS().Realpath(path)
281281
}
282282

283283
// Stat implements vfs.FS.
284-
func (fs *sourceFS) Stat(path string) vfs.FileInfo {
284+
func (fs *SourceFS) Stat(path string) vfs.FileInfo {
285285
return fs.source.FS().Stat(path)
286286
}
287287

288288
// UseCaseSensitiveFileNames implements vfs.FS.
289-
func (fs *sourceFS) UseCaseSensitiveFileNames() bool {
289+
func (fs *SourceFS) UseCaseSensitiveFileNames() bool {
290290
return fs.source.FS().UseCaseSensitiveFileNames()
291291
}
292292

293293
// WalkDir implements vfs.FS.
294-
func (fs *sourceFS) WalkDir(root string, walkFn vfs.WalkDirFunc) error {
294+
func (fs *SourceFS) WalkDir(root string, walkFn vfs.WalkDirFunc) error {
295295
return fs.source.FS().WalkDir(root, walkFn)
296296
}
297297

package-lock.json

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)