Skip to content

Commit 9f3b77a

Browse files
committed
PR feedback
1 parent 520e33f commit 9f3b77a

File tree

8 files changed

+41
-39
lines changed

8 files changed

+41
-39
lines changed

src/compiler/builder.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -717,22 +717,26 @@ namespace ts {
717717
getState: notImplemented,
718718
backupCurrentState: noop,
719719
useBackupState: noop,
720-
getProgram: () => Debug.assertDefined(state.program),
720+
getProgram,
721721
getProgramOrUndefined: () => state.program,
722722
releaseProgram: () => state.program = undefined,
723723
getCompilerOptions: () => state.compilerOptions,
724-
getSourceFile: fileName => Debug.assertDefined(state.program).getSourceFile(fileName),
725-
getSourceFiles: () => Debug.assertDefined(state.program).getSourceFiles(),
726-
getOptionsDiagnostics: cancellationToken => Debug.assertDefined(state.program).getOptionsDiagnostics(cancellationToken),
727-
getGlobalDiagnostics: cancellationToken => Debug.assertDefined(state.program).getGlobalDiagnostics(cancellationToken),
724+
getSourceFile: fileName => getProgram().getSourceFile(fileName),
725+
getSourceFiles: () => getProgram().getSourceFiles(),
726+
getOptionsDiagnostics: cancellationToken => getProgram().getOptionsDiagnostics(cancellationToken),
727+
getGlobalDiagnostics: cancellationToken => getProgram().getGlobalDiagnostics(cancellationToken),
728728
getConfigFileParsingDiagnostics: () => configFileParsingDiagnostics,
729-
getSyntacticDiagnostics: (sourceFile, cancellationToken) => Debug.assertDefined(state.program).getSyntacticDiagnostics(sourceFile, cancellationToken),
730-
getDeclarationDiagnostics: (sourceFile, cancellationToken) => Debug.assertDefined(state.program).getDeclarationDiagnostics(sourceFile, cancellationToken),
731-
getSemanticDiagnostics: (sourceFile, cancellationToken) => Debug.assertDefined(state.program).getSemanticDiagnostics(sourceFile, cancellationToken),
732-
emit: (sourceFile, writeFile, cancellationToken, emitOnlyDts, customTransformers) => Debug.assertDefined(state.program).emit(sourceFile, writeFile, cancellationToken, emitOnlyDts, customTransformers),
729+
getSyntacticDiagnostics: (sourceFile, cancellationToken) => getProgram().getSyntacticDiagnostics(sourceFile, cancellationToken),
730+
getDeclarationDiagnostics: (sourceFile, cancellationToken) => getProgram().getDeclarationDiagnostics(sourceFile, cancellationToken),
731+
getSemanticDiagnostics: (sourceFile, cancellationToken) => getProgram().getSemanticDiagnostics(sourceFile, cancellationToken),
732+
emit: (sourceFile, writeFile, cancellationToken, emitOnlyDts, customTransformers) => getProgram().emit(sourceFile, writeFile, cancellationToken, emitOnlyDts, customTransformers),
733733
getAllDependencies: notImplemented,
734-
getCurrentDirectory: () => Debug.assertDefined(state.program).getCurrentDirectory()
734+
getCurrentDirectory: () => getProgram().getCurrentDirectory()
735735
};
736+
737+
function getProgram() {
738+
return Debug.assertDefined(state.program);
739+
}
736740
}
737741
}
738742

src/compiler/core.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,10 +1370,6 @@ namespace ts {
13701370
return result;
13711371
}
13721372

