mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-04 03:09:39 -06:00
Extract wildcard check into typesIncludesWildcard helper
- Added typesIncludesWildcard() function in moduleNameResolver.ts - Updated all wildcard checks to use the helper function: - checker.ts: getCannotFindNameDiagnosticForName - programDiagnostics.ts: FileIncludeKind.AutomaticTypeDirectiveFile - watch.ts: fileIncludeReasonToRelatedInformation - resolutionCache.ts: updateTypeRootsWatch - jsTyping.ts: discoverTypings - Added imports in all consuming files This centralizes the wildcard checking logic and makes the code more maintainable. Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
This commit is contained in:
parent
a2b2a62d18
commit
73f106fc15
@ -1113,6 +1113,7 @@ import {
|
||||
TypeReferenceNode,
|
||||
TypeReferenceSerializationKind,
|
||||
TypeReferenceType,
|
||||
typesIncludesWildcard,
|
||||
TypeVariable,
|
||||
unescapeLeadingUnderscores,
|
||||
UnionOrIntersectionType,
|
||||
@ -27628,25 +27629,25 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
case "console":
|
||||
return Diagnostics.Cannot_find_name_0_Do_you_need_to_change_your_target_library_Try_changing_the_lib_compiler_option_to_include_dom;
|
||||
case "$":
|
||||
return compilerOptions.types?.includes("*")
|
||||
return typesIncludesWildcard(compilerOptions.types)
|
||||
? Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_jQuery_Try_npm_i_save_dev_types_Slashjquery
|
||||
: Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_jQuery_Try_npm_i_save_dev_types_Slashjquery_and_then_add_jquery_to_the_types_field_in_your_tsconfig;
|
||||
case "describe":
|
||||
case "suite":
|
||||
case "it":
|
||||
case "test":
|
||||
return compilerOptions.types?.includes("*")
|
||||
return typesIncludesWildcard(compilerOptions.types)
|
||||
? Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_a_test_runner_Try_npm_i_save_dev_types_Slashjest_or_npm_i_save_dev_types_Slashmocha
|
||||
: Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_a_test_runner_Try_npm_i_save_dev_types_Slashjest_or_npm_i_save_dev_types_Slashmocha_and_then_add_jest_or_mocha_to_the_types_field_in_your_tsconfig;
|
||||
case "process":
|
||||
case "require":
|
||||
case "Buffer":
|
||||
case "module":
|
||||
return compilerOptions.types?.includes("*")
|
||||
return typesIncludesWildcard(compilerOptions.types)
|
||||
? Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_node_Try_npm_i_save_dev_types_Slashnode
|
||||
: Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_node_Try_npm_i_save_dev_types_Slashnode_and_then_add_node_to_the_types_field_in_your_tsconfig;
|
||||
case "Bun":
|
||||
return compilerOptions.types?.includes("*")
|
||||
return typesIncludesWildcard(compilerOptions.types)
|
||||
? Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_Bun_Try_npm_i_save_dev_types_Slashbun
|
||||
: Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_Bun_Try_npm_i_save_dev_types_Slashbun_and_then_add_bun_to_the_types_field_in_your_tsconfig;
|
||||
case "Map":
|
||||
|
||||
@ -801,6 +801,14 @@ export function resolvePackageNameToPackageJson(
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the types compiler option includes the "*" wildcard.
|
||||
* @internal
|
||||
*/
|
||||
export function typesIncludesWildcard(types: readonly string[] | undefined): boolean {
|
||||
return types?.includes("*") ?? false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a set of options, returns the set of type directive names
|
||||
* that should be included for this program automatically.
|
||||
@ -815,7 +823,7 @@ export function getAutomaticTypeDirectiveNames(options: CompilerOptions, host: M
|
||||
return emptyArray;
|
||||
}
|
||||
|
||||
if (!options.types.includes("*")) {
|
||||
if (!typesIncludesWildcard(options.types)) {
|
||||
// No wildcard, no need to iterate anything
|
||||
return options.types;
|
||||
}
|
||||
|
||||
@ -52,6 +52,7 @@ import {
|
||||
removeSuffix,
|
||||
SourceFile,
|
||||
TsConfigSourceFile,
|
||||
typesIncludesWildcard,
|
||||
} from "./_namespaces/ts.js";
|
||||
|
||||
interface FileReasonToChainCache {
|
||||
@ -400,7 +401,7 @@ export function createProgramDiagnostics(getCompilerOptionsObjectLiteralSyntax:
|
||||
) :
|
||||
undefined;
|
||||
case FileIncludeKind.AutomaticTypeDirectiveFile:
|
||||
if (options.types?.includes("*")) return undefined;
|
||||
if (typesIncludesWildcard(options.types)) return undefined;
|
||||
configFileNode = getOptionsSyntaxByArrayElementValue(getCompilerOptionsObjectLiteralSyntax(), "types", reason.typeReference);
|
||||
message = Diagnostics.File_is_entry_point_of_type_library_specified_here;
|
||||
break;
|
||||
|
||||
@ -73,6 +73,7 @@ import {
|
||||
startsWith,
|
||||
StringLiteralLike,
|
||||
trace,
|
||||
typesIncludesWildcard,
|
||||
updateResolutionField,
|
||||
WatchDirectoryFlags,
|
||||
} from "./_namespaces/ts.js";
|
||||
@ -1667,7 +1668,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
|
||||
*/
|
||||
function updateTypeRootsWatch() {
|
||||
const options = resolutionHost.getCompilationSettings();
|
||||
if (options.types && !options.types.includes("*")) {
|
||||
if (options.types && !typesIncludesWildcard(options.types)) {
|
||||
// No need to do any watch since resolution cache is going to handle the failed lookups
|
||||
// for the types added by this
|
||||
closeTypeRootsWatch();
|
||||
|
||||
@ -97,6 +97,7 @@ import {
|
||||
sourceMapCommentRegExpDontCareLineStart,
|
||||
sys,
|
||||
System,
|
||||
typesIncludesWildcard,
|
||||
WatchCompilerHost,
|
||||
WatchCompilerHostOfConfigFile,
|
||||
WatchCompilerHostOfFilesAndCompilerOptions,
|
||||
@ -529,7 +530,7 @@ export function fileIncludeReasonToDiagnostics(program: Program, reason: FileInc
|
||||
options.outFile ? "--outFile" : "--out",
|
||||
);
|
||||
case FileIncludeKind.AutomaticTypeDirectiveFile: {
|
||||
const messageAndArgs: DiagnosticAndArguments = options.types?.includes("*") ?
|
||||
const messageAndArgs: DiagnosticAndArguments = typesIncludesWildcard(options.types) ?
|
||||
reason.packageId ?
|
||||
[Diagnostics.Entry_point_for_implicit_type_library_0_with_packageId_1, reason.typeReference, packageIdToString(reason.packageId)] :
|
||||
[Diagnostics.Entry_point_for_implicit_type_library_0, reason.typeReference] :
|
||||
|
||||
@ -28,6 +28,7 @@ import {
|
||||
some,
|
||||
toFileNameLowerCase,
|
||||
TypeAcquisition,
|
||||
typesIncludesWildcard,
|
||||
Version,
|
||||
versionMajorMinor,
|
||||
} from "./_namespaces/ts.js";
|
||||
@ -133,8 +134,7 @@ export function discoverTypings(
|
||||
const exclude = typeAcquisition.exclude || [];
|
||||
|
||||
// Directories to search for package.json, bower.json and other typing information
|
||||
if (compilerOptions.types?.includes("*")) {
|
||||
|
||||
if (typesIncludesWildcard(compilerOptions.types)) {
|
||||
const possibleSearchDirs = new Set(fileNames.map(getDirectoryPath));
|
||||
possibleSearchDirs.add(projectRootPath);
|
||||
possibleSearchDirs.forEach(searchDir => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user