Skip to content

Clean up code for nonrelative path completions #23150

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

Merged
4 commits merged into from
Apr 6, 2018
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
7 changes: 4 additions & 3 deletions src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ namespace ts {
}
}

function readJson(path: string, host: ModuleResolutionHost): PackageJson {
/* @internal */
export function readJson(path: string, host: { readFile(fileName: string): string | undefined }): object {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not mark readFile optional?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, left in a change to the function body on accident. This should not be optional here because it is checked by the callers.

try {
const jsonText = host.readFile(path);
return jsonText ? JSON.parse(jsonText) : {};
Expand Down Expand Up @@ -300,7 +301,7 @@ namespace ts {
// `types-publisher` sometimes creates packages with `"typings": null` for packages that don't provide their own types.
// See `createNotNeededPackageJSON` in the types-publisher` repo.
// tslint:disable-next-line:no-null-keyword
const isNotNeededPackage = host.fileExists(packageJsonPath) && readJson(packageJsonPath, host).typings === null;
const isNotNeededPackage = host.fileExists(packageJsonPath) && (readJson(packageJsonPath, host) as PackageJson).typings === null;
if (!isNotNeededPackage) {
// Return just the type directive names
result.push(getBaseFileName(normalized));
Expand Down Expand Up @@ -983,7 +984,7 @@ namespace ts {
const directoryExists = !onlyRecordFailures && directoryProbablyExists(nodeModuleDirectory, host);
const packageJsonPath = pathToPackageJson(nodeModuleDirectory);
if (directoryExists && host.fileExists(packageJsonPath)) {
const packageJsonContent = readJson(packageJsonPath, host);
const packageJsonContent = readJson(packageJsonPath, host) as PackageJson;
if (subModuleName === "") { // looking up the root - need to handle types/typings/main redirects for subModuleName
const path = tryReadPackageJsonFields(/*readTypes*/ true, packageJsonContent, nodeModuleDirectory, state);
if (typeof path === "string") {
Expand Down
143 changes: 48 additions & 95 deletions src/services/pathCompletions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,9 @@ namespace ts.Completions.PathCompletions {
if (directories) {
for (const directory of directories) {
const directoryName = getBaseFileName(normalizePath(directory));

result.push(nameAndKind(directoryName, ScriptElementKind.directory));
if (directoryName !== "@types") {
result.push(nameAndKind(directoryName, ScriptElementKind.directory));
}
}
}
}
Expand Down Expand Up @@ -177,19 +178,33 @@ namespace ts.Completions.PathCompletions {
}
}

if (compilerOptions.moduleResolution === ModuleResolutionKind.NodeJs) {
forEachAncestorDirectory(scriptPath, ancestor => {
const nodeModules = combinePaths(ancestor, "node_modules");
if (host.directoryExists(nodeModules)) {
getCompletionEntriesForDirectoryFragment(fragment, nodeModules, fileExtensions, /*includeExtensions*/ false, host, /*exclude*/ undefined, result);
}
});
const fragmentDirectory = containsSlash(fragment) ? getDirectoryPath(fragment) : undefined;
for (const ambientName of getAmbientModuleCompletions(fragment, fragmentDirectory, typeChecker)) {
result.push(nameAndKind(ambientName, ScriptElementKind.externalModuleName));
}

getCompletionEntriesFromTypings(host, compilerOptions, scriptPath, result);

for (const moduleName of enumeratePotentialNonRelativeModules(fragment, scriptPath, compilerOptions, typeChecker, host)) {
result.push(nameAndKind(moduleName, ScriptElementKind.externalModuleName));
if (getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.NodeJs) {
// If looking for a global package name, don't just include everything in `node_modules` because that includes dependencies' own dependencies.
// (But do if we didn't find anything, e.g. 'package.json' missing.)
let foundGlobal = false;
if (fragmentDirectory === undefined) {
for (const moduleName of enumerateNodeModulesVisibleToScript(host, scriptPath)) {
if (!result.some(entry => entry.name === moduleName)) {
foundGlobal = true;
result.push(nameAndKind(moduleName, ScriptElementKind.externalModuleName));
}
}
}
if (!foundGlobal) {
forEachAncestorDirectory(scriptPath, ancestor => {
const nodeModules = combinePaths(ancestor, "node_modules");
if (tryDirectoryExists(host, nodeModules)) {
getCompletionEntriesForDirectoryFragment(fragment, nodeModules, fileExtensions, /*includeExtensions*/ false, host, /*exclude*/ undefined, result);
}
});
}
}

return result;
Expand Down Expand Up @@ -228,7 +243,7 @@ namespace ts.Completions.PathCompletions {
const normalizedPrefixDirectory = getDirectoryPath(normalizedPrefix);
const normalizedPrefixBase = getBaseFileName(normalizedPrefix);

const fragmentHasPath = stringContains(fragment, directorySeparator);
const fragmentHasPath = containsSlash(fragment);

// Try and expand the prefix to include any path from the fragment so that we can limit the readDirectory call
const expandedPrefixDirectory = fragmentHasPath ? combinePaths(normalizedPrefixDirectory, normalizedPrefixBase + getDirectoryPath(fragment)) : normalizedPrefixDirectory;
Expand Down Expand Up @@ -262,45 +277,19 @@ namespace ts.Completions.PathCompletions {
return path[0] === directorySeparator ? path.slice(1) : path;
}

function enumeratePotentialNonRelativeModules(fragment: string, scriptPath: string, options: CompilerOptions, typeChecker: TypeChecker, host: LanguageServiceHost): string[] {
// Check If this is a nested module
const isNestedModule = stringContains(fragment, directorySeparator);
const moduleNameFragment = isNestedModule ? fragment.substr(0, fragment.lastIndexOf(directorySeparator)) : undefined;

function getAmbientModuleCompletions(fragment: string, fragmentDirectory: string | undefined, checker: TypeChecker): ReadonlyArray<string> {
// Get modules that the type checker picked up
const ambientModules = map(typeChecker.getAmbientModules(), sym => stripQuotes(sym.name));
let nonRelativeModuleNames = filter(ambientModules, moduleName => startsWith(moduleName, fragment));
const ambientModules = checker.getAmbientModules().map(sym => stripQuotes(sym.name));
const nonRelativeModuleNames = ambientModules.filter(moduleName => startsWith(moduleName, fragment));

// Nested modules of the form "module-name/sub" need to be adjusted to only return the string
// after the last '/' that appears in the fragment because that's where the replacement span
// starts
if (isNestedModule) {
const moduleNameWithSeperator = ensureTrailingDirectorySeparator(moduleNameFragment);
nonRelativeModuleNames = map(nonRelativeModuleNames, nonRelativeModuleName => {
return removePrefix(nonRelativeModuleName, moduleNameWithSeperator);
});
}


if (!options.moduleResolution || options.moduleResolution === ModuleResolutionKind.NodeJs) {
for (const visibleModule of enumerateNodeModulesVisibleToScript(host, scriptPath)) {
if (!isNestedModule) {
nonRelativeModuleNames.push(visibleModule.moduleName);
}
else if (startsWith(visibleModule.moduleName, moduleNameFragment)) {
const nestedFiles = tryReadDirectory(host, visibleModule.moduleDir, supportedTypeScriptExtensions, /*exclude*/ undefined, /*include*/ ["./*"]);
if (nestedFiles) {
for (let f of nestedFiles) {
f = normalizePath(f);
const nestedModule = removeFileExtension(getBaseFileName(f));
nonRelativeModuleNames.push(nestedModule);
}
}
}
}
if (fragmentDirectory !== undefined) {
const moduleNameWithSeperator = ensureTrailingDirectorySeparator(fragmentDirectory);
return nonRelativeModuleNames.map(nonRelativeModuleName => removePrefix(nonRelativeModuleName, moduleNameWithSeperator));
}

return deduplicate(nonRelativeModuleNames, equateStringsCaseSensitive, compareStringsCaseSensitive);
return nonRelativeModuleNames;
}

export function getTripleSlashReferenceCompletion(sourceFile: SourceFile, position: number, compilerOptions: CompilerOptions, host: LanguageServiceHost): ReadonlyArray<PathCompletion> | undefined {
Expand Down Expand Up @@ -390,55 +379,24 @@ namespace ts.Completions.PathCompletions {
return paths;
}

function enumerateNodeModulesVisibleToScript(host: LanguageServiceHost, scriptPath: string) {
const result: VisibleModuleInfo[] = [];

if (host.readFile && host.fileExists) {
for (const packageJson of findPackageJsons(scriptPath, host)) {
const contents = tryReadingPackageJson(packageJson);
if (!contents) {
return;
}
function enumerateNodeModulesVisibleToScript(host: LanguageServiceHost, scriptPath: string): ReadonlyArray<string> {
if (!host.readFile || !host.fileExists) return emptyArray;

const nodeModulesDir = combinePaths(getDirectoryPath(packageJson), "node_modules");
const foundModuleNames: string[] = [];

// Provide completions for all non @types dependencies
for (const key of nodeModulesDependencyKeys) {
addPotentialPackageNames(contents[key], foundModuleNames);
}

for (const moduleName of foundModuleNames) {
const moduleDir = combinePaths(nodeModulesDir, moduleName);
result.push({
moduleName,
moduleDir
});
}
}
}

return result;

function tryReadingPackageJson(filePath: string) {
try {
const fileText = tryReadFile(host, filePath);
return fileText ? JSON.parse(fileText) : undefined;
}
catch (e) {
return undefined;
}
}

function addPotentialPackageNames(dependencies: any, result: string[]) {
if (dependencies) {
const result: string[] = [];
for (const packageJson of findPackageJsons(scriptPath, host)) {
const contents = readJson(packageJson, host as { readFile: (filename: string) => string | undefined }); // Cast to assert that readFile is defined
// Provide completions for all non @types dependencies
for (const key of nodeModulesDependencyKeys) {
const dependencies: object | undefined = (contents as any)[key];
if (!dependencies) continue;
for (const dep in dependencies) {
if (dependencies.hasOwnProperty(dep) && !startsWith(dep, "@types/")) {
result.push(dep);
}
}
}
}
return result;
}

// Replace everything after the last directory seperator that appears
Expand Down Expand Up @@ -484,11 +442,6 @@ namespace ts.Completions.PathCompletions {
*/
const tripleSlashDirectiveFragmentRegex = /^(\/\/\/\s*<reference\s+(path|types)\s*=\s*(?:'|"))([^\3"]*)$/;

interface VisibleModuleInfo {
moduleName: string;
moduleDir: string;
}

const nodeModulesDependencyKeys = ["dependencies", "devDependencies", "peerDependencies", "optionalDependencies"];

function tryGetDirectories(host: LanguageServiceHost, directoryName: string): string[] {
Expand All @@ -499,10 +452,6 @@ namespace ts.Completions.PathCompletions {
return tryIOAndConsumeErrors(host, host.readDirectory, path, extensions, exclude, include) || emptyArray;
}

function tryReadFile(host: LanguageServiceHost, path: string): string | undefined {
return tryIOAndConsumeErrors(host, host.readFile, path);
}

function tryFileExists(host: LanguageServiceHost, path: string): boolean {
return tryIOAndConsumeErrors(host, host.fileExists, path);
}
Expand All @@ -522,4 +471,8 @@ namespace ts.Completions.PathCompletions {
catch { /*ignore*/ }
return undefined;
}

function containsSlash(fragment: string) {
return stringContains(fragment, directorySeparator);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,4 @@
// @Filename: ambient.ts
//// declare module "fake-module/other"

const kinds = ["import_as", "import_equals", "require"];

for (const kind of kinds) {
goTo.marker(kind + "0");
verify.completionListContains("repeated");
verify.completionListContains("other");
verify.not.completionListItemsCountIsGreaterThan(2);
}
verify.completionsAt(["import_as0", "import_equals0", "require0"], ["other", "repeated"], { isNewIdentifierLocation: true })
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,4 @@
// @Filename: node_modules/fake-module/repeated.jsx
//// /*repeatedjsx*/

const kinds = ["import_as", "import_equals", "require"];

for (const kind of kinds) {
goTo.marker(kind + "0");
verify.completionListContains("ts");
verify.completionListContains("tsx");
verify.completionListContains("dts");
verify.not.completionListItemsCountIsGreaterThan(3);
}
verify.completionsAt(["import_as0", "import_equals0", "require0"], ["dts", "js", "jsx", "repeated", "ts", "tsx"], { isNewIdentifierLocation: true });
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,4 @@
// @Filename: package.json
//// { "dependencies": { "@types/module-y": "latest" } }

const kinds = ["types_ref", "import_as", "import_equals", "require"];

for (const kind of kinds) {
goTo.marker(kind + "0");
verify.completionListContains("module-x");
verify.completionListContains("module-y");
verify.not.completionListItemsCountIsGreaterThan(2);
}
verify.completionsAt(["types_ref0", "import_as0", "import_equals0", "require0"], ["module-x", "module-y"], { isNewIdentifierLocation: true });
2 changes: 1 addition & 1 deletion tests/cases/fourslash/completionListInImportClause05.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
// NOTE: The node_modules folder is in "/", rather than ".", because it requires
// less scaffolding to mock. In particular, "/" is where we look for type roots.

verify.completionsAt("1", ["@a/b", "@c/d", "@e/f"], { isNewIdentifierLocation: true });
verify.completionsAt("1", ["@e/f", "@a/b", "@c/d"], { isNewIdentifierLocation: true });
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The order changed because I moved up the check for ambient module declarations. Don't think it matters.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the results be sorted?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They weren't sorted by name before, except by coincidence. We could make a PR sorting them but it would change a bunch of baselines. I think editors do their own sorting on the result anyway.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright.

1 change: 0 additions & 1 deletion tests/cases/fourslash/completionsPaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,5 @@
// @Filename: /src/folder/4.ts
////const foo = require(`x//*4*/`);

const [r0, r1, r2, r3] = test.ranges();
verify.completionsAt("1", ["y", "x"], { isNewIdentifierLocation: true });
verify.completionsAt(["2", "3", "4"], ["bar", "foo"], { isNewIdentifierLocation: true });