Skip to content

Commit aa12649

Browse files
committed
Use pointers for GetPackage and implement WIP GetResolvedProjectReference
1 parent 17d4e68 commit aa12649

File tree

3 files changed

+47
-34
lines changed

3 files changed

+47
-34
lines changed

internal/modulespecifiers/specifiers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ func tryGetModuleNameAsNodeModule(
675675
}
676676

677677
if fromLocator != nil && toLocator != nil {
678-
fromInfo := pnpApi.GetPackage(*fromLocator)
678+
fromInfo := pnpApi.GetPackage(fromLocator)
679679

680680
useToLocator := false
681681

@@ -696,7 +696,7 @@ func tryGetModuleNameAsNodeModule(
696696
}
697697

698698
if parts != nil {
699-
toInfo := pnpApi.GetPackage(*toLocator)
699+
toInfo := pnpApi.GetPackage(toLocator)
700700
parts = &NodeModulePathParts{
701701
TopLevelNodeModulesIndex: -1,
702702
TopLevelPackageNameIndex: -1,

internal/pnp/pnpapi.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (p *PnpApi) ResolveToUnqualified(specifier string, parentPath string) (stri
5050
return "", nil
5151
}
5252

53-
parentPkg := p.GetPackage(*parentLocator)
53+
parentPkg := p.GetPackage(parentLocator)
5454

5555
var referenceOrAlias *PackageDependency
5656
for _, dep := range parentPkg.PackageDependencies {
@@ -99,9 +99,9 @@ func (p *PnpApi) ResolveToUnqualified(specifier string, parentPath string) (stri
9999

100100
var dependencyPkg *PackageInfo
101101
if referenceOrAlias.IsAlias() {
102-
dependencyPkg = p.GetPackage(Locator{Name: referenceOrAlias.AliasName, Reference: referenceOrAlias.Reference})
102+
dependencyPkg = p.GetPackage(&Locator{Name: referenceOrAlias.AliasName, Reference: referenceOrAlias.Reference})
103103
} else {
104-
dependencyPkg = p.GetPackage(Locator{Name: referenceOrAlias.Ident, Reference: referenceOrAlias.Reference})
104+
dependencyPkg = p.GetPackage(&Locator{Name: referenceOrAlias.Ident, Reference: referenceOrAlias.Reference})
105105
}
106106

107107
return filepath.Join(p.manifest.dirPath, dependencyPkg.PackageLocation, modulePath), nil
@@ -123,7 +123,7 @@ func (p *PnpApi) findClosestPnpManifest() (*PnpManifestData, error) {
123123
}
124124
}
125125

126-
func (p *PnpApi) GetPackage(locator Locator) *PackageInfo {
126+
func (p *PnpApi) GetPackage(locator *Locator) *PackageInfo {
127127
packageRegistryMap := p.manifest.packageRegistryMap
128128
packageInfo, ok := packageRegistryMap[locator.Name][locator.Reference]
129129
if !ok {
@@ -177,7 +177,7 @@ func (p *PnpApi) FindLocator(parentPath string) (*Locator, error) {
177177
}
178178

179179
func (p *PnpApi) ResolveViaFallback(name string) *PackageDependency {
180-
topLevelPkg := p.GetPackage(Locator{Name: "", Reference: ""})
180+
topLevelPkg := p.GetPackage(&Locator{Name: "", Reference: ""})
181181

182182
if topLevelPkg != nil {
183183
for _, dep := range topLevelPkg.PackageDependencies {
@@ -250,12 +250,12 @@ func (p *PnpApi) GetPnpTypeRoots(currentDirectory string) []string {
250250
return []string{}
251251
}
252252

253-
packageDependencies := p.GetPackage(*currentPackage).PackageDependencies
253+
packageDependencies := p.GetPackage(currentPackage).PackageDependencies
254254

255255
typeRoots := []string{}
256256
for _, dep := range packageDependencies {
257257
if strings.HasPrefix(dep.Ident, "@types/") && dep.Reference != "" {
258-
packageInfo := p.GetPackage(Locator{Name: dep.Ident, Reference: dep.Reference})
258+
packageInfo := p.GetPackage(&Locator{Name: dep.Ident, Reference: dep.Reference})
259259
typeRoots = append(typeRoots, path.Dir(path.Join(p.manifest.dirPath, packageInfo.PackageLocation)))
260260
}
261261
}

internal/project/project.go

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,11 @@ func (p *Project) GetSourceFile(opts ast.SourceFileParseOptions) *ast.SourceFile
291291

292292
// GetResolvedProjectReference implements compiler.CompilerHost.
293293
func (p *Project) GetResolvedProjectReference(fileName string, path tspath.Path) *tsoptions.ParsedCommandLine {
294-
// TODO, we might need to add the code below for pnp (converted to go)
294+
config := p.host.ConfigFileRegistry().acquireConfig(fileName, path, p, nil)
295+
if config == nil {
296+
return nil
297+
}
298+
// TODO fix this and identify cases where we do need this
295299

296300
// With Plug'n'Play, dependencies that list peer dependencies
297301
// are "virtualized": they are resolved to a unique (virtual)
@@ -308,34 +312,43 @@ func (p *Project) GetResolvedProjectReference(fileName string, path tspath.Path)
308312
// user-provided references in our references by directly querying
309313
// the PnP API. This way users don't have to know the virtual paths,
310314
// but we still support them just fine even through references.
311-
312315
// pnpApi := pnp.GetPnpApi(fileName)
313316
// if pnpApi != nil {
314-
// pnpApi.GetPackage(fileName)
317+
// basePath := p.GetCurrentDirectory()
318+
319+
// getPnpPath := func(path string) string {
320+
// targetLocator, err := pnpApi.FindLocator(path + "/")
321+
// if err != nil {
322+
// return path
323+
// }
324+
325+
// packageLocation := tspath.ResolvePath(basePath, pnpApi.GetPackage(targetLocator).PackageLocation)
326+
327+
// compareOptions := tspath.ComparePathsOptions{
328+
// UseCaseSensitiveFileNames: p.host.FS().UseCaseSensitiveFileNames(),
329+
// CurrentDirectory: basePath,
330+
// }
331+
332+
// request := tspath.CombinePaths(targetLocator.Name, tspath.GetRelativePathFromDirectory(packageLocation, path, compareOptions))
333+
// unqualified, err := pnpApi.ResolveToUnqualified(request, basePath+"/")
334+
// if err != nil {
335+
// return path
336+
// }
337+
338+
// return unqualified
339+
// }
340+
341+
// for _, ref := range config.ProjectReferences() {
342+
// ref.Path = getPnpPath(ref.Path)
343+
// }
344+
345+
// fmt.Println("Project refs after")
346+
// for _, ref := range config.ProjectReferences() {
347+
// fmt.Println("Project ref", ref.Path)
348+
// }
315349
// }
316350

317-
// const basePath = this.getCurrentDirectory();
318-
319-
// const getPnpPath = (path: string) => {
320-
// try {
321-
// const pnpApi = getPnpApi(`${path}/`);
322-
// if (!pnpApi) {
323-
// return path;
324-
// }
325-
// const targetLocator = pnpApi.findPackageLocator(`${path}/`);
326-
// const { packageLocation } = pnpApi.getPackageInformation(targetLocator);
327-
// const request = combinePaths(targetLocator.name, getRelativePathFromDirectory(packageLocation, path, /*ignoreCase*/ false));
328-
// return pnpApi.resolveToUnqualified(request, `${basePath}/`);
329-
// }
330-
// catch {
331-
// // something went wrong with the resolution, try not to fail
332-
// return path;
333-
// }
334-
// };
335-
336-
// Then run getPnpPath on all resolvedReferences from acquireConfig()
337-
338-
return p.host.ConfigFileRegistry().acquireConfig(fileName, path, p, nil)
351+
return config
339352
}
340353

341354
// Updates the program if needed.

0 commit comments

Comments
 (0)