Handle empty package.json files

This commit is contained in:
Andrew Branch 2020-08-06 09:34:46 -07:00
parent 6247364181
commit 0b9b321b0c
No known key found for this signature in database
GPG key ID: 22CCA4B120C427D2
4 changed files with 28 additions and 16 deletions

View file

@ -3774,12 +3774,10 @@ namespace ts.server {
/*@internal*/
getPackageJsonsVisibleToFile(fileName: string, rootDir?: string): readonly PackageJsonInfo[] {
const packageJsonCache = this.packageJsonCache;
const watchPackageJsonFile = this.watchPackageJsonFile.bind(this);
const toPath = this.toPath.bind(this);
const rootPath = rootDir && toPath(rootDir);
const filePath = toPath(fileName);
const rootPath = rootDir && this.toPath(rootDir);
const filePath = this.toPath(fileName);
const result: PackageJsonInfo[] = [];
forEachAncestorDirectory(getDirectoryPath(filePath), function processDirectory(directory): boolean | undefined {
const processDirectory = (directory: Path): boolean | undefined => {
switch (packageJsonCache.directoryHasPackageJson(directory)) {
// Sync and check same directory again
case Ternary.Maybe:
@ -3788,15 +3786,16 @@ namespace ts.server {
// Check package.json
case Ternary.True:
const packageJsonFileName = combinePaths(directory, "package.json");
watchPackageJsonFile(packageJsonFileName);
this.watchPackageJsonFile(packageJsonFileName as Path);
const info = packageJsonCache.getInDirectory(directory);
if (info) result.push(info);
}
if (rootPath && rootPath === toPath(directory)) {
if (rootPath && rootPath === this.toPath(directory)) {
return true;
}
});
};
forEachAncestorDirectory(getDirectoryPath(filePath), processDirectory);
return result;
}

View file

@ -42,11 +42,9 @@ namespace ts.server {
};
function addOrUpdate(fileName: Path) {
const packageJsonInfo = createPackageJsonInfo(fileName, host.host);
if (packageJsonInfo !== undefined) {
packageJsons.set(fileName, packageJsonInfo);
directoriesWithoutPackageJson.delete(getDirectoryPath(fileName));
}
const packageJsonInfo = Debug.checkDefined(createPackageJsonInfo(fileName, host.host));
packageJsons.set(fileName, packageJsonInfo);
directoriesWithoutPackageJson.delete(getDirectoryPath(fileName));
}
function directoryHasPackageJson(directory: Path) {

View file

@ -2735,9 +2735,7 @@ namespace ts {
type PackageJsonRaw = Record<typeof dependencyKeys[number], Record<string, string> | undefined>;
const dependencyKeys = ["dependencies", "devDependencies", "optionalDependencies", "peerDependencies"] as const;
const stringContent = host.readFile(fileName);
if (!stringContent) return undefined;
const stringContent = host.readFile(fileName) || "";
const content = tryParseJson(stringContent) as PackageJsonRaw | undefined;
const info: Pick<PackageJsonInfo, typeof dependencyKeys[number]> = {};
if (content) {

View file

@ -82,6 +82,23 @@ namespace ts.projectSystem {
assert.ok(packageJsonInfo2.peerDependencies);
assert.ok(packageJsonInfo2.optionalDependencies);
});
it("handles empty package.json", () => {
const packageJsonContent = "";
const { projectService, host } = setup([tsConfig, { path: packageJson.path, content: packageJsonContent }]);
projectService.getPackageJsonsVisibleToFile("/src/whatever/blah.ts" as Path);
const packageJsonInfo = projectService.packageJsonCache.getInDirectory("/" as Path)!;
assert.isFalse(packageJsonInfo.parseable);
host.writeFile(packageJson.path, packageJson.content);
projectService.getPackageJsonsVisibleToFile("/src/whatever/blah.ts" as Path);
const packageJsonInfo2 = projectService.packageJsonCache.getInDirectory("/" as Path)!;
assert.ok(packageJsonInfo2);
assert.ok(packageJsonInfo2.dependencies);
assert.ok(packageJsonInfo2.devDependencies);
assert.ok(packageJsonInfo2.peerDependencies);
assert.ok(packageJsonInfo2.optionalDependencies);
});
});
function setup(files: readonly File[] = [tsConfig, packageJson]) {