Skip to content

Commit d763060

Browse files
author
Andy Hanson
committed
readFile may return undefined
1 parent e842182 commit d763060

16 files changed

+44
-45
lines changed

src/compiler/commandLineParser.ts

+13-14
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ namespace ts {
752752
}
753753
}
754754

755-
export function parseCommandLine(commandLine: string[], readFile?: (path: string) => string): ParsedCommandLine {
755+
export function parseCommandLine(commandLine: string[], readFile?: (path: string) => string | undefined): ParsedCommandLine {
756756
const options: CompilerOptions = {};
757757
const fileNames: string[] = [];
758758
const errors: Diagnostic[] = [];
@@ -878,15 +878,9 @@ namespace ts {
878878
* Read tsconfig.json file
879879
* @param fileName The path to the config file
880880
*/
881-
export function readConfigFile(fileName: string, readFile: (path: string) => string): { config?: any; error?: Diagnostic } {
882-
let text = "";
883-
try {
884-
text = readFile(fileName);
885-
}
886-
catch (e) {
887-
return { config: {}, error: createCompilerDiagnostic(Diagnostics.Cannot_read_file_0_Colon_1, fileName, e.message) };
888-
}
889-
return parseConfigFileTextToJson(fileName, text);
881+
export function readConfigFile(fileName: string, readFile: (path: string) => string | undefined): { config?: any; error?: Diagnostic } {
882+
const textOrDiagnostic = tryReadFile(fileName, readFile);
883+
return typeof textOrDiagnostic === "string" ? parseConfigFileTextToJson(fileName, textOrDiagnostic) : { config: {}, error: textOrDiagnostic };
890884
}
891885

892886
/**
@@ -906,15 +900,20 @@ namespace ts {
906900
* Read tsconfig.json file
907901
* @param fileName The path to the config file
908902
*/
909-
export function readJsonConfigFile(fileName: string, readFile: (path: string) => string): JsonSourceFile {
910-
let text = "";
903+
export function readJsonConfigFile(fileName: string, readFile: (path: string) => string | undefined): JsonSourceFile {
904+
const textOrDiagnostic = tryReadFile(fileName, readFile);
905+
return typeof textOrDiagnostic === "string" ? parseJsonText(fileName, textOrDiagnostic) : <JsonSourceFile>{ parseDiagnostics: [textOrDiagnostic] };
906+
}
907+
908+
function tryReadFile(fileName: string, readFile: (path: string) | string | undefined): string | Diagnostic {
909+
let text: string | undefined;
911910
try {
912911
text = readFile(fileName);
913912
}
914913
catch (e) {
915-
return <JsonSourceFile>{ parseDiagnostics: [createCompilerDiagnostic(Diagnostics.Cannot_read_file_0_Colon_1, fileName, e.message)] };
914+
return createCompilerDiagnostic(Diagnostics.Cannot_read_file_0_Colon_1, fileName, e.message);
916915
}
917-
return parseJsonText(fileName, text);
916+
return text === undefined ? createCompilerDiagnostic(Diagnostics.Cannot_read_file_0_Colon_1, fileName, "File does not exist.") : text;
918917
}
919918

920919
function commandLineOptionsToMap(options: CommandLineOption[]) {

src/compiler/sys.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace ts {
2323
newLine: string;
2424
useCaseSensitiveFileNames: boolean;
2525
write(s: string): void;
26-
readFile(path: string, encoding?: string): string;
26+
readFile(path: string, encoding?: string): string | undefined;
2727
getFileSize?(path: string): number;
2828
writeFile(path: string, data: string, writeByteOrderMark?: boolean): void;
2929
/**
@@ -97,7 +97,7 @@ namespace ts {
9797
directoryExists(path: string): boolean;
9898
createDirectory(path: string): void;
9999
resolvePath(path: string): string;
100-
readFile(path: string): string;
100+
readFile(path: string): string | undefined;
101101
writeFile(path: string, contents: string): void;
102102
getDirectories(path: string): string[];
103103
readDirectory(path: string, extensions?: ReadonlyArray<string>, basePaths?: ReadonlyArray<string>, excludeEx?: string, includeFileEx?: string, includeDirEx?: string): string[];
@@ -204,7 +204,7 @@ namespace ts {
204204
const platform: string = _os.platform();
205205
const useCaseSensitiveFileNames = isFileSystemCaseSensitive();
206206

207-
function readFile(fileName: string, _encoding?: string): string {
207+
function readFile(fileName: string, _encoding?: string): string | undefined {
208208
if (!fileExists(fileName)) {
209209
return undefined;
210210
}

src/compiler/types.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2414,7 +2414,7 @@ namespace ts {
24142414
*/
24152415
fileExists(path: string): boolean;
24162416

2417-
readFile(path: string): string;
2417+
readFile(path: string): string | undefined;
24182418
}
24192419

24202420
export interface WriteFileCallback {
@@ -3921,7 +3921,7 @@ namespace ts {
39213921
fileExists(fileName: string): boolean;
39223922
// readFile function is used to read arbitrary text files on disk, i.e. when resolution procedure needs the content of 'package.json'
39233923
// to determine location of bundled typings for node module
3924-
readFile(fileName: string): string;
3924+
readFile(fileName: string): string | undefined;
39253925
trace?(s: string): void;
39263926
directoryExists?(directoryName: string): boolean;
39273927
realpath?(path: string): string;

src/compiler/utilities.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3921,7 +3921,7 @@ namespace ts {
39213921
*/
39223922
export function validateLocaleAndSetLanguage(
39233923
locale: string,
3924-
sys: { getExecutingFilePath(): string, resolvePath(path: string): string, fileExists(fileName: string): boolean, readFile(fileName: string): string },
3924+
sys: { getExecutingFilePath(): string, resolvePath(path: string): string, fileExists(fileName: string): boolean, readFile(fileName: string): string | undefined },
39253925
errors?: Diagnostic[]) {
39263926
const matchResult = /^([a-z]+)([_\-]([a-z]+))?$/.exec(locale.toLowerCase());
39273927

src/harness/harness.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ namespace Harness {
479479
getCurrentDirectory(): string;
480480
useCaseSensitiveFileNames(): boolean;
481481
resolvePath(path: string): string;
482-
readFile(path: string): string;
482+
readFile(path: string): string | undefined;
483483
writeFile(path: string, contents: string): void;
484484
directoryName(path: string): string;
485485
getDirectories(path: string): string[];
@@ -719,7 +719,7 @@ namespace Harness {
719719
}
720720
});
721721

722-
export function readFile(file: string) {
722+
export function readFile(file: string): string | undefined {
723723
const response = Http.getFileFromServerSync(serverRoot + file);
724724
if (response.status === 200) {
725725
return response.responseText;
@@ -976,7 +976,7 @@ namespace Harness {
976976
useCaseSensitiveFileNames: () => useCaseSensitiveFileNames,
977977
getNewLine: () => newLine,
978978
fileExists: fileName => fileMap.has(toPath(fileName)),
979-
readFile: (fileName: string): string => {
979+
readFile(fileName: string): string | undefined {
980980
const file = fileMap.get(toPath(fileName));
981981
if (ts.endsWith(fileName, "json")) {
982982
// strip comments

src/harness/harnessLanguageService.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ namespace Harness.LanguageService {
215215
depth,
216216
(p) => this.virtualFileSystem.getAccessibleFileSystemEntries(p));
217217
}
218-
readFile(path: string): string {
218+
readFile(path: string): string | undefined {
219219
const snapshot = this.getScriptSnapshot(path);
220220
return snapshot.getText(0, snapshot.getLength());
221221
}
@@ -619,7 +619,7 @@ namespace Harness.LanguageService {
619619
this.writeMessage(message);
620620
}
621621

622-
readFile(fileName: string): string {
622+
readFile(fileName: string): string | undefined {
623623
if (fileName.indexOf(Harness.Compiler.defaultLibFileName) >= 0) {
624624
fileName = Harness.Compiler.defaultLibFileName;
625625
}

src/harness/projectsRunner.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ class ProjectRunner extends RunnerBase {
289289
return Harness.IO.fileExists(getFileNameInTheProjectTest(fileName));
290290
}
291291

292-
function readFile(fileName: string): string {
292+
function readFile(fileName: string): string | undefined {
293293
return Harness.IO.readFile(getFileNameInTheProjectTest(fileName));
294294
}
295295

src/harness/unittests/moduleResolution.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ namespace ts {
5656
else {
5757
return { readFile, fileExists: path => map.has(path) };
5858
}
59-
function readFile(path: string): string {
59+
function readFile(path: string): string | undefined {
6060
const file = map.get(path);
6161
return file && file.content;
6262
}

src/harness/unittests/session.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace ts.server {
99
newLine: "\n",
1010
useCaseSensitiveFileNames: true,
1111
write(s): void { lastWrittenToHost = s; },
12-
readFile(): string { return void 0; },
12+
readFile: () => undefined,
1313
writeFile: noop,
1414
resolvePath(): string { return void 0; },
1515
fileExists: () => false,

src/harness/virtualFileSystem.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ namespace Utils {
209209
}
210210
}
211211

212-
readFile(path: string): string {
212+
readFile(path: string): string | undefined {
213213
const value = this.traversePath(path);
214214
if (value && value.isFile()) {
215215
return value.content.content;

src/server/lsHost.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ namespace ts.server {
217217
return !this.project.isWatchedMissingFile(path) && this.host.fileExists(file);
218218
}
219219

220-
readFile(fileName: string): string {
220+
readFile(fileName: string): string | undefined {
221221
return this.host.readFile(fileName);
222222
}
223223

src/server/typingsInstaller/typingsInstaller.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ namespace ts.server.typingsInstaller {
9393
abstract readonly typesRegistry: Map<void>;
9494

9595
constructor(
96-
readonly installTypingHost: InstallTypingHost,
97-
readonly globalCachePath: string,
98-
readonly safeListPath: Path,
99-
readonly throttleLimit: number,
96+
private readonly installTypingHost: InstallTypingHost,
97+
private readonly globalCachePath: string,
98+
private readonly safeListPath: Path,
99+
private readonly throttleLimit: number,
100100
protected readonly log = nullLog) {
101101
if (this.log.isEnabled()) {
102102
this.log.writeLine(`Global cache location '${globalCachePath}', safe file path '${safeListPath}'`);

src/services/jsTyping.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
namespace ts.JsTyping {
1010

1111
export interface TypingResolutionHost {
12-
directoryExists: (path: string) => boolean;
13-
fileExists: (fileName: string) => boolean;
14-
readFile: (path: string, encoding?: string) => string;
15-
readDirectory: (rootDir: string, extensions: ReadonlyArray<string>, excludes: ReadonlyArray<string>, includes: ReadonlyArray<string>, depth?: number) => string[];
12+
directoryExists(path: string): boolean;
13+
fileExists(fileName: string): boolean;
14+
readFile(path: string, encoding?: string): string | undefined;
15+
readDirectory(rootDir: string, extensions: ReadonlyArray<string>, excludes: ReadonlyArray<string>, includes: ReadonlyArray<string>, depth?: number): string[];
1616
}
1717

1818
interface PackageJson {

src/services/services.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,7 @@ namespace ts {
11491149
!!hostCache.getEntryByPath(path) :
11501150
(host.fileExists && host.fileExists(fileName));
11511151
},
1152-
readFile: (fileName): string => {
1152+
readFile(fileName) {
11531153
// stub missing host functionality
11541154
const path = toPath(fileName, currentDirectory, getCanonicalFileName);
11551155
if (hostCache.containsEntryByPath(path)) {

src/services/shims.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ namespace ts {
6969

7070
getTypeRootsVersion?(): number;
7171
readDirectory(rootDir: string, extension: string, basePaths?: string, excludeEx?: string, includeFileEx?: string, includeDirEx?: string, depth?: number): string;
72-
readFile(path: string, encoding?: string): string;
72+
readFile(path: string, encoding?: string): string | undefined;
7373
fileExists(path: string): boolean;
7474

7575
getModuleResolutionsForFile?(fileName: string): string;
@@ -95,7 +95,7 @@ namespace ts {
9595
/**
9696
* Read arbitary text files on disk, i.e. when resolution procedure needs the content of 'package.json' to determine location of bundled typings for node modules
9797
*/
98-
readFile(fileName: string): string;
98+
readFile(fileName: string): string | undefined;
9999
realpath?(path: string): string;
100100
trace(s: string): void;
101101
useCaseSensitiveFileNames?(): boolean;
@@ -458,7 +458,7 @@ namespace ts {
458458
));
459459
}
460460

461-
public readFile(path: string, encoding?: string): string {
461+
public readFile(path: string, encoding?: string): string | undefined {
462462
return this.shimHost.readFile(path, encoding);
463463
}
464464

@@ -501,7 +501,7 @@ namespace ts {
501501
return this.shimHost.fileExists(fileName);
502502
}
503503

504-
public readFile(fileName: string): string {
504+
public readFile(fileName: string): string | undefined {
505505
return this.shimHost.readFile(fileName);
506506
}
507507

@@ -1006,7 +1006,7 @@ namespace ts {
10061006
class CoreServicesShimObject extends ShimBase implements CoreServicesShim {
10071007
private logPerformance = false;
10081008

1009-
constructor(factory: ShimFactory, public logger: Logger, private host: CoreServicesShimHostAdapter) {
1009+
constructor(factory: ShimFactory, public readonly logger: Logger, private readonly host: CoreServicesShimHostAdapter) {
10101010
super(factory);
10111011
}
10121012

src/services/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ namespace ts {
165165
* Without these methods, only completions for ambient modules will be provided.
166166
*/
167167
readDirectory?(path: string, extensions?: ReadonlyArray<string>, exclude?: ReadonlyArray<string>, include?: ReadonlyArray<string>, depth?: number): string[];
168-
readFile?(path: string, encoding?: string): string;
168+
readFile?(path: string, encoding?: string): string | undefined;
169169
fileExists?(path: string): boolean;
170170

171171
/*

0 commit comments

Comments
 (0)