MCPcopy Index your code
hub / github.com/microsoft/typescript-go / collectPackageNames

Method collectPackageNames

internal/compiler/program.go:1948–2003  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1946}
1947
1948func (p *Program) collectPackageNames() *packageNamesInfo {
1949 return p.packageNames.getValue(func() *packageNamesInfo {
1950 packageNames := &packageNamesInfo{&collections.Set[string]{}, &collections.Set[string]{}, &collections.Set[string]{}}
1951 for _, file := range p.files {
1952 if p.IsSourceFileDefaultLibrary(file.Path()) || p.IsSourceFileFromExternalLibrary(file) || strings.Contains(file.FileName(), "/node_modules/") {
1953 // Checking for /node_modules/ is a little imprecise, but ATA treats locally installed typings
1954 // as root files, which would not pass IsSourceFileFromExternalLibrary.
1955 continue
1956 }
1957 for _, imp := range file.Imports() {
1958 if tspath.IsExternalModuleNameRelative(imp.Text()) {
1959 continue
1960 }
1961 if resolvedModules, ok := p.resolvedModules[file.Path()]; ok {
1962 key := module.ModeAwareCacheKey{Name: imp.Text(), Mode: p.GetModeForUsageLocation(file, imp)}
1963 if resolvedModule, ok := resolvedModules[key]; ok && resolvedModule.IsResolved() {
1964 if !resolvedModule.IsExternalLibraryImport {
1965 continue
1966 }
1967 // Priority order for getting package name:
1968 // 1. PackageId.Name (requires both name and version in package.json)
1969 name := resolvedModule.PackageId.Name
1970 if name == "" {
1971 // 2. GetPackageScopeForPath - get name from package.json in the package directory
1972 if packageScope := p.resolver.GetPackageScopeForPath(resolvedModule.ResolvedFileName); packageScope != nil && packageScope.Exists() {
1973 if scopeName, ok := packageScope.Contents.Name.GetValue(); ok {
1974 name = scopeName
1975 }
1976 }
1977 }
1978 if name == "" {
1979 // 3. GetPackageNameFromDirectory - extract from node_modules path
1980 name = modulespecifiers.GetPackageNameFromDirectory(resolvedModule.ResolvedFileName)
1981 }
1982 // 4. If all fail, don't add empty string
1983 if name != "" {
1984 packageNames.resolved.Add(name)
1985 // Detect deep imports: subpath imports in packages without exports.
1986 // These are imports like "lodash/fp" where the package has no exports
1987 // map, so auto-import can only find them via recursive directory search.
1988 _, rest := module.ParsePackageName(imp.Text())
1989 if rest != "" {
1990 if scope := p.resolver.GetPackageScopeForPath(resolvedModule.ResolvedFileName); scope != nil && scope.Exists() && !scope.Contents.Exports.IsPresent() {
1991 packageNames.deepImportPackages.Add(module.GetPackageNameFromTypesPackageName(name))
1992 }
1993 }
1994 }
1995 continue
1996 }
1997 }
1998 packageNames.unresolved.Add(imp.Text())
1999 }
2000 }
2001 return packageNames
2002 })
2003}
2004
2005func (p *Program) IsLibFile(sourceFile *ast.SourceFile) bool {

Callers 3

ResolvedPackageNamesMethod · 0.95

Calls 15

ParsePackageNameFunction · 0.92
GetValueMethod · 0.80
getValueMethod · 0.65
PathMethod · 0.65
ContainsMethod · 0.65

Tested by

no test coverage detected