diff --git a/src/compiler/builder.ts b/src/compiler/builder.ts index 89577cfd68a..e8a2bae0513 100644 --- a/src/compiler/builder.ts +++ b/src/compiler/builder.ts @@ -717,22 +717,26 @@ namespace ts { getState: notImplemented, backupCurrentState: noop, useBackupState: noop, - getProgram: () => Debug.assertDefined(state.program), + getProgram, getProgramOrUndefined: () => state.program, releaseProgram: () => state.program = undefined, getCompilerOptions: () => state.compilerOptions, - getSourceFile: fileName => Debug.assertDefined(state.program).getSourceFile(fileName), - getSourceFiles: () => Debug.assertDefined(state.program).getSourceFiles(), - getOptionsDiagnostics: cancellationToken => Debug.assertDefined(state.program).getOptionsDiagnostics(cancellationToken), - getGlobalDiagnostics: cancellationToken => Debug.assertDefined(state.program).getGlobalDiagnostics(cancellationToken), + getSourceFile: fileName => getProgram().getSourceFile(fileName), + getSourceFiles: () => getProgram().getSourceFiles(), + getOptionsDiagnostics: cancellationToken => getProgram().getOptionsDiagnostics(cancellationToken), + getGlobalDiagnostics: cancellationToken => getProgram().getGlobalDiagnostics(cancellationToken), getConfigFileParsingDiagnostics: () => configFileParsingDiagnostics, - getSyntacticDiagnostics: (sourceFile, cancellationToken) => Debug.assertDefined(state.program).getSyntacticDiagnostics(sourceFile, cancellationToken), - getDeclarationDiagnostics: (sourceFile, cancellationToken) => Debug.assertDefined(state.program).getDeclarationDiagnostics(sourceFile, cancellationToken), - getSemanticDiagnostics: (sourceFile, cancellationToken) => Debug.assertDefined(state.program).getSemanticDiagnostics(sourceFile, cancellationToken), - emit: (sourceFile, writeFile, cancellationToken, emitOnlyDts, customTransformers) => Debug.assertDefined(state.program).emit(sourceFile, writeFile, cancellationToken, emitOnlyDts, customTransformers), + getSyntacticDiagnostics: (sourceFile, cancellationToken) => getProgram().getSyntacticDiagnostics(sourceFile, cancellationToken), + getDeclarationDiagnostics: (sourceFile, cancellationToken) => getProgram().getDeclarationDiagnostics(sourceFile, cancellationToken), + getSemanticDiagnostics: (sourceFile, cancellationToken) => getProgram().getSemanticDiagnostics(sourceFile, cancellationToken), + emit: (sourceFile, writeFile, cancellationToken, emitOnlyDts, customTransformers) => getProgram().emit(sourceFile, writeFile, cancellationToken, emitOnlyDts, customTransformers), getAllDependencies: notImplemented, - getCurrentDirectory: () => Debug.assertDefined(state.program).getCurrentDirectory() + getCurrentDirectory: () => getProgram().getCurrentDirectory() }; + + function getProgram() { + return Debug.assertDefined(state.program); + } } } diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 74ac9e93047..d4f5fe9664a 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -1370,10 +1370,6 @@ namespace ts { return result; } - export function createRedirectObject(redirectTarget: T): T { - return Object.create(redirectTarget); - } - export function extend(first: T1, second: T2): T1 & T2 { const result: T1 & T2 = {}; for (const id in second) { @@ -1399,6 +1395,10 @@ namespace ts { } } + export function maybeBind(obj: T, fn: ((this: T, ...args: A) => R) | undefined): ((...args: A) => R) | undefined { + return fn ? fn.bind(obj) : undefined; + } + export interface MultiMap extends Map { /** * Adds the value to an array of values associated with the key, and returns the array. diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 8b66d82fa81..0eff6998d8d 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -2187,7 +2187,7 @@ namespace ts { } function createRedirectSourceFile(redirectTarget: SourceFile, unredirected: SourceFile, fileName: string, path: Path, resolvedPath: Path, originalFileName: string): SourceFile { - const redirect = createRedirectObject(redirectTarget); + const redirect = Object.create(redirectTarget); redirect.fileName = fileName; redirect.path = path; redirect.resolvedPath = resolvedPath; diff --git a/src/compiler/tsbuild.ts b/src/compiler/tsbuild.ts index 4395b302944..61b3c610f5a 100644 --- a/src/compiler/tsbuild.ts +++ b/src/compiler/tsbuild.ts @@ -447,7 +447,7 @@ namespace ts { let nextProjectToBuild = 0; let timerToBuildInvalidatedProject: any; let reportFileChangeDetected = false; - const { watchFile, watchFilePath, watchDirectory } = createWatchFactory(host, options); + const { watchFile, watchFilePath, watchDirectory, writeLog } = createWatchFactory(host, options); // Watches for the solution const allWatchedWildcardDirectories = createFileMap>(toPath); @@ -593,12 +593,12 @@ namespace ts { fileOrDirectory => { const fileOrDirectoryPath = toPath(fileOrDirectory); if (fileOrDirectoryPath !== toPath(dir) && hasExtension(fileOrDirectoryPath) && !isSupportedSourceFileName(fileOrDirectory, parsed.options)) { - // writeLog(`Project: ${configFileName} Detected file add/remove of non supported extension: ${fileOrDirectory}`); + writeLog(`Project: ${resolved} Detected file add/remove of non supported extension: ${fileOrDirectory}`); return; } if (isOutputFile(fileOrDirectory, parsed)) { - // writeLog(`${fileOrDirectory} is output file`); + writeLog(`${fileOrDirectory} is output file`); return; } @@ -991,7 +991,7 @@ namespace ts { } if (status.type === UpToDateStatusType.UpToDateWithUpstreamTypes) { - // Fake build + // Fake that files have been built by updating output file stamps updateOutputTimestamps(proj); return; } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 852437942f1..ceec8d60cf2 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -5000,7 +5000,6 @@ namespace ts { getDefaultLibLocation?(): string; writeFile: WriteFileCallback; getCurrentDirectory(): string; - getDirectories(path: string): string[]; getCanonicalFileName(fileName: string): string; useCaseSensitiveFileNames(): boolean; getNewLine(): string; diff --git a/src/compiler/watch.ts b/src/compiler/watch.ts index 6dbed259547..827a3279b3b 100644 --- a/src/compiler/watch.ts +++ b/src/compiler/watch.ts @@ -187,10 +187,10 @@ namespace ts { const onWatchStatusChange = reportWatchStatus || createWatchStatusReporter(system); return { onWatchStatusChange, - watchFile: system.watchFile ? ((path, callback, pollingInterval) => system.watchFile!(path, callback, pollingInterval)) : () => noopFileWatcher, - watchDirectory: system.watchDirectory ? ((path, callback, recursive) => system.watchDirectory!(path, callback, recursive)) : () => noopFileWatcher, - setTimeout: system.setTimeout ? ((callback, ms, ...args: any[]) => system.setTimeout!.call(system, callback, ms, ...args)) : noop, - clearTimeout: system.clearTimeout ? (timeoutId => system.clearTimeout!(timeoutId)) : noop + watchFile: maybeBind(system, system.watchFile) || (() => noopFileWatcher), + watchDirectory: maybeBind(system, system.watchDirectory) || (() => noopFileWatcher), + setTimeout: maybeBind(system, system.setTimeout) || noop, + clearTimeout: maybeBind(system, system.clearTimeout) || noop }; } @@ -217,6 +217,7 @@ namespace ts { export function createCompilerHostFromProgramHost(host: ProgramHost, getCompilerOptions: () => CompilerOptions, directoryStructureHost: DirectoryStructureHost = host): CompilerHost { const useCaseSensitiveFileNames = host.useCaseSensitiveFileNames(); + const hostGetNewLine = memoize(() => host.getNewLine()); return { getSourceFile: (fileName, languageVersion, onError) => { let text: string | undefined; @@ -235,22 +236,22 @@ namespace ts { return text !== undefined ? createSourceFile(fileName, text, languageVersion) : undefined; }, - getDefaultLibLocation: host.getDefaultLibLocation && (() => host.getDefaultLibLocation!()), + getDefaultLibLocation: maybeBind(host, host.getDefaultLibLocation), getDefaultLibFileName: options => host.getDefaultLibFileName(options), writeFile, getCurrentDirectory: memoize(() => host.getCurrentDirectory()), useCaseSensitiveFileNames: () => useCaseSensitiveFileNames, getCanonicalFileName: createGetCanonicalFileName(useCaseSensitiveFileNames), - getNewLine: memoize(() => getNewLineCharacter(getCompilerOptions(), () => host.getNewLine())), + getNewLine: () => getNewLineCharacter(getCompilerOptions(), hostGetNewLine), fileExists: f => host.fileExists(f), readFile: f => host.readFile(f), - trace: host.trace && (s => host.trace!(s)), - directoryExists: directoryStructureHost.directoryExists && (path => directoryStructureHost.directoryExists!(path)), - getDirectories: (directoryStructureHost.getDirectories && ((path: string) => directoryStructureHost.getDirectories!(path)))!, // TODO: GH#18217 - realpath: host.realpath && (s => host.realpath!(s)), - getEnvironmentVariable: host.getEnvironmentVariable ? (name => host.getEnvironmentVariable!(name)) : (() => ""), - createHash: host.createHash && (data => host.createHash!(data)), - readDirectory: (path, extensions, exclude, include, depth?) => directoryStructureHost.readDirectory!(path, extensions, exclude, include, depth), + trace: maybeBind(host, host.trace), + directoryExists: maybeBind(directoryStructureHost, directoryStructureHost.directoryExists), + getDirectories: maybeBind(directoryStructureHost, directoryStructureHost.getDirectories), + realpath: maybeBind(host, host.realpath), + getEnvironmentVariable: maybeBind(host, host.getEnvironmentVariable) || (() => ""), + createHash: maybeBind(host, host.createHash), + readDirectory: maybeBind(host, host.readDirectory), }; function ensureDirectoriesExist(directoryPath: string) { @@ -297,13 +298,13 @@ namespace ts { directoryExists: path => system.directoryExists(path), getDirectories: path => system.getDirectories(path), readDirectory: (path, extensions, exclude, include, depth) => system.readDirectory(path, extensions, exclude, include, depth), - realpath: system.realpath && (path => system.realpath!(path)), - getEnvironmentVariable: system.getEnvironmentVariable && (name => system.getEnvironmentVariable(name)), + realpath: maybeBind(system, system.realpath), + getEnvironmentVariable: maybeBind(system, system.getEnvironmentVariable), trace: s => system.write(s + system.newLine), createDirectory: path => system.createDirectory(path), writeFile: (path, data, writeByteOrderMark) => system.writeFile(path, data, writeByteOrderMark), onCachedDirectoryStructureHostCreate: cacheHost => host = cacheHost || system, - createHash: system.createHash && (s => system.createHash!(s)), + createHash: maybeBind(system, system.createHash), createProgram }; } @@ -758,7 +759,7 @@ namespace ts { // Create new source file if requested or the versions dont match if (!hostSourceFile || shouldCreateNewSourceFile || !isFilePresentOnHost(hostSourceFile) || hostSourceFile.version.toString() !== hostSourceFile.sourceFile.version) { - const sourceFile = getNewSourceFile.call(compilerHost, fileName, languageVersion, onError); + const sourceFile = getNewSourceFile(fileName, languageVersion, onError); if (hostSourceFile) { if (shouldCreateNewSourceFile) { hostSourceFile.version++; diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index aee6b01eaff..4801d83de31 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -2689,7 +2689,6 @@ declare namespace ts { getDefaultLibLocation?(): string; writeFile: WriteFileCallback; getCurrentDirectory(): string; - getDirectories(path: string): string[]; getCanonicalFileName(fileName: string): string; useCaseSensitiveFileNames(): boolean; getNewLine(): string; diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index ca4177d87cc..2e347d5a53a 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -2689,7 +2689,6 @@ declare namespace ts { getDefaultLibLocation?(): string; writeFile: WriteFileCallback; getCurrentDirectory(): string; - getDirectories(path: string): string[]; getCanonicalFileName(fileName: string): string; useCaseSensitiveFileNames(): boolean; getNewLine(): string;