1373-
export function createRedirectObject<T extends object>(redirectTarget: T): T {
1374-
return Object.create(redirectTarget);
1375-
}
1376-
13771373
export function extend<T1, T2>(first: T1, second: T2): T1 & T2 {
13781374
const result: T1 & T2 = <any>{};
13791375
for (const id in second) {
@@ -1399,6 +1395,10 @@ namespace ts {
13991395
}
14001396
}
14011397

1398+
export function maybeBind<T, A extends any[], R>(obj: T, fn: ((this: T, ...args: A) => R) | undefined): ((...args: A) => R) | undefined {
1399+
return fn ? fn.bind(obj) : undefined;
1400+
}
1401+
14021402
export interface MultiMap<T> extends Map<T[]> {
14031403
/**
14041404
* Adds the value to an array of values associated with the key, and returns the array.

src/compiler/program.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2187,7 +2187,7 @@ namespace ts {
21872187
}
21882188

21892189
function createRedirectSourceFile(redirectTarget: SourceFile, unredirected: SourceFile, fileName: string, path: Path, resolvedPath: Path, originalFileName: string): SourceFile {
2190-
const redirect = createRedirectObject(redirectTarget);
2190+
const redirect = Object.create(redirectTarget);
21912191
redirect.fileName = fileName;
21922192
redirect.path = path;
21932193
redirect.resolvedPath = resolvedPath;

src/compiler/tsbuild.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ namespace ts {
447447
let nextProjectToBuild = 0;
448448
let timerToBuildInvalidatedProject: any;
449449
let reportFileChangeDetected = false;
450-
const { watchFile, watchFilePath, watchDirectory } = createWatchFactory<ResolvedConfigFileName>(host, options);
450+
const { watchFile, watchFilePath, watchDirectory, writeLog } = createWatchFactory<ResolvedConfigFileName>(host, options);
451451

452452
// Watches for the solution
453453
const allWatchedWildcardDirectories = createFileMap<Map<WildcardDirectoryWatcher>>(toPath);
@@ -593,12 +593,12 @@ namespace ts {
593593
fileOrDirectory => {
594594
const fileOrDirectoryPath = toPath(fileOrDirectory);
595595
if (fileOrDirectoryPath !== toPath(dir) && hasExtension(fileOrDirectoryPath) && !isSupportedSourceFileName(fileOrDirectory, parsed.options)) {
596-
// writeLog(`Project: ${configFileName} Detected file add/remove of non supported extension: ${fileOrDirectory}`);
596+
writeLog(`Project: ${resolved} Detected file add/remove of non supported extension: ${fileOrDirectory}`);
597597
return;
598598
}
599599

600600
if (isOutputFile(fileOrDirectory, parsed)) {
601-
// writeLog(`${fileOrDirectory} is output file`);
601+
writeLog(`${fileOrDirectory} is output file`);
602602
return;
603603
}
604604

@@ -991,7 +991,7 @@ namespace ts {
991991
}
992992

993993
if (status.type === UpToDateStatusType.UpToDateWithUpstreamTypes) {
994-
// Fake build
994+
// Fake that files have been built by updating output file stamps
995995
updateOutputTimestamps(proj);
996996
return;
997997
}

src/compiler/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5000,7 +5000,6 @@ namespace ts {
50005000
getDefaultLibLocation?(): string;
50015001
writeFile: WriteFileCallback;
50025002
getCurrentDirectory(): string;
5003-
getDirectories(path: string): string[];
50045003
getCanonicalFileName(fileName: string): string;
50055004
useCaseSensitiveFileNames(): boolean;
50065005
getNewLine(): string;

src/compiler/watch.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,10 @@ namespace ts {
187187
const onWatchStatusChange = reportWatchStatus || createWatchStatusReporter(system);
188188
return {
189189
onWatchStatusChange,
190-
watchFile: system.watchFile ? ((path, callback, pollingInterval) => system.watchFile!(path, callback, pollingInterval)) : () => noopFileWatcher,
191-
watchDirectory: system.watchDirectory ? ((path, callback, recursive) => system.watchDirectory!(path, callback, recursive)) : () => noopFileWatcher,
192-
setTimeout: system.setTimeout ? ((callback, ms, ...args: any[]) => system.setTimeout!.call(system, callback, ms, ...args)) : noop,
193-
clearTimeout: system.clearTimeout ? (timeoutId => system.clearTimeout!(timeoutId)) : noop
190+
watchFile: maybeBind(system, system.watchFile) || (() => noopFileWatcher),
191+
watchDirectory: maybeBind(system, system.watchDirectory) || (() => noopFileWatcher),
192+
setTimeout: maybeBind(system, system.setTimeout) || noop,
193+
clearTimeout: maybeBind(system, system.clearTimeout) || noop
194194
};
195195
}
196196

@@ -217,6 +217,7 @@ namespace ts {
217217

218218
export function createCompilerHostFromProgramHost(host: ProgramHost<any>, getCompilerOptions: () => CompilerOptions, directoryStructureHost: DirectoryStructureHost = host): CompilerHost {
219219
const useCaseSensitiveFileNames = host.useCaseSensitiveFileNames();
220+
const hostGetNewLine = memoize(() => host.getNewLine());
220221
return {
221222
getSourceFile: (fileName, languageVersion, onError) => {
222223
let text: string | undefined;
@@ -235,22 +236,22 @@ namespace ts {
235236

236237
return text !== undefined ? createSourceFile(fileName, text, languageVersion) : undefined;
237238
},
238-
getDefaultLibLocation: host.getDefaultLibLocation && (() => host.getDefaultLibLocation!()),
239+
getDefaultLibLocation: maybeBind(host, host.getDefaultLibLocation),
239240
getDefaultLibFileName: options => host.getDefaultLibFileName(options),
240241
writeFile,
241242
getCurrentDirectory: memoize(() => host.getCurrentDirectory()),
242243
useCaseSensitiveFileNames: () => useCaseSensitiveFileNames,
243244
getCanonicalFileName: createGetCanonicalFileName(useCaseSensitiveFileNames),
244-
getNewLine: memoize(() => getNewLineCharacter(getCompilerOptions(), () => host.getNewLine())),
245+
getNewLine: () => getNewLineCharacter(getCompilerOptions(), hostGetNewLine),
245246
fileExists: f => host.fileExists(f),
246247
readFile: f => host.readFile(f),
247-
trace: host.trace && (s => host.trace!(s)),
248-
directoryExists: directoryStructureHost.directoryExists && (path => directoryStructureHost.directoryExists!(path)),
249-
getDirectories: (directoryStructureHost.getDirectories && ((path: string) => directoryStructureHost.getDirectories!(path)))!, // TODO: GH#18217
250-
realpath: host.realpath && (s => host.realpath!(s)),
251-
getEnvironmentVariable: host.getEnvironmentVariable ? (name => host.getEnvironmentVariable!(name)) : (() => ""),
252-
createHash: host.createHash && (data => host.createHash!(data)),
253-
readDirectory: (path, extensions, exclude, include, depth?) => directoryStructureHost.readDirectory!(path, extensions, exclude, include, depth),
248+
trace: maybeBind(host, host.trace),
249+
directoryExists: maybeBind(directoryStructureHost, directoryStructureHost.directoryExists),
250+
getDirectories: maybeBind(directoryStructureHost, directoryStructureHost.getDirectories),
251+
realpath: maybeBind(host, host.realpath),
252+
getEnvironmentVariable: maybeBind(host, host.getEnvironmentVariable) || (() => ""),
253+
createHash: maybeBind(host, host.createHash),
254+
readDirectory: maybeBind(host, host.readDirectory),
254255
};
255256

256257
function ensureDirectoriesExist(directoryPath: string) {
@@ -297,13 +298,13 @@ namespace ts {
297298
directoryExists: path => system.directoryExists(path),
298299
getDirectories: path => system.getDirectories(path),
299300
readDirectory: (path, extensions, exclude, include, depth) => system.readDirectory(path, extensions, exclude, include, depth),
300-
realpath: system.realpath && (path => system.realpath!(path)),
301-
getEnvironmentVariable: system.getEnvironmentVariable && (name => system.getEnvironmentVariable(name)),
301+
realpath: maybeBind(system, system.realpath),
302+
getEnvironmentVariable: maybeBind(system, system.getEnvironmentVariable),
302303
trace: s => system.write(s + system.newLine),
303304
createDirectory: path => system.createDirectory(path),
304305
writeFile: (path, data, writeByteOrderMark) => system.writeFile(path, data, writeByteOrderMark),
305306
onCachedDirectoryStructureHostCreate: cacheHost => host = cacheHost || system,
306-
createHash: system.createHash && (s => system.createHash!(s)),
307+
createHash: maybeBind(system, system.createHash),
307308
createProgram
308309
};
309310
}
@@ -758,7 +759,7 @@ namespace ts {
758759

759760
// Create new source file if requested or the versions dont match
760761
if (!hostSourceFile || shouldCreateNewSourceFile || !isFilePresentOnHost(hostSourceFile) || hostSourceFile.version.toString() !== hostSourceFile.sourceFile.version) {
761-
const sourceFile = getNewSourceFile.call(compilerHost, fileName, languageVersion, onError);
762+
const sourceFile = getNewSourceFile(fileName, languageVersion, onError);
762763
if (hostSourceFile) {
763764
if (shouldCreateNewSourceFile) {
764765
hostSourceFile.version++;

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2689,7 +2689,6 @@ declare namespace ts {
26892689
getDefaultLibLocation?(): string;
26902690
writeFile: WriteFileCallback;
26912691
getCurrentDirectory(): string;
2692-
getDirectories(path: string): string[];
26932692
getCanonicalFileName(fileName: string): string;
26942693
useCaseSensitiveFileNames(): boolean;
26952694
getNewLine(): string;

tests/baselines/reference/api/typescript.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2689,7 +2689,6 @@ declare namespace ts {
26892689
getDefaultLibLocation?(): string;
26902690
writeFile: WriteFileCallback;
26912691
getCurrentDirectory(): string;
2692-
getDirectories(path: string): string[];
26932692
getCanonicalFileName(fileName: string): string;
26942693
useCaseSensitiveFileNames(): boolean;
26952694
getNewLine(): string;

0 commit comments

Comments
 (0)