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

Conversation

ghost
Copy link

@ghost ghost commented Apr 4, 2018

Fixes #23096

  • Only check moduleResolutionKind in one place. And correctly check it using getEmitModuleResolutionKind.
  • This uncovered duplicate completions when we add completions from package.json and add completions from searching through node_modules. Now, we will only do the latter if the former failed.
  • Searching through ambient modules was moved to before this as it doesn't depend on moduleResolutionKind.

@ghost ghost requested review from sheetalkamat and weswigham April 4, 2018 20:21
@@ -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.

}
function getNamesFromVisibleNodeModules(fragmentDirectory: string | undefined, scriptPath: string, host: LanguageServiceHost): string[] {
return flatMap(enumerateNodeModulesVisibleToScript(host, scriptPath), visibleModule => {
if (fragmentDirectory === undefined) {
Copy link
Member

Choose a reason for hiding this comment

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

Isnt this always undefined since its called only when fragmentDirectory ===undefined

Copy link
Author

@ghost ghost Apr 4, 2018

Choose a reason for hiding this comment

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

Wow, if we're always in the first case we can clean out a lot of dead code.

@ghost ghost force-pushed the pathCompletionsNonrelative branch from 5e80112 to be6e420 Compare April 4, 2018 22:27
if (!foundGlobal) {
forEachAncestorDirectory(scriptPath, ancestor => {
const nodeModules = combinePaths(ancestor, "node_modules");
if (host.directoryExists(nodeModules)) {
Copy link
Member

Choose a reason for hiding this comment

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

host.directoryExists is optional, right?

@@ -117,9 +117,10 @@ 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.

let foundGlobal = false;
if (fragmentDirectory === undefined) {
const oldLength = result.length;
getCompletionEntriesFromTypings(host, compilerOptions, scriptPath, result);
Copy link
Member

Choose a reason for hiding this comment

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

earlier this was done irrespective of fragment/module emit .. Why the change?

Copy link
Author

Choose a reason for hiding this comment

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

👍 good catch

@ghost
Copy link
Author

ghost commented Apr 6, 2018

@weswigham Good to go?

@ghost ghost merged commit 70682b7 into master Apr 6, 2018
@ghost ghost deleted the pathCompletionsNonrelative branch April 6, 2018 19:19
@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants