Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use empty object for invalid package json contents instead of undefined #34906

Merged
merged 2 commits into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 13 additions & 13 deletions src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2218,22 +2218,22 @@ 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);
const content = stringContent && tryParseJson(stringContent) as PackageJsonRaw;
if (!content) {
return undefined;
}
if (!stringContent) return undefined;

const content = tryParseJson(stringContent) as PackageJsonRaw;
const info: Pick<PackageJsonInfo, typeof dependencyKeys[number]> = {};
for (const key of dependencyKeys) {
const dependencies = content[key];
if (!dependencies) {
continue;
}
const dependencyMap = createMap<string>();
for (const packageName in dependencies) {
dependencyMap.set(packageName, dependencies[packageName]);
if (content) {
for (const key of dependencyKeys) {
const dependencies = content[key];
if (!dependencies) {
continue;
}
const dependencyMap = createMap<string>();
for (const packageName in dependencies) {
dependencyMap.set(packageName, dependencies[packageName]);
}
info[key] = dependencyMap;
}
info[key] = dependencyMap;
}

const dependencyGroups = [
Expand Down
12 changes: 12 additions & 0 deletions src/testRunner/unittests/tsserver/packageJsonInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ namespace ts.projectSystem {
assert.lengthOf(project.getPackageJsonsVisibleToFile("/a.ts" as Path), 1);
assert.lengthOf(project.getPackageJsonsVisibleToFile("/src/b.ts" as Path), 2);
});

it("handles errors in json parsing of package.json", () => {
const packageJsonContent = `{ "mod" }`;
const { project } = setup([tsConfig, { path: packageJson.path, content: packageJsonContent }]);
project.getPackageJsonsVisibleToFile("/src/whatever/blah.ts" as Path);
const packageJsonInfo = project.packageJsonCache.getInDirectory("/" as Path)!;
assert.isObject(packageJsonInfo);
assert.isUndefined(packageJsonInfo.dependencies);
assert.isUndefined(packageJsonInfo.devDependencies);
assert.isUndefined(packageJsonInfo.peerDependencies);
assert.isUndefined(packageJsonInfo.optionalDependencies);
});
});

function setup(files: readonly File[] = [tsConfig, packageJson]) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/// <reference path="../fourslash.ts" />

// @Filename: /jsconfig.json
////{
//// "compilerOptions": {
//// "module": "commonjs",
//// },
////}

// @Filename: /node_modules/@types/node/index.d.ts
////declare module 'fs' {
//// export function readFile(): void;
////}
////declare module 'util' {
//// export function promisify(): void;
////}

// @Filename: /package.json
////{ "mod" }

// @Filename: /a.js
////
////readF/**/

verifyExcludes("readFile");
edit.replaceLine(0, "import { promisify } from 'util';");
verifyIncludes("readFile");
edit.deleteLine(0);
verifyExcludes("readFile");

function verifyIncludes(name: string) {
goTo.marker("");
verify.completions({
includes: {
name,
source: "fs",
hasAction: true,
sortText: completion.SortText.AutoImportSuggestions,
},
preferences: {
includeCompletionsForModuleExports: true,
includeInsertTextCompletions: true,
},
});
}

function verifyExcludes(name: string) {
goTo.marker("");
verify.completions({
excludes: name,
preferences: {
includeCompletionsForModuleExports: true,
includeInsertTextCompletions: true,
},
});
}