Skip to content

Commit 6d92482

Browse files
committed
Watch type reference locations too and handle auto type refs
1 parent 96bcae5 commit 6d92482

File tree

5 files changed

+79
-19
lines changed

5 files changed

+79
-19
lines changed

internal/compiler/fileloader.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,16 @@ func processAllProgramFiles(
113113
var unsupportedExtensions []string
114114

115115
loader.parseTasks.collect(&loader, loader.rootTasks, func(task *parseTask, _ []tspath.Path) {
116-
file := task.file
117116
if task.isRedirected {
118117
return
119118
}
119+
120+
if task.isForAutomaticTypeDirective {
121+
typeResolutionsInFile[task.path] = task.typeResolutionsInFile
122+
return
123+
}
124+
file := task.file
125+
path := task.path
120126
if file == nil {
121127
missingFiles = append(missingFiles, task.normalizedFilePath)
122128
return
@@ -126,7 +132,6 @@ func processAllProgramFiles(
126132
} else {
127133
files = append(files, file)
128134
}
129-
path := file.Path()
130135

131136
filesByPath[path] = file
132137
resolvedModules[path] = task.resolutionsInFile
@@ -189,14 +194,31 @@ func (p *fileLoader) addAutomaticTypeDirectiveTasks() {
189194
containingDirectory = p.opts.Host.GetCurrentDirectory()
190195
}
191196
containingFileName := tspath.CombinePaths(containingDirectory, module.InferredTypesContainingFile)
197+
p.rootTasks = append(p.rootTasks, &parseTask{normalizedFilePath: containingFileName, isLib: false, isForAutomaticTypeDirective: true})
198+
}
192199

193-
automaticTypeDirectiveNames := module.GetAutomaticTypeDirectiveNames(compilerOptions, p.opts.Host)
194-
for _, name := range automaticTypeDirectiveNames {
195-
resolved := p.resolver.ResolveTypeReferenceDirective(name, containingFileName, core.ModuleKindNodeNext, nil)
196-
if resolved.IsResolved() {
197-
p.rootTasks = append(p.rootTasks, &parseTask{normalizedFilePath: resolved.ResolvedFileName, isLib: false})
200+
func (p *fileLoader) resolveAutomaticTypeDirectives(containingFileName string) (
201+
toParse []resolvedRef,
202+
typeResolutionsInFile module.ModeAwareCache[*module.ResolvedTypeReferenceDirective],
203+
) {
204+
automaticTypeDirectiveNames := module.GetAutomaticTypeDirectiveNames(p.opts.Config.CompilerOptions(), p.opts.Host)
205+
if len(automaticTypeDirectiveNames) != 0 {
206+
toParse = make([]resolvedRef, 0, len(automaticTypeDirectiveNames))
207+
typeResolutionsInFile = make(module.ModeAwareCache[*module.ResolvedTypeReferenceDirective], len(automaticTypeDirectiveNames))
208+
for _, name := range automaticTypeDirectiveNames {
209+
resolutionMode := core.ModuleKindNodeNext
210+
resolved := p.resolver.ResolveTypeReferenceDirective(name, containingFileName, resolutionMode, nil)
211+
typeResolutionsInFile[module.ModeAwareCacheKey{Name: name, Mode: resolutionMode}] = resolved
212+
if resolved.IsResolved() {
213+
toParse = append(toParse, resolvedRef{
214+
fileName: resolved.ResolvedFileName,
215+
increaseDepth: resolved.IsExternalLibraryImport,
216+
elideOnDepth: false,
217+
})
218+
}
198219
}
199220
}
221+
return toParse, typeResolutionsInFile
200222
}
201223

202224
func (p *fileLoader) addProjectReferenceTasks() {

internal/compiler/parsetask.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ import (
99
)
1010

1111
type parseTask struct {
12-
normalizedFilePath string
13-
path tspath.Path
14-
file *ast.SourceFile
15-
isLib bool
16-
isRedirected bool
17-
subTasks []*parseTask
18-
loaded bool
12+
normalizedFilePath string
13+
path tspath.Path
14+
file *ast.SourceFile
15+
isLib bool
16+
isRedirected bool
17+
subTasks []*parseTask
18+
loaded bool
19+
isForAutomaticTypeDirective bool
1920

2021
metadata ast.SourceFileMetaData
2122
resolutionsInFile module.ModeAwareCache[*module.ResolvedModule]
@@ -36,8 +37,11 @@ func (t *parseTask) Path() tspath.Path {
3637

3738
func (t *parseTask) load(loader *fileLoader) {
3839
t.loaded = true
39-
4040
t.path = loader.toPath(t.normalizedFilePath)
41+
if t.isForAutomaticTypeDirective {
42+
t.loadAutomaticTypeDirectives(loader)
43+
return
44+
}
4145
redirect := loader.projectReferenceFileMapper.getParseFileRedirect(t)
4246
if redirect != "" {
4347
t.redirect(loader, redirect)
@@ -97,6 +101,14 @@ func (t *parseTask) redirect(loader *fileLoader, fileName string) {
97101
t.subTasks = []*parseTask{{normalizedFilePath: tspath.NormalizePath(fileName), isLib: t.isLib}}
98102
}
99103

104+
func (t *parseTask) loadAutomaticTypeDirectives(loader *fileLoader) {
105+
toParseTypeRefs, typeResolutionsInFile := loader.resolveAutomaticTypeDirectives(t.normalizedFilePath)
106+
t.typeResolutionsInFile = typeResolutionsInFile
107+
for _, typeResolution := range toParseTypeRefs {
108+
t.addSubTask(typeResolution, false)
109+
}
110+
}
111+
100112
type resolvedRef struct {
101113
fileName string
102114
increaseDepth bool

internal/compiler/program.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,10 @@ func (p *Program) GetResolvedTypeReferenceDirectiveFromTypeReferenceDirective(ty
813813
return nil
814814
}
815815

816+
func (p *Program) GetResolvedTypeReferenceDirectives() map[tspath.Path]module.ModeAwareCache[*module.ResolvedTypeReferenceDirective] {
817+
return p.typeResolutionsInFile
818+
}
819+
816820
func (p *Program) getModeForTypeReferenceDirectiveInFile(ref *ast.FileReference, sourceFile *ast.SourceFile) core.ResolutionMode {
817821
if ref.ResolutionMode != core.ResolutionModeNone {
818822
return ref.ResolutionMode

internal/module/types.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ func (r *ResolvedModule) IsResolved() bool {
8181
return r != nil && r.ResolvedFileName != ""
8282
}
8383

84+
func (r *ResolvedModule) GetLookupLocations() *LookupLocations {
85+
return &r.LookupLocations
86+
}
87+
8488
type ResolvedTypeReferenceDirective struct {
8589
LookupLocations
8690
Primary bool
@@ -94,6 +98,10 @@ func (r *ResolvedTypeReferenceDirective) IsResolved() bool {
9498
return r.ResolvedFileName != ""
9599
}
96100

101+
func (r *ResolvedTypeReferenceDirective) GetLookupLocations() *LookupLocations {
102+
return &r.LookupLocations
103+
}
104+
97105
type extensions int32
98106

99107
const (

internal/project/project.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -350,23 +350,37 @@ func (p *Project) GetLanguageServiceForRequest(ctx context.Context) (*ls.Languag
350350
func (p *Project) getModuleResolutionWatchGlobs() (failedLookups map[tspath.Path]string, affectingLocaions map[tspath.Path]string) {
351351
failedLookups = make(map[tspath.Path]string)
352352
affectingLocaions = make(map[tspath.Path]string)
353-
for _, resolvedModulesInFile := range p.program.GetResolvedModules() {
353+
extractLookups(p, failedLookups, affectingLocaions, p.program.GetResolvedModules())
354+
extractLookups(p, failedLookups, affectingLocaions, p.program.GetResolvedTypeReferenceDirectives())
355+
return failedLookups, affectingLocaions
356+
}
357+
358+
type ResolutionWithLookupLocations interface {
359+
GetLookupLocations() *module.LookupLocations
360+
}
361+
362+
func extractLookups[T ResolutionWithLookupLocations](
363+
p *Project,
364+
failedLookups map[tspath.Path]string,
365+
affectingLocaions map[tspath.Path]string,
366+
cache map[tspath.Path]module.ModeAwareCache[T],
367+
) {
368+
for _, resolvedModulesInFile := range cache {
354369
for _, resolvedModule := range resolvedModulesInFile {
355-
for _, failedLookupLocation := range resolvedModule.FailedLookupLocations {
370+
for _, failedLookupLocation := range resolvedModule.GetLookupLocations().FailedLookupLocations {
356371
path := p.toPath(failedLookupLocation)
357372
if _, ok := failedLookups[path]; !ok {
358373
failedLookups[path] = failedLookupLocation
359374
}
360375
}
361-
for _, affectingLocation := range resolvedModule.AffectingLocations {
376+
for _, affectingLocation := range resolvedModule.GetLookupLocations().AffectingLocations {
362377
path := p.toPath(affectingLocation)
363378
if _, ok := affectingLocaions[path]; !ok {
364379
affectingLocaions[path] = affectingLocation
365380
}
366381
}
367382
}
368383
}
369-
return failedLookups, affectingLocaions
370384
}
371385

372386
func (p *Project) updateWatchers(ctx context.Context) {

0 commit comments

Comments
 (0)