Skip to content

Commit 90ee42f

Browse files
authored
Make getResolvedModule use module resolver (#171)
1 parent ac80542 commit 90ee42f

File tree

1 file changed

+2
-38
lines changed

1 file changed

+2
-38
lines changed

internal/compiler/program.go

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package compiler
22

33
import (
4-
"encoding/json"
54
"fmt"
65
"slices"
76
"strings"
@@ -179,11 +178,8 @@ func (p *Program) startParseTask(fileName string, wg *core.WorkGroup) {
179178
}
180179

181180
func (p *Program) getResolvedModule(currentSourceFile *ast.SourceFile, moduleReference string) *ast.SourceFile {
182-
directory := tspath.GetDirectoryPath(currentSourceFile.FileName())
183-
if tspath.IsExternalModuleNameRelative(moduleReference) {
184-
return p.findSourceFile(tspath.CombinePaths(directory, moduleReference), FileIncludeReason{FileIncludeKindImport, 0})
185-
}
186-
return p.findNodeModule(moduleReference)
181+
resolved := p.resolver.ResolveModuleName(moduleReference, currentSourceFile.FileName(), core.ModuleKindNodeNext, nil)
182+
return p.findSourceFile(resolved.ResolvedFileName, FileIncludeReason{FileIncludeKindImport, 0})
187183
}
188184

189185
func (p *Program) findSourceFile(candidate string, reason FileIncludeReason) *ast.SourceFile {
@@ -262,38 +258,6 @@ func (p *Program) resolveImportsAndModuleAugmentations(file *ast.SourceFile) []s
262258
return toParse
263259
}
264260

265-
func (p *Program) findNodeModule(moduleReference string) *ast.SourceFile {
266-
if p.nodeModules == nil {
267-
p.nodeModules = make(map[string]*ast.SourceFile)
268-
}
269-
if sourceFile, ok := p.nodeModules[moduleReference]; ok {
270-
return sourceFile
271-
}
272-
sourceFile := p.tryLoadNodeModule(tspath.CombinePaths(p.rootPath, "node_modules", moduleReference))
273-
if sourceFile == nil {
274-
sourceFile = p.tryLoadNodeModule(tspath.CombinePaths(p.rootPath, "node_modules/@types", moduleReference))
275-
}
276-
p.nodeModules[moduleReference] = sourceFile
277-
return sourceFile
278-
}
279-
280-
func (p *Program) tryLoadNodeModule(modulePath string) *ast.SourceFile {
281-
if packageJson, ok := p.host.FS().ReadFile(tspath.CombinePaths(modulePath, "package.json")); ok {
282-
var jsonMap map[string]any
283-
if json.Unmarshal([]byte(packageJson), &jsonMap) == nil {
284-
typesValue := jsonMap["types"]
285-
if typesValue == nil {
286-
typesValue = jsonMap["typings"]
287-
}
288-
if fileName, ok := typesValue.(string); ok {
289-
path := tspath.CombinePaths(modulePath, fileName)
290-
return p.filesByPath[tspath.ToPath(path, p.host.GetCurrentDirectory(), p.host.FS().UseCaseSensitiveFileNames())]
291-
}
292-
}
293-
}
294-
return nil
295-
}
296-
297261
func (p *Program) GetSyntacticDiagnostics(sourceFile *ast.SourceFile) []*ast.Diagnostic {
298262
return p.getDiagnosticsHelper(sourceFile, false /*ensureBound*/, p.getSyntaticDiagnosticsForFile)
299263
}

0 commit comments

Comments
 (0)