Skip to content

Commit 73f106f

Browse files
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>
1 parent a2b2a62 commit 73f106f

File tree

6 files changed

+22
-10
lines changed

6 files changed

+22
-10
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,7 @@ import {
11131113
TypeReferenceNode,
11141114
TypeReferenceSerializationKind,
11151115
TypeReferenceType,
1116+
typesIncludesWildcard,
11161117
TypeVariable,
11171118
unescapeLeadingUnderscores,
11181119
UnionOrIntersectionType,
@@ -27628,25 +27629,25 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2762827629
case "console":
2762927630
return Diagnostics.Cannot_find_name_0_Do_you_need_to_change_your_target_library_Try_changing_the_lib_compiler_option_to_include_dom;
2763027631
case "$":
27631-
return compilerOptions.types?.includes("*")
27632+
return typesIncludesWildcard(compilerOptions.types)
2763227633
? Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_jQuery_Try_npm_i_save_dev_types_Slashjquery
2763327634
: 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;
2763427635
case "describe":
2763527636
case "suite":
2763627637
case "it":
2763727638
case "test":
27638-
return compilerOptions.types?.includes("*")
27639+
return typesIncludesWildcard(compilerOptions.types)
2763927640
? 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
2764027641
: 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;
2764127642
case "process":
2764227643
case "require":
2764327644
case "Buffer":
2764427645
case "module":
27645-
return compilerOptions.types?.includes("*")
27646+
return typesIncludesWildcard(compilerOptions.types)
2764627647
? Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_node_Try_npm_i_save_dev_types_Slashnode
2764727648
: 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;
2764827649
case "Bun":
27649-
return compilerOptions.types?.includes("*")
27650+
return typesIncludesWildcard(compilerOptions.types)
2765027651
? Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_Bun_Try_npm_i_save_dev_types_Slashbun
2765127652
: 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;
2765227653
case "Map":

src/compiler/moduleNameResolver.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,14 @@ export function resolvePackageNameToPackageJson(
801801
});
802802
}
803803

804+
/**
805+
* Returns true if the types compiler option includes the "*" wildcard.
806+
* @internal
807+
*/
808+
export function typesIncludesWildcard(types: readonly string[] | undefined): boolean {
809+
return types?.includes("*") ?? false;
810+
}
811+
804812
/**
805813
* Given a set of options, returns the set of type directive names
806814
* that should be included for this program automatically.
@@ -815,7 +823,7 @@ export function getAutomaticTypeDirectiveNames(options: CompilerOptions, host: M
815823
return emptyArray;
816824
}
817825

818-
if (!options.types.includes("*")) {
826+
if (!typesIncludesWildcard(options.types)) {
819827
// No wildcard, no need to iterate anything
820828
return options.types;
821829
}

src/compiler/programDiagnostics.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import {
5252
removeSuffix,
5353
SourceFile,
5454
TsConfigSourceFile,
55+
typesIncludesWildcard,
5556
} from "./_namespaces/ts.js";
5657

5758
interface FileReasonToChainCache {
@@ -400,7 +401,7 @@ export function createProgramDiagnostics(getCompilerOptionsObjectLiteralSyntax:
400401
) :
401402
undefined;
402403
case FileIncludeKind.AutomaticTypeDirectiveFile:
403-
if (options.types?.includes("*")) return undefined;
404+
if (typesIncludesWildcard(options.types)) return undefined;
404405
configFileNode = getOptionsSyntaxByArrayElementValue(getCompilerOptionsObjectLiteralSyntax(), "types", reason.typeReference);
405406
message = Diagnostics.File_is_entry_point_of_type_library_specified_here;
406407
break;

src/compiler/resolutionCache.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ import {
7373
startsWith,
7474
StringLiteralLike,
7575
trace,
76+
typesIncludesWildcard,
7677
updateResolutionField,
7778
WatchDirectoryFlags,
7879
} from "./_namespaces/ts.js";
@@ -1667,7 +1668,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
16671668
*/
16681669
function updateTypeRootsWatch() {
16691670
const options = resolutionHost.getCompilationSettings();
1670-
if (options.types && !options.types.includes("*")) {
1671+
if (options.types && !typesIncludesWildcard(options.types)) {
16711672
// No need to do any watch since resolution cache is going to handle the failed lookups
16721673
// for the types added by this
16731674
closeTypeRootsWatch();

src/compiler/watch.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ import {
9797
sourceMapCommentRegExpDontCareLineStart,
9898
sys,
9999
System,
100+
typesIncludesWildcard,
100101
WatchCompilerHost,
101102
WatchCompilerHostOfConfigFile,
102103
WatchCompilerHostOfFilesAndCompilerOptions,
@@ -529,7 +530,7 @@ export function fileIncludeReasonToDiagnostics(program: Program, reason: FileInc
529530
options.outFile ? "--outFile" : "--out",
530531
);
531532
case FileIncludeKind.AutomaticTypeDirectiveFile: {
532-
const messageAndArgs: DiagnosticAndArguments = options.types?.includes("*") ?
533+
const messageAndArgs: DiagnosticAndArguments = typesIncludesWildcard(options.types) ?
533534
reason.packageId ?
534535
[Diagnostics.Entry_point_for_implicit_type_library_0_with_packageId_1, reason.typeReference, packageIdToString(reason.packageId)] :
535536
[Diagnostics.Entry_point_for_implicit_type_library_0, reason.typeReference] :

src/jsTyping/jsTyping.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
some,
2929
toFileNameLowerCase,
3030
TypeAcquisition,
31+
typesIncludesWildcard,
3132
Version,
3233
versionMajorMinor,
3334
} from "./_namespaces/ts.js";
@@ -133,8 +134,7 @@ export function discoverTypings(
133134
const exclude = typeAcquisition.exclude || [];
134135

135136
// Directories to search for package.json, bower.json and other typing information
136-
if (compilerOptions.types?.includes("*")) {
137-
137+
if (typesIncludesWildcard(compilerOptions.types)) {
138138
const possibleSearchDirs = new Set(fileNames.map(getDirectoryPath));
139139
possibleSearchDirs.add(projectRootPath);
140140
possibleSearchDirs.forEach(searchDir => {

0 commit comments

Comments
 (0)