Skip to content

Commit 9cd3e93

Browse files
committed
Create Noop watcher if file or directory being watched is excluded
1 parent ac325de commit 9cd3e93

File tree

3 files changed

+71
-3
lines changed

3 files changed

+71
-3
lines changed

src/compiler/commandLineParser.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3109,7 +3109,32 @@ namespace ts {
31093109
}
31103110
}
31113111

3112-
const excludePattern = getRegularExpressionForWildcard(validatedExcludeSpecs, combinePaths(normalizePath(currentDirectory), basePath), "exclude");
3112+
return matchesExcludeWorker(pathToCheck, validatedExcludeSpecs, useCaseSensitiveFileNames, currentDirectory, basePath);
3113+
}
3114+
3115+
/* @internal */
3116+
export function matchesExclude(
3117+
pathToCheck: string,
3118+
excludeSpecs: readonly string[] | undefined,
3119+
useCaseSensitiveFileNames: boolean,
3120+
currentDirectory: string
3121+
) {
3122+
return matchesExcludeWorker(
3123+
pathToCheck,
3124+
filter(excludeSpecs, spec => !invalidDotDotAfterRecursiveWildcardPattern.test(spec)),
3125+
useCaseSensitiveFileNames,
3126+
currentDirectory
3127+
);
3128+
}
3129+
3130+
function matchesExcludeWorker(
3131+
pathToCheck: string,
3132+
excludeSpecs: readonly string[] | undefined,
3133+
useCaseSensitiveFileNames: boolean,
3134+
currentDirectory: string,
3135+
basePath?: string
3136+
) {
3137+
const excludePattern = getRegularExpressionForWildcard(excludeSpecs, combinePaths(normalizePath(currentDirectory), basePath), "exclude");
31133138
const excludeRegex = excludePattern && getRegexFromPattern(excludePattern, useCaseSensitiveFileNames);
31143139
if (!excludeRegex) return false;
31153140
if (excludeRegex.test(pathToCheck)) return true;

src/compiler/watchUtilities.ts

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,8 @@ namespace ts {
423423
export interface WatchFactoryHost {
424424
watchFile(path: string, callback: FileWatcherCallback, pollingInterval?: number, options?: WatchOptions): FileWatcher;
425425
watchDirectory(path: string, callback: DirectoryWatcherCallback, recursive?: boolean, options?: WatchOptions): FileWatcher;
426+
getCurrentDirectory?(): string;
427+
useCaseSensitiveFileNames: boolean | (() => boolean);
426428
}
427429

428430
export interface WatchFactory<X, Y = undefined> {
@@ -445,12 +447,52 @@ namespace ts {
445447
watchDirectory: createTriggerLoggingAddWatch("watchDirectory")
446448
} :
447449
undefined;
448-
return watchLogLevel === WatchLogLevel.Verbose ?
450+
const factory = watchLogLevel === WatchLogLevel.Verbose ?
449451
{
450452
watchFile: createFileWatcherWithLogging,
451453
watchDirectory: createDirectoryWatcherWithLogging
452454
} :
453455
triggerInvokingFactory || plainInvokeFactory;
456+
const excludeWatcherFactory = watchLogLevel === WatchLogLevel.Verbose ?
457+
createExcludeWatcherWithLogging :
458+
returnNoopFileWatcher;
459+
460+
return {
461+
watchFile: createExcludeHandlingAddWatch("watchFile"),
462+
watchDirectory: createExcludeHandlingAddWatch("watchDirectory")
463+
};
464+
465+
function createExcludeHandlingAddWatch<T extends keyof WatchFactory<X, Y>>(key: T): WatchFactory<X, Y>[T] {
466+
return (
467+
file: string,
468+
cb: FileWatcherCallback | DirectoryWatcherCallback,
469+
flags: PollingInterval | WatchDirectoryFlags,
470+
options: WatchOptions | undefined,
471+
detailInfo1: X,
472+
detailInfo2?: Y
473+
) => !matchesExclude(file, key === "watchFile" ? options?.excludeFiles : options?.excludeDirectories, useCaseSensitiveFileNames(), host.getCurrentDirectory?.() || "") ?
474+
factory[key].call(/*thisArgs*/ undefined, file, cb, flags, options, detailInfo1, detailInfo2) :
475+
excludeWatcherFactory(file, flags, options, detailInfo1, detailInfo2);
476+
}
477+
478+
function useCaseSensitiveFileNames() {
479+
return typeof host.useCaseSensitiveFileNames === "boolean" ?
480+
host.useCaseSensitiveFileNames :
481+
host.useCaseSensitiveFileNames();
482+
}
483+
484+
function createExcludeWatcherWithLogging(
485+
file: string,
486+
flags: PollingInterval | WatchDirectoryFlags,
487+
options: WatchOptions | undefined,
488+
detailInfo1: X,
489+
detailInfo2?: Y
490+
) {
491+
log(`ExcludeWatcher:: Added:: ${getWatchInfo(file, flags, options, detailInfo1, detailInfo2, getDetailWatchInfo)}`);
492+
return {
493+
close: () => log(`ExcludeWatcher:: Close:: ${getWatchInfo(file, flags, options, detailInfo1, detailInfo2, getDetailWatchInfo)}`)
494+
};
495+
}
454496

455497
function createFileWatcherWithLogging(
456498
file: string,
@@ -496,7 +538,7 @@ namespace ts {
496538
};
497539
}
498540

499-
function createTriggerLoggingAddWatch<T extends keyof WatchFactory<X, Y>>(key: T,): WatchFactory<X, Y>[T] {
541+
function createTriggerLoggingAddWatch<T extends keyof WatchFactory<X, Y>>(key: T): WatchFactory<X, Y>[T] {
500542
return (
501543
file: string,
502544
cb: FileWatcherCallback | DirectoryWatcherCallback,

src/jsTyping/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ declare namespace ts.server {
8383
useCaseSensitiveFileNames: boolean;
8484
writeFile(path: string, content: string): void;
8585
createDirectory(path: string): void;
86+
getCurrentDirectory?(): string;
8687
watchFile?(path: string, callback: FileWatcherCallback, pollingInterval?: number, options?: WatchOptions): FileWatcher;
8788
watchDirectory?(path: string, callback: DirectoryWatcherCallback, recursive?: boolean, options?: WatchOptions): FileWatcher;
8889
}

0 commit comments

Comments
 (0)