Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 96d537b

Browse filesBrowse files
author
Andy Hanson
committed
readFile may return undefined
1 parent e842182 commit 96d537b
Copy full SHA for 96d537b

16 files changed

+44-45Lines changed: 44 additions & 45 deletions
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/compiler/commandLineParser.ts‎

Copy file name to clipboardExpand all lines: src/compiler/commandLineParser.ts
+13-14Lines changed: 13 additions & 14 deletions
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[]) {
Collapse file

‎src/compiler/sys.ts‎

Copy file name to clipboardExpand all lines: src/compiler/sys.ts
+3-3Lines changed: 3 additions & 3 deletions
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
}
Collapse file

‎src/compiler/types.ts‎

Copy file name to clipboardExpand all lines: src/compiler/types.ts
+2-2Lines changed: 2 additions & 2 deletions
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;
Collapse file

‎src/compiler/utilities.ts‎

Copy file name to clipboardExpand all lines: src/compiler/utilities.ts
+1-1Lines changed: 1 addition & 1 deletion
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

Collapse file

‎src/harness/harness.ts‎

Copy file name to clipboardExpand all lines: src/harness/harness.ts
+3-3Lines changed: 3 additions & 3 deletions
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
Collapse file

‎src/harness/harnessLanguageService.ts‎

Copy file name to clipboardExpand all lines: src/harness/harnessLanguageService.ts
+2-2Lines changed: 2 additions & 2 deletions
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
}
Collapse file

‎src/harness/projectsRunner.ts‎

Copy file name to clipboardExpand all lines: src/harness/projectsRunner.ts
+1-1Lines changed: 1 addition & 1 deletion
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

Collapse file

‎src/harness/unittests/moduleResolution.ts‎

Copy file name to clipboardExpand all lines: src/harness/unittests/moduleResolution.ts
+1-1Lines changed: 1 addition & 1 deletion
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
}
Collapse file

‎src/harness/unittests/session.ts‎

Copy file name to clipboardExpand all lines: src/harness/unittests/session.ts
+1-1Lines changed: 1 addition & 1 deletion
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,
Collapse file

‎src/harness/virtualFileSystem.ts‎

Copy file name to clipboardExpand all lines: src/harness/virtualFileSystem.ts
+1-1Lines changed: 1 addition & 1 deletion
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;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.