mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Behave as if package json doesnt exist in case of invalid json in package json
This commit is contained in:
@@ -9,7 +9,7 @@ namespace ts.server {
|
||||
}
|
||||
|
||||
export function createPackageJsonCache(project: Project): PackageJsonCache {
|
||||
const packageJsons = createMap<PackageJsonInfo>();
|
||||
const packageJsons = createMap<PackageJsonInfo | false>();
|
||||
const directoriesWithoutPackageJson = createMap<true>();
|
||||
return {
|
||||
addOrUpdate,
|
||||
@@ -18,7 +18,7 @@ namespace ts.server {
|
||||
directoriesWithoutPackageJson.set(getDirectoryPath(fileName), true);
|
||||
},
|
||||
getInDirectory: directory => {
|
||||
return packageJsons.get(combinePaths(directory, "package.json"));
|
||||
return packageJsons.get(combinePaths(directory, "package.json")) || undefined;
|
||||
},
|
||||
directoryHasPackageJson,
|
||||
searchDirectoryAndAncestors: directory => {
|
||||
@@ -39,7 +39,7 @@ namespace ts.server {
|
||||
|
||||
function addOrUpdate(fileName: Path) {
|
||||
const packageJsonInfo = createPackageJsonInfo(fileName, project);
|
||||
if (packageJsonInfo) {
|
||||
if (packageJsonInfo !== undefined) {
|
||||
packageJsons.set(fileName, packageJsonInfo);
|
||||
directoriesWithoutPackageJson.delete(getDirectoryPath(fileName));
|
||||
}
|
||||
|
||||
@@ -1474,7 +1474,8 @@ namespace ts.server {
|
||||
case Ternary.True:
|
||||
const packageJsonFileName = combinePaths(directory, "package.json");
|
||||
watchPackageJsonFile(packageJsonFileName);
|
||||
result.push(Debug.assertDefined(packageJsonCache.getInDirectory(directory)));
|
||||
const info = packageJsonCache.getInDirectory(directory);
|
||||
if (info) result.push(info);
|
||||
}
|
||||
if (rootPath && rootPath === toPath(directory)) {
|
||||
return true;
|
||||
|
||||
+11
-12
@@ -2210,7 +2210,7 @@ namespace ts {
|
||||
return packageJsons;
|
||||
}
|
||||
|
||||
export function createPackageJsonInfo(fileName: string, host: LanguageServiceHost): PackageJsonInfo | undefined {
|
||||
export function createPackageJsonInfo(fileName: string, host: LanguageServiceHost): PackageJsonInfo | false | undefined {
|
||||
if (!host.readFile) {
|
||||
return undefined;
|
||||
}
|
||||
@@ -2221,19 +2221,18 @@ namespace ts {
|
||||
if (!stringContent) return undefined;
|
||||
|
||||
const content = tryParseJson(stringContent) as PackageJsonRaw;
|
||||
if (!content) return false;
|
||||
const info: Pick<PackageJsonInfo, typeof dependencyKeys[number]> = {};
|
||||
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;
|
||||
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;
|
||||
}
|
||||
|
||||
const dependencyGroups = [
|
||||
|
||||
@@ -74,14 +74,19 @@ namespace ts.projectSystem {
|
||||
|
||||
it("handles errors in json parsing of package.json", () => {
|
||||
const packageJsonContent = `{ "mod" }`;
|
||||
const { project } = setup([tsConfig, { path: packageJson.path, content: packageJsonContent }]);
|
||||
const { project, host } = 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);
|
||||
assert.isUndefined(packageJsonInfo);
|
||||
|
||||
host.writeFile(packageJson.path, packageJson.content);
|
||||
project.getPackageJsonsVisibleToFile("/src/whatever/blah.ts" as Path);
|
||||
const packageJsonInfo2 = project.packageJsonCache.getInDirectory("/" as Path)!;
|
||||
assert.ok(packageJsonInfo2);
|
||||
assert.ok(packageJsonInfo2.dependencies);
|
||||
assert.ok(packageJsonInfo2.devDependencies);
|
||||
assert.ok(packageJsonInfo2.peerDependencies);
|
||||
assert.ok(packageJsonInfo2.optionalDependencies);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
/// <reference path="fourslash.ts" />
|
||||
|
||||
//@noEmit: true
|
||||
|
||||
//@Filename: /package.json
|
||||
////{
|
||||
//// "mod"
|
||||
//// "dependencies": {
|
||||
//// "react": "*"
|
||||
//// }
|
||||
////}
|
||||
|
||||
//@Filename: /node_modules/react/index.d.ts
|
||||
////export declare var React: any;
|
||||
|
||||
//@Filename: /node_modules/react/package.json
|
||||
////{
|
||||
//// "name": "react",
|
||||
//// "types": "./index.d.ts"
|
||||
////}
|
||||
|
||||
//@Filename: /node_modules/fake-react/index.d.ts
|
||||
////export declare var ReactFake: any;
|
||||
|
||||
//@Filename: /node_modules/fake-react/package.json
|
||||
////{
|
||||
//// "name": "fake-react",
|
||||
//// "types": "./index.d.ts"
|
||||
////}
|
||||
|
||||
//@Filename: /src/index.ts
|
||||
////const x = Re/**/
|
||||
|
||||
verify.completions({
|
||||
marker: test.marker(""),
|
||||
isNewIdentifierLocation: true,
|
||||
includes: [{
|
||||
name: "React",
|
||||
hasAction: true,
|
||||
source: "/node_modules/react/index",
|
||||
sortText: completion.SortText.AutoImportSuggestions
|
||||
},
|
||||
{
|
||||
name: "ReactFake",
|
||||
hasAction: true,
|
||||
source: "/node_modules/fake-react/index",
|
||||
sortText: completion.SortText.AutoImportSuggestions
|
||||
}
|
||||
],
|
||||
preferences: {
|
||||
includeCompletionsForModuleExports: true
|
||||
}
|
||||
});
|
||||
@@ -22,36 +22,16 @@
|
||||
////
|
||||
////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,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
goTo.marker("");
|
||||
verify.completions({
|
||||
includes: {
|
||||
name: "readFile",
|
||||
source: "fs",
|
||||
hasAction: true,
|
||||
sortText: completion.SortText.AutoImportSuggestions,
|
||||
},
|
||||
preferences: {
|
||||
includeCompletionsForModuleExports: true,
|
||||
includeInsertTextCompletions: true,
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user