Skip to content

Commit 68d3605

Browse files
committed
PR feedback
1 parent 38f3a2b commit 68d3605

20 files changed

+472
-431
lines changed

src/compiler/builder.ts

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,23 @@ namespace ts {
4343
}
4444

4545
interface EmitHandler {
46-
addScriptInfo(program: Program, sourceFile: SourceFile): void;
47-
removeScriptInfo(path: Path): void;
48-
updateScriptInfo(program: Program, sourceFile: SourceFile): void;
49-
updaterScriptInfoWithSameVersion(program: Program, sourceFile: SourceFile): boolean;
46+
/**
47+
* Called when sourceFile is added to the program
48+
*/
49+
onAddSourceFile(program: Program, sourceFile: SourceFile): void;
50+
/**
51+
* Called when sourceFile is removed from the program
52+
*/
53+
onRemoveSourceFile(path: Path): void;
54+
/**
55+
* Called when sourceFile is changed
56+
*/
57+
onUpdateSourceFile(program: Program, sourceFile: SourceFile): void;
58+
/**
59+
* Called when source file has not changed but has some of the resolutions invalidated
60+
* If returned true, builder will mark the file as changed (noting that something associated with file has changed)
61+
*/
62+
onUpdateSourceFileWithSameVersion(program: Program, sourceFile: SourceFile): boolean;
5063
/**
5164
* Gets the files affected by the script info which has updated shape from the known one
5265
*/
@@ -135,23 +148,23 @@ namespace ts {
135148

136149
function addNewFileInfo(program: Program, sourceFile: SourceFile): FileInfo {
137150
registerChangedFile(sourceFile.path, sourceFile.fileName);
138-
emitHandler.addScriptInfo(program, sourceFile);
151+
emitHandler.onAddSourceFile(program, sourceFile);
139152
return { fileName: sourceFile.fileName, version: sourceFile.version, signature: undefined };
140153
}
141154

142155
function removeExistingFileInfo(existingFileInfo: FileInfo, path: Path) {
143156
registerChangedFile(path, existingFileInfo.fileName);
144-
emitHandler.removeScriptInfo(path);
157+
emitHandler.onRemoveSourceFile(path);
145158
}
146159

147160
function updateExistingFileInfo(program: Program, existingInfo: FileInfo, sourceFile: SourceFile, hasInvalidatedResolution: HasInvalidatedResolution) {
148161
if (existingInfo.version !== sourceFile.version) {
149162
registerChangedFile(sourceFile.path, sourceFile.fileName);
150163
existingInfo.version = sourceFile.version;
151-
emitHandler.updateScriptInfo(program, sourceFile);
164+
emitHandler.onUpdateSourceFile(program, sourceFile);
152165
}
153166
else if (hasInvalidatedResolution(sourceFile.path) &&
154-
emitHandler.updaterScriptInfoWithSameVersion(program, sourceFile)) {
167+
emitHandler.onUpdateSourceFileWithSameVersion(program, sourceFile)) {
155168
registerChangedFile(sourceFile.path, sourceFile.fileName);
156169
}
157170
}
@@ -388,10 +401,10 @@ namespace ts {
388401

389402
function getNonModuleEmitHandler(): EmitHandler {
390403
return {
391-
addScriptInfo: noop,
392-
removeScriptInfo: noop,
393-
updateScriptInfo: noop,
394-
updaterScriptInfoWithSameVersion: returnFalse,
404+
onAddSourceFile: noop,
405+
onRemoveSourceFile: noop,
406+
onUpdateSourceFile: noop,
407+
onUpdateSourceFileWithSameVersion: returnFalse,
395408
getFilesAffectedByUpdatedShape
396409
};
397410

@@ -409,17 +422,17 @@ namespace ts {
409422
function getModuleEmitHandler(): EmitHandler {
410423
const references = createMap<Map<true>>();
411424
return {
412-
addScriptInfo: setReferences,
413-
removeScriptInfo,
414-
updateScriptInfo: updateReferences,
415-
updaterScriptInfoWithSameVersion: updateReferencesTrackingChangedReferences,
425+
onAddSourceFile: setReferences,
426+
onRemoveSourceFile,
427+
onUpdateSourceFile: updateReferences,
428+
onUpdateSourceFileWithSameVersion: updateReferencesTrackingChangedReferences,
416429
getFilesAffectedByUpdatedShape
417430
};
418431

419432
function setReferences(program: Program, sourceFile: SourceFile) {
420433
const newReferences = getReferencedFiles(program, sourceFile);
421434
if (newReferences) {
422-
references.set(sourceFile.path, getReferencedFiles(program, sourceFile));
435+
references.set(sourceFile.path, newReferences);
423436
}
424437
}
425438

@@ -452,7 +465,7 @@ namespace ts {
452465
!!oldReferences.size;
453466
}
454467

455-
function removeScriptInfo(removedFilePath: Path) {
468+
function onRemoveSourceFile(removedFilePath: Path) {
456469
// Remove existing references
457470
references.forEach((referencesInFile, filePath) => {
458471
if (referencesInFile.has(removedFilePath)) {

src/compiler/commandLineParser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1683,7 +1683,7 @@ namespace ts {
16831683
return undefined;
16841684
}
16851685
let extendedConfigPath = toPath(extendedConfig, basePath, getCanonicalFileName);
1686-
if (!host.fileExists(extendedConfigPath) && !endsWith(extendedConfigPath, ".json")) {
1686+
if (!host.fileExists(extendedConfigPath) && !endsWith(extendedConfigPath, Extension.Json)) {
16871687
extendedConfigPath = `${extendedConfigPath}.json` as Path;
16881688
if (!host.fileExists(extendedConfigPath)) {
16891689
errors.push(createDiagnostic(Diagnostics.File_0_does_not_exist, extendedConfig));

src/compiler/core.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2262,7 +2262,7 @@ namespace ts {
22622262
return ScriptKind.TS;
22632263
case Extension.Tsx:
22642264
return ScriptKind.TSX;
2265-
case ".json":
2265+
case Extension.Json:
22662266
return ScriptKind.JSON;
22672267
default:
22682268
return ScriptKind.Unknown;
@@ -2669,7 +2669,7 @@ namespace ts {
26692669
export function assertTypeIsNever(_: never): void { }
26702670

26712671
export interface CachedDirectoryStructureHost extends DirectoryStructureHost {
2672-
addOrDeleteFileOrFolder(fileOrFolder: string, fileOrFolderPath: Path): void;
2672+
addOrDeleteFileOrDirectory(fileOrDirectory: string, fileOrDirectoryPath: Path): void;
26732673
addOrDeleteFile(fileName: string, filePath: Path, eventKind: FileWatcherEventKind): void;
26742674
clearCache(): void;
26752675
}
@@ -2695,7 +2695,7 @@ namespace ts {
26952695
getCurrentDirectory,
26962696
getDirectories,
26972697
readDirectory,
2698-
addOrDeleteFileOrFolder,
2698+
addOrDeleteFileOrDirectory,
26992699
addOrDeleteFile,
27002700
clearCache,
27012701
exit: code => host.exit(code)
@@ -2824,23 +2824,23 @@ namespace ts {
28242824
}
28252825
}
28262826

2827-
function addOrDeleteFileOrFolder(fileOrFolder: string, fileOrFolderPath: Path) {
2828-
const existingResult = getCachedFileSystemEntries(fileOrFolderPath);
2827+
function addOrDeleteFileOrDirectory(fileOrDirectory: string, fileOrDirectoryPath: Path) {
2828+
const existingResult = getCachedFileSystemEntries(fileOrDirectoryPath);
28292829
if (existingResult) {
28302830
// This was a folder already present, remove it if this doesnt exist any more
2831-
if (!host.directoryExists(fileOrFolder)) {
2832-
cachedReadDirectoryResult.delete(fileOrFolderPath);
2831+
if (!host.directoryExists(fileOrDirectory)) {
2832+
cachedReadDirectoryResult.delete(fileOrDirectoryPath);
28332833
}
28342834
}
28352835
else {
28362836
// This was earlier a file (hence not in cached directory contents)
28372837
// or we never cached the directory containing it
2838-
const parentResult = getCachedFileSystemEntriesForBaseDir(fileOrFolderPath);
2838+
const parentResult = getCachedFileSystemEntriesForBaseDir(fileOrDirectoryPath);
28392839
if (parentResult) {
2840-
const baseName = getBaseNameOfFileName(fileOrFolder);
2840+
const baseName = getBaseNameOfFileName(fileOrDirectory);
28412841
if (parentResult) {
2842-
updateFilesOfFileSystemEntry(parentResult, baseName, host.fileExists(fileOrFolderPath));
2843-
updateFileSystemEntry(parentResult.directories, baseName, host.directoryExists(fileOrFolderPath));
2842+
updateFilesOfFileSystemEntry(parentResult, baseName, host.fileExists(fileOrDirectoryPath));
2843+
updateFileSystemEntry(parentResult.directories, baseName, host.directoryExists(fileOrDirectoryPath));
28442844
}
28452845
}
28462846
}

src/compiler/program.ts

Lines changed: 7 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -395,17 +395,20 @@ namespace ts {
395395
allDiagnostics?: Diagnostic[];
396396
}
397397

398+
/**
399+
* Determines if program structure is upto date or needs to be recreated
400+
*/
398401
export function isProgramUptoDate(
399402
program: Program | undefined,
400403
rootFileNames: string[],
401404
newOptions: CompilerOptions,
402405
getSourceVersion: (path: Path) => string,
403406
fileExists: (fileName: string) => boolean,
404407
hasInvalidatedResolution: HasInvalidatedResolution,
405-
hasChangedAutomaticTypeDirectiveNames: () => boolean,
408+
hasChangedAutomaticTypeDirectiveNames: boolean,
406409
): boolean {
407410
// If we haven't create a program yet or has changed automatic type directives, then it is not up-to-date
408-
if (!program || hasChangedAutomaticTypeDirectiveNames()) {
411+
if (!program || hasChangedAutomaticTypeDirectiveNames) {
409412
return false;
410413
}
411414

@@ -416,7 +419,7 @@ namespace ts {
416419

417420
// If any file is not up-to-date, then the whole program is not up-to-date
418421
if (program.getSourceFiles().some(sourceFileNotUptoDate)) {
419-
return false;
422+
return false;
420423
}
421424

422425
// If any of the missing file paths are now created
@@ -464,78 +467,6 @@ namespace ts {
464467
);
465468
}
466469

467-
/**
468-
* Updates the existing missing file watches with the new set of missing files after new program is created
469-
*/
470-
export function updateMissingFilePathsWatch(
471-
program: Program,
472-
missingFileWatches: Map<FileWatcher>,
473-
createMissingFileWatch: (missingFilePath: Path) => FileWatcher,
474-
) {
475-
const missingFilePaths = program.getMissingFilePaths();
476-
const newMissingFilePathMap = arrayToSet(missingFilePaths);
477-
// Update the missing file paths watcher
478-
mutateMap(
479-
missingFileWatches,
480-
newMissingFilePathMap,
481-
{
482-
// Watch the missing files
483-
createNewValue: createMissingFileWatch,
484-
// Files that are no longer missing (e.g. because they are no longer required)
485-
// should no longer be watched.
486-
onDeleteValue: closeFileWatcher
487-
}
488-
);
489-
}
490-
491-
export interface WildcardDirectoryWatcher {
492-
watcher: FileWatcher;
493-
flags: WatchDirectoryFlags;
494-
}
495-
496-
/**
497-
* Updates the existing wild card directory watches with the new set of wild card directories from the config file
498-
* after new program is created because the config file was reloaded or program was created first time from the config file
499-
* Note that there is no need to call this function when the program is updated with additional files without reloading config files,
500-
* as wildcard directories wont change unless reloading config file
501-
*/
502-
export function updateWatchingWildcardDirectories(
503-
existingWatchedForWildcards: Map<WildcardDirectoryWatcher>,
504-
wildcardDirectories: Map<WatchDirectoryFlags>,
505-
watchDirectory: (directory: string, flags: WatchDirectoryFlags) => FileWatcher
506-
) {
507-
mutateMap(
508-
existingWatchedForWildcards,
509-
wildcardDirectories,
510-
{
511-
// Create new watch and recursive info
512-
createNewValue: createWildcardDirectoryWatcher,
513-
// Close existing watch thats not needed any more
514-
onDeleteValue: closeFileWatcherOf,
515-
// Close existing watch that doesnt match in the flags
516-
onExistingValue: updateWildcardDirectoryWatcher
517-
}
518-
);
519-
520-
function createWildcardDirectoryWatcher(directory: string, flags: WatchDirectoryFlags): WildcardDirectoryWatcher {
521-
// Create new watch and recursive info
522-
return {
523-
watcher: watchDirectory(directory, flags),
524-
flags
525-
};
526-
}
527-
528-
function updateWildcardDirectoryWatcher(existingWatcher: WildcardDirectoryWatcher, flags: WatchDirectoryFlags, directory: string) {
529-
// Watcher needs to be updated if the recursive flags dont match
530-
if (existingWatcher.flags === flags) {
531-
return;
532-
}
533-
534-
existingWatcher.watcher.close();
535-
existingWatchedForWildcards.set(directory, createWildcardDirectoryWatcher(directory, flags));
536-
}
537-
}
538-
539470
/**
540471
* Create a new 'Program' instance. A Program is an immutable collection of 'SourceFile's and a 'CompilerOptions'
541472
* that represent a compilation unit.
@@ -599,7 +530,6 @@ namespace ts {
599530
let moduleResolutionCache: ModuleResolutionCache;
600531
let resolveModuleNamesWorker: (moduleNames: string[], containingFile: string, reusedNames?: string[]) => ResolvedModuleFull[];
601532
const hasInvalidatedResolution = host.hasInvalidatedResolution || returnFalse;
602-
const hasChangedAutomaticTypeDirectiveNames = host.hasChangedAutomaticTypeDirectiveNames && host.hasChangedAutomaticTypeDirectiveNames.bind(host) || returnFalse;
603533
if (host.resolveModuleNames) {
604534
resolveModuleNamesWorker = (moduleNames, containingFile, reusedNames) => host.resolveModuleNames(checkAllDefined(moduleNames), containingFile, reusedNames).map(resolved => {
605535
// An older host may have omitted extension, in which case we should infer it from the file extension of resolvedFileName.
@@ -1105,7 +1035,7 @@ namespace ts {
11051035
return oldProgram.structureIsReused;
11061036
}
11071037

1108-
if (hasChangedAutomaticTypeDirectiveNames()) {
1038+
if (host.hasChangedAutomaticTypeDirectiveNames) {
11091039
return oldProgram.structureIsReused = StructureIsReused.SafeModules;
11101040
}
11111041

0 commit comments

Comments
 (0)