Skip to content

Commit 276b56d

Browse files
committed
More PR feedback
1 parent 34847f0 commit 276b56d

File tree

2 files changed

+51
-19
lines changed

2 files changed

+51
-19
lines changed

src/services/services.ts

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,6 +2076,8 @@ namespace ts {
20762076
*/
20772077
const tripleSlashDirectiveFragmentRegex = /^(\/\/\/\s*<reference\s+(path|types)\s*=\s*(?:'|"))([^\3]*)$/;
20782078

2079+
const nodeModulesDependencyKeys = ["dependencies", "devDependencies", "peerDependencies", "optionalDependencies"];
2080+
20792081
let commandLineOptionsStringToEnum: CommandLineOptionOfCustomType[];
20802082

20812083
/** JS users may pass in string values for enum compiler options (such as ModuleKind), so convert. */
@@ -4606,23 +4608,27 @@ namespace ts {
46064608
if (host.readDirectory) {
46074609
// Enumerate the available files if possible
46084610
const files = host.readDirectory(baseDirectory, extensions, /*exclude*/undefined, /*include*/["./*"]);
4611+
const foundFiles = createMap<boolean>();
46094612
for (let filePath of files) {
46104613
filePath = normalizePath(filePath);
46114614
if (exclude && comparePaths(filePath, exclude, scriptPath, ignoreCase) === Comparison.EqualTo) {
46124615
continue;
46134616
}
46144617

4615-
const fileName = includeExtensions ? getBaseFileName(filePath) : removeFileExtension(getBaseFileName(filePath));
4616-
const duplicate = !includeExtensions && forEach(result, entry => entry.name === fileName);
4618+
const foundFileName = includeExtensions ? getBaseFileName(filePath) : removeFileExtension(getBaseFileName(filePath));
46174619

4618-
if (!duplicate) {
4619-
result.push({
4620-
name: fileName,
4621-
kind: ScriptElementKind.scriptElement,
4622-
sortText: fileName
4623-
});
4620+
if (!foundFiles[foundFileName]) {
4621+
foundFiles[foundFileName] = true;
46244622
}
46254623
}
4624+
4625+
for (const foundFile in foundFiles) {
4626+
result.push({
4627+
name: foundFile,
4628+
kind: ScriptElementKind.scriptElement,
4629+
sortText: foundFile
4630+
});
4631+
}
46264632
}
46274633

46284634
// If possible, get folder completion as well
@@ -4836,7 +4842,7 @@ namespace ts {
48364842
function getCompletionEntriesFromTypings(host: LanguageServiceHost, options: CompilerOptions, scriptPath: string, result: ImportCompletionEntry[] = []): ImportCompletionEntry[] {
48374843
// Check for typings specified in compiler options
48384844
if (options.types) {
4839-
for (const moduleName of options.types){
4845+
for (const moduleName of options.types) {
48404846
result.push(createCompletionEntryForModule(moduleName, ScriptElementKind.externalModuleName));
48414847
}
48424848
}
@@ -4911,11 +4917,9 @@ namespace ts {
49114917
const nodeModulesDir = combinePaths(getDirectoryPath(packageJson), "node_modules");
49124918
const foundModuleNames: string[] = [];
49134919

4914-
if (package.dependencies) {
4915-
addPotentialPackageNames(package.dependencies, foundModuleNames);
4916-
}
4917-
if (package.devDependencies) {
4918-
addPotentialPackageNames(package.devDependencies, foundModuleNames);
4920+
// Provide completions for all non @types dependencies
4921+
for (const key of nodeModulesDependencyKeys) {
4922+
addPotentialPackageNames(package[key], foundModuleNames);
49194923
}
49204924

49214925
for (const moduleName of foundModuleNames) {
@@ -4940,11 +4944,12 @@ namespace ts {
49404944
}
49414945
}
49424946

4943-
// Add all the package names that are not in the @types scope
49444947
function addPotentialPackageNames(dependencies: any, result: string[]) {
4945-
for (const dep in dependencies) {
4946-
if (dependencies.hasOwnProperty(dep) && !startsWith(dep, "@types/")) {
4947-
result.push(dep);
4948+
if (dependencies) {
4949+
for (const dep in dependencies) {
4950+
if (dependencies.hasOwnProperty(dep) && !startsWith(dep, "@types/")) {
4951+
result.push(dep);
4952+
}
49484953
}
49494954
}
49504955
}
@@ -4955,7 +4960,6 @@ namespace ts {
49554960
}
49564961

49574962
// Replace everything after the last directory seperator that appears
4958-
// FIXME: do we care about the other seperator?
49594963
function getDirectoryFragmentTextSpan(text: string, textStart: number): TextSpan {
49604964
const index = text.lastIndexOf(directorySeparator);
49614965
const offset = index !== -1 ? index + 1 : 0;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// Should give completions for all dependencies in package.json
4+
5+
// @Filename: tests/test0.ts
6+
//// import * as foo1 from "m/*import_as0*/
7+
//// import foo2 = require("m/*import_equals0*/
8+
//// var foo3 = require("m/*require0*/
9+
10+
// @Filename: package.json
11+
//// {
12+
//// "dependencies": { "module": "latest" },
13+
//// "devDependencies": { "dev-module": "latest" },
14+
//// "optionalDependencies": { "optional-module": "latest" },
15+
//// "peerDependencies": { "peer-module": "latest" }
16+
//// }
17+
18+
const kinds = ["import_as", "import_equals", "require"];
19+
20+
for (const kind of kinds) {
21+
goTo.marker(kind + "0");
22+
23+
verify.importModuleCompletionListContains("module");
24+
verify.importModuleCompletionListContains("dev-module");
25+
verify.importModuleCompletionListContains("optional-module");
26+
verify.importModuleCompletionListContains("peer-module");
27+
verify.not.importModuleCompletionListItemsCountIsGreaterThan(4);
28+
}

0 commit comments

Comments
 (0)