Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ namespace ts {
}
}

export function parseCommandLine(commandLine: string[], readFile?: (path: string) => string): ParsedCommandLine {
export function parseCommandLine(commandLine: string[], readFile?: (path: string) => string | undefined): ParsedCommandLine {
const options: CompilerOptions = {};
const fileNames: string[] = [];
const errors: Diagnostic[] = [];
Expand Down Expand Up @@ -878,15 +878,9 @@ namespace ts {
* Read tsconfig.json file
* @param fileName The path to the config file
*/
export function readConfigFile(fileName: string, readFile: (path: string) => string): { config?: any; error?: Diagnostic } {
let text = "";
try {
text = readFile(fileName);
}
catch (e) {
return { config: {}, error: createCompilerDiagnostic(Diagnostics.Cannot_read_file_0_Colon_1, fileName, e.message) };
}
return parseConfigFileTextToJson(fileName, text);
export function readConfigFile(fileName: string, readFile: (path: string) => string | undefined): { config?: any; error?: Diagnostic } {
const textOrDiagnostic = tryReadFile(fileName, readFile);
return typeof textOrDiagnostic === "string" ? parseConfigFileTextToJson(fileName, textOrDiagnostic) : { config: {}, error: textOrDiagnostic };
}

/**
Expand All @@ -906,15 +900,20 @@ namespace ts {
* Read tsconfig.json file
* @param fileName The path to the config file
*/
export function readJsonConfigFile(fileName: string, readFile: (path: string) => string): JsonSourceFile {
let text = "";
export function readJsonConfigFile(fileName: string, readFile: (path: string) => string | undefined): JsonSourceFile {
const textOrDiagnostic = tryReadFile(fileName, readFile);
return typeof textOrDiagnostic === "string" ? parseJsonText(fileName, textOrDiagnostic) : <JsonSourceFile>{ parseDiagnostics: [textOrDiagnostic] };
}

function tryReadFile(fileName: string, readFile: (path: string) => string | undefined): string | Diagnostic {
let text: string | undefined;
try {
text = readFile(fileName);
}
catch (e) {
return <JsonSourceFile>{ parseDiagnostics: [createCompilerDiagnostic(Diagnostics.Cannot_read_file_0_Colon_1, fileName, e.message)] };
return createCompilerDiagnostic(Diagnostics.Cannot_read_file_0_Colon_1, fileName, e.message);
}
return parseJsonText(fileName, text);
return text === undefined ? createCompilerDiagnostic(Diagnostics.The_specified_path_does_not_exist_Colon_0, fileName) : text;
}

function commandLineOptionsToMap(options: CommandLineOption[]) {
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/sys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace ts {
newLine: string;
useCaseSensitiveFileNames: boolean;
write(s: string): void;
readFile(path: string, encoding?: string): string;
readFile(path: string, encoding?: string): string | undefined;
getFileSize?(path: string): number;
writeFile(path: string, data: string, writeByteOrderMark?: boolean): void;
/**
Expand Down Expand Up @@ -97,7 +97,7 @@ namespace ts {
directoryExists(path: string): boolean;
createDirectory(path: string): void;
resolvePath(path: string): string;
readFile(path: string): string;
readFile(path: string): string | undefined;
writeFile(path: string, contents: string): void;
getDirectories(path: string): string[];
readDirectory(path: string, extensions?: ReadonlyArray<string>, basePaths?: ReadonlyArray<string>, excludeEx?: string, includeFileEx?: string, includeDirEx?: string): string[];
Expand Down Expand Up @@ -204,7 +204,7 @@ namespace ts {
const platform: string = _os.platform();
const useCaseSensitiveFileNames = isFileSystemCaseSensitive();

function readFile(fileName: string, _encoding?: string): string {
function readFile(fileName: string, _encoding?: string): string | undefined {
if (!fileExists(fileName)) {
return undefined;
}
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2414,7 +2414,7 @@ namespace ts {
*/
fileExists(path: string): boolean;

readFile(path: string): string;
readFile(path: string): string | undefined;
}

export interface WriteFileCallback {
Expand Down Expand Up @@ -3921,7 +3921,7 @@ namespace ts {
fileExists(fileName: string): boolean;
// readFile function is used to read arbitrary text files on disk, i.e. when resolution procedure needs the content of 'package.json'
// to determine location of bundled typings for node module
readFile(fileName: string): string;
readFile(fileName: string): string | undefined;
trace?(s: string): void;
directoryExists?(directoryName: string): boolean;
realpath?(path: string): string;
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3921,7 +3921,7 @@ namespace ts {
*/
export function validateLocaleAndSetLanguage(
locale: string,
sys: { getExecutingFilePath(): string, resolvePath(path: string): string, fileExists(fileName: string): boolean, readFile(fileName: string): string },
sys: { getExecutingFilePath(): string, resolvePath(path: string): string, fileExists(fileName: string): boolean, readFile(fileName: string): string | undefined },
errors?: Diagnostic[]) {
const matchResult = /^([a-z]+)([_\-]([a-z]+))?$/.exec(locale.toLowerCase());

Expand Down
6 changes: 3 additions & 3 deletions src/harness/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ namespace Harness {
getCurrentDirectory(): string;
useCaseSensitiveFileNames(): boolean;
resolvePath(path: string): string;
readFile(path: string): string;
readFile(path: string): string | undefined;
writeFile(path: string, contents: string): void;
directoryName(path: string): string;
getDirectories(path: string): string[];
Expand Down Expand Up @@ -719,7 +719,7 @@ namespace Harness {
}
});

export function readFile(file: string) {
export function readFile(file: string): string | undefined {
const response = Http.getFileFromServerSync(serverRoot + file);
if (response.status === 200) {
return response.responseText;
Expand Down Expand Up @@ -976,7 +976,7 @@ namespace Harness {
useCaseSensitiveFileNames: () => useCaseSensitiveFileNames,
getNewLine: () => newLine,
fileExists: fileName => fileMap.has(toPath(fileName)),
readFile: (fileName: string): string => {
readFile(fileName: string): string | undefined {
const file = fileMap.get(toPath(fileName));
if (ts.endsWith(fileName, "json")) {
// strip comments
Expand Down
4 changes: 2 additions & 2 deletions src/harness/harnessLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ namespace Harness.LanguageService {
depth,
(p) => this.virtualFileSystem.getAccessibleFileSystemEntries(p));
}
readFile(path: string): string {
readFile(path: string): string | undefined {
const snapshot = this.getScriptSnapshot(path);
return snapshot.getText(0, snapshot.getLength());
}
Expand Down Expand Up @@ -619,7 +619,7 @@ namespace Harness.LanguageService {
this.writeMessage(message);
}

readFile(fileName: string): string {
readFile(fileName: string): string | undefined {
if (fileName.indexOf(Harness.Compiler.defaultLibFileName) >= 0) {
fileName = Harness.Compiler.defaultLibFileName;
}
Expand Down
2 changes: 1 addition & 1 deletion src/harness/projectsRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ class ProjectRunner extends RunnerBase {
return Harness.IO.fileExists(getFileNameInTheProjectTest(fileName));
}

function readFile(fileName: string): string {
function readFile(fileName: string): string | undefined {
return Harness.IO.readFile(getFileNameInTheProjectTest(fileName));
}

Expand Down
2 changes: 1 addition & 1 deletion src/harness/unittests/moduleResolution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace ts {
else {
return { readFile, fileExists: path => map.has(path) };
}
function readFile(path: string): string {
function readFile(path: string): string | undefined {
const file = map.get(path);
return file && file.content;
}
Expand Down
2 changes: 1 addition & 1 deletion src/harness/unittests/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace ts.server {
newLine: "\n",
useCaseSensitiveFileNames: true,
write(s): void { lastWrittenToHost = s; },
readFile(): string { return void 0; },
readFile: () => undefined,
writeFile: noop,
resolvePath(): string { return void 0; },
fileExists: () => false,
Expand Down
2 changes: 1 addition & 1 deletion src/harness/virtualFileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ namespace Utils {
}
}

readFile(path: string): string {
readFile(path: string): string | undefined {
const value = this.traversePath(path);
if (value && value.isFile()) {
return value.content.content;
Expand Down
2 changes: 1 addition & 1 deletion src/server/lsHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ namespace ts.server {
return !this.project.isWatchedMissingFile(path) && this.host.fileExists(file);
}

readFile(fileName: string): string {
readFile(fileName: string): string | undefined {
return this.host.readFile(fileName);
}

Expand Down
8 changes: 4 additions & 4 deletions src/server/typingsInstaller/typingsInstaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ namespace ts.server.typingsInstaller {
abstract readonly typesRegistry: Map<void>;

constructor(
readonly installTypingHost: InstallTypingHost,
readonly globalCachePath: string,
readonly safeListPath: Path,
readonly throttleLimit: number,
protected readonly installTypingHost: InstallTypingHost,
private readonly globalCachePath: string,
private readonly safeListPath: Path,
private readonly throttleLimit: number,
protected readonly log = nullLog) {
if (this.log.isEnabled()) {
this.log.writeLine(`Global cache location '${globalCachePath}', safe file path '${safeListPath}'`);
Expand Down
8 changes: 4 additions & 4 deletions src/services/jsTyping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
namespace ts.JsTyping {

export interface TypingResolutionHost {
directoryExists: (path: string) => boolean;
fileExists: (fileName: string) => boolean;
readFile: (path: string, encoding?: string) => string;
readDirectory: (rootDir: string, extensions: ReadonlyArray<string>, excludes: ReadonlyArray<string>, includes: ReadonlyArray<string>, depth?: number) => string[];
directoryExists(path: string): boolean;
fileExists(fileName: string): boolean;
readFile(path: string, encoding?: string): string | undefined;
readDirectory(rootDir: string, extensions: ReadonlyArray<string>, excludes: ReadonlyArray<string>, includes: ReadonlyArray<string>, depth?: number): string[];
}

interface PackageJson {
Expand Down
2 changes: 1 addition & 1 deletion src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1149,7 +1149,7 @@ namespace ts {
!!hostCache.getEntryByPath(path) :
(host.fileExists && host.fileExists(fileName));
},
readFile: (fileName): string => {
readFile(fileName) {
// stub missing host functionality
const path = toPath(fileName, currentDirectory, getCanonicalFileName);
if (hostCache.containsEntryByPath(path)) {
Expand Down
10 changes: 5 additions & 5 deletions src/services/shims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ namespace ts {

getTypeRootsVersion?(): number;
readDirectory(rootDir: string, extension: string, basePaths?: string, excludeEx?: string, includeFileEx?: string, includeDirEx?: string, depth?: number): string;
readFile(path: string, encoding?: string): string;
readFile(path: string, encoding?: string): string | undefined;
fileExists(path: string): boolean;

getModuleResolutionsForFile?(fileName: string): string;
Expand All @@ -95,7 +95,7 @@ namespace ts {
/**
* 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
*/
readFile(fileName: string): string;
readFile(fileName: string): string | undefined;
realpath?(path: string): string;
trace(s: string): void;
useCaseSensitiveFileNames?(): boolean;
Expand Down Expand Up @@ -458,7 +458,7 @@ namespace ts {
));
}

public readFile(path: string, encoding?: string): string {
public readFile(path: string, encoding?: string): string | undefined {
return this.shimHost.readFile(path, encoding);
}

Expand Down Expand Up @@ -501,7 +501,7 @@ namespace ts {
return this.shimHost.fileExists(fileName);
}

public readFile(fileName: string): string {
public readFile(fileName: string): string | undefined {
return this.shimHost.readFile(fileName);
}

Expand Down Expand Up @@ -1006,7 +1006,7 @@ namespace ts {
class CoreServicesShimObject extends ShimBase implements CoreServicesShim {
private logPerformance = false;

constructor(factory: ShimFactory, public logger: Logger, private host: CoreServicesShimHostAdapter) {
constructor(factory: ShimFactory, public readonly logger: Logger, private readonly host: CoreServicesShimHostAdapter) {
super(factory);
}

Expand Down
2 changes: 1 addition & 1 deletion src/services/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ namespace ts {
* Without these methods, only completions for ambient modules will be provided.
*/
readDirectory?(path: string, extensions?: ReadonlyArray<string>, exclude?: ReadonlyArray<string>, include?: ReadonlyArray<string>, depth?: number): string[];
readFile?(path: string, encoding?: string): string;
readFile?(path: string, encoding?: string): string | undefined;
fileExists?(path: string): boolean;

/*
Expand Down