Skip to content

Commit 25b06e8

Browse files
Refactor getAutomaticTypeDirectiveNames for clarity
- Early return when hasWildcard is false - Remove explanatory comments as code is self-documenting - Addresses code review feedback Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
1 parent 95e2cd0 commit 25b06e8

File tree

1 file changed

+27
-39
lines changed

1 file changed

+27
-39
lines changed

src/compiler/moduleNameResolver.ts

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -808,56 +808,44 @@ export function resolvePackageNameToPackageJson(
808808
* this list is only the set of defaults that are implicitly included.
809809
*/
810810
export function getAutomaticTypeDirectiveNames(options: CompilerOptions, host: ModuleResolutionHost): string[] {
811-
// Use explicit type list from tsconfig.json
812811
if (options.types) {
813-
// Check if the special "*" value is present, which means "include all from typeRoots"
814812
const hasWildcard = options.types.includes("*");
815-
if (hasWildcard) {
816-
// Enumerate all packages from typeRoots
817-
const result: string[] = [];
818-
if (host.directoryExists && host.getDirectories) {
819-
const typeRoots = getEffectiveTypeRoots(options, host);
820-
if (typeRoots) {
821-
for (const root of typeRoots) {
822-
if (host.directoryExists(root)) {
823-
for (const typeDirectivePath of host.getDirectories(root)) {
824-
const normalized = normalizePath(typeDirectivePath);
825-
const packageJsonPath = combinePaths(root, normalized, "package.json");
826-
// `types-publisher` sometimes creates packages with `"typings": null` for packages that don't provide their own types.
827-
// See `createNotNeededPackageJSON` in the types-publisher` repo.
828-
// eslint-disable-next-line no-restricted-syntax
829-
const isNotNeededPackage = host.fileExists(packageJsonPath) && (readJson(packageJsonPath, host) as PackageJson).typings === null;
830-
if (!isNotNeededPackage) {
831-
const baseFileName = getBaseFileName(normalized);
832-
833-
// At this stage, skip results with leading dot.
834-
if (baseFileName.charCodeAt(0) !== CharacterCodes.dot) {
835-
// Return just the type directive names
836-
result.push(baseFileName);
837-
}
813+
if (!hasWildcard) {
814+
return options.types;
815+
}
816+
817+
const result: string[] = [];
818+
if (host.directoryExists && host.getDirectories) {
819+
const typeRoots = getEffectiveTypeRoots(options, host);
820+
if (typeRoots) {
821+
for (const root of typeRoots) {
822+
if (host.directoryExists(root)) {
823+
for (const typeDirectivePath of host.getDirectories(root)) {
824+
const normalized = normalizePath(typeDirectivePath);
825+
const packageJsonPath = combinePaths(root, normalized, "package.json");
826+
// `types-publisher` sometimes creates packages with `"typings": null` for packages that don't provide their own types.
827+
// See `createNotNeededPackageJSON` in the types-publisher` repo.
828+
// eslint-disable-next-line no-restricted-syntax
829+
const isNotNeededPackage = host.fileExists(packageJsonPath) && (readJson(packageJsonPath, host) as PackageJson).typings === null;
830+
if (!isNotNeededPackage) {
831+
const baseFileName = getBaseFileName(normalized);
832+
if (baseFileName.charCodeAt(0) !== CharacterCodes.dot) {
833+
result.push(baseFileName);
838834
}
839835
}
840836
}
841837
}
842838
}
843839
}
844-
// Add any explicitly listed types that aren't already included (and aren't the wildcard itself)
845-
for (const type of options.types) {
846-
if (type !== "*" && !result.includes(type)) {
847-
result.push(type);
848-
}
849-
}
850-
return result;
851840
}
852-
else {
853-
return options.types;
841+
for (const type of options.types) {
842+
if (type !== "*" && !result.includes(type)) {
843+
result.push(type);
844+
}
854845
}
846+
return result;
855847
}
856-
else {
857-
// When types is undefined (not specified in tsconfig), default to empty array
858-
// This is a breaking change from the previous behavior which included all @types packages
859-
return emptyArray;
860-
}
848+
return emptyArray;
861849
}
862850

863851
export interface TypeReferenceDirectiveResolutionCache extends PerDirectoryResolutionCache<ResolvedTypeReferenceDirectiveWithFailedLookupLocations>, NonRelativeNameResolutionCache<ResolvedTypeReferenceDirectiveWithFailedLookupLocations>, PackageJsonInfoCache {

0 commit comments

Comments
 (0)