mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-04 21:19:30 -06:00
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>
This commit is contained in:
parent
95e2cd01ee
commit
25b06e826d
@ -808,56 +808,44 @@ export function resolvePackageNameToPackageJson(
|
||||
* this list is only the set of defaults that are implicitly included.
|
||||
*/
|
||||
export function getAutomaticTypeDirectiveNames(options: CompilerOptions, host: ModuleResolutionHost): string[] {
|
||||
// Use explicit type list from tsconfig.json
|
||||
if (options.types) {
|
||||
// Check if the special "*" value is present, which means "include all from typeRoots"
|
||||
const hasWildcard = options.types.includes("*");
|
||||
if (hasWildcard) {
|
||||
// Enumerate all packages from typeRoots
|
||||
const result: string[] = [];
|
||||
if (host.directoryExists && host.getDirectories) {
|
||||
const typeRoots = getEffectiveTypeRoots(options, host);
|
||||
if (typeRoots) {
|
||||
for (const root of typeRoots) {
|
||||
if (host.directoryExists(root)) {
|
||||
for (const typeDirectivePath of host.getDirectories(root)) {
|
||||
const normalized = normalizePath(typeDirectivePath);
|
||||
const packageJsonPath = combinePaths(root, normalized, "package.json");
|
||||
// `types-publisher` sometimes creates packages with `"typings": null` for packages that don't provide their own types.
|
||||
// See `createNotNeededPackageJSON` in the types-publisher` repo.
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
const isNotNeededPackage = host.fileExists(packageJsonPath) && (readJson(packageJsonPath, host) as PackageJson).typings === null;
|
||||
if (!isNotNeededPackage) {
|
||||
const baseFileName = getBaseFileName(normalized);
|
||||
if (!hasWildcard) {
|
||||
return options.types;
|
||||
}
|
||||
|
||||
// At this stage, skip results with leading dot.
|
||||
if (baseFileName.charCodeAt(0) !== CharacterCodes.dot) {
|
||||
// Return just the type directive names
|
||||
result.push(baseFileName);
|
||||
}
|
||||
const result: string[] = [];
|
||||
if (host.directoryExists && host.getDirectories) {
|
||||
const typeRoots = getEffectiveTypeRoots(options, host);
|
||||
if (typeRoots) {
|
||||
for (const root of typeRoots) {
|
||||
if (host.directoryExists(root)) {
|
||||
for (const typeDirectivePath of host.getDirectories(root)) {
|
||||
const normalized = normalizePath(typeDirectivePath);
|
||||
const packageJsonPath = combinePaths(root, normalized, "package.json");
|
||||
// `types-publisher` sometimes creates packages with `"typings": null` for packages that don't provide their own types.
|
||||
// See `createNotNeededPackageJSON` in the types-publisher` repo.
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
const isNotNeededPackage = host.fileExists(packageJsonPath) && (readJson(packageJsonPath, host) as PackageJson).typings === null;
|
||||
if (!isNotNeededPackage) {
|
||||
const baseFileName = getBaseFileName(normalized);
|
||||
if (baseFileName.charCodeAt(0) !== CharacterCodes.dot) {
|
||||
result.push(baseFileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Add any explicitly listed types that aren't already included (and aren't the wildcard itself)
|
||||
for (const type of options.types) {
|
||||
if (type !== "*" && !result.includes(type)) {
|
||||
result.push(type);
|
||||
}
|
||||
}
|
||||
for (const type of options.types) {
|
||||
if (type !== "*" && !result.includes(type)) {
|
||||
result.push(type);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
else {
|
||||
return options.types;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
else {
|
||||
// When types is undefined (not specified in tsconfig), default to empty array
|
||||
// This is a breaking change from the previous behavior which included all @types packages
|
||||
return emptyArray;
|
||||
}
|
||||
return emptyArray;
|
||||
}
|
||||
|
||||
export interface TypeReferenceDirectiveResolutionCache extends PerDirectoryResolutionCache<ResolvedTypeReferenceDirectiveWithFailedLookupLocations>, NonRelativeNameResolutionCache<ResolvedTypeReferenceDirectiveWithFailedLookupLocations>, PackageJsonInfoCache {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user