Skip to content

Fix ordering of module specifiers based on package.json presence #46437

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
merged 2 commits into from
Oct 21, 2021

Conversation

andrewbranch
Copy link
Member

Fixes #46332

Guess I got Comparison.GreaterThan and Comparison.LessThan mixed up 9 months ago in #42614 😵

function compareModuleSpecifiers(a: ImportFix, b: ImportFix, importingFile: SourceFile, program: Program, allowsImportingSpecifier: (specifier: string) => boolean): Comparison {
if (a.kind !== ImportFixKind.UseNamespace && b.kind !== ImportFixKind.UseNamespace) {
return compareBooleans(allowsImportingSpecifier(a.moduleSpecifier), allowsImportingSpecifier(b.moduleSpecifier))
return compareBooleans(allowsImportingSpecifier(b.moduleSpecifier), allowsImportingSpecifier(a.moduleSpecifier))
Copy link
Member Author

Choose a reason for hiding this comment

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

From compareBooleans:

true is greater than false

So we need to reverse the order here to ensure module specifiers that are allowed are sorted LessThan module specifiers that aren’t.

Copy link
Member

Choose a reason for hiding this comment

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

just for my own understanding: is there a reason why you chose to reverse a and b here instead of, say, changing from Comparison.LessThan to Comparison.GreaterThan?

Copy link
Member Author

@andrewbranch andrewbranch Oct 21, 2021

Choose a reason for hiding this comment

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

It was simplest to leave the sort order going from LessThan to GreaterThan (just like numbers are sorted), so I think the simplest two options were this and

Suggested change
return compareBooleans(allowsImportingSpecifier(b.moduleSpecifier), allowsImportingSpecifier(a.moduleSpecifier))
return compareBooleans(!allowsImportingSpecifier(a.moduleSpecifier), !allowsImportingSpecifier(b.moduleSpecifier))

which would be read like “true is greater than falsedoesn’t allow importing this specifier is greater than allows importing this specifier → ones with disallowed specifiers are sorted last.” That might be more direct reading from zero context, but when I realized I just needed to reverse the polarity of this comparison, swapping the order of the comparison was my go-to, probably because of the common analog of

nums.sort((a, b) => a - b); // how do you reverse this sort order?
nums.sort((a, b) => b - a); // like this

The other option that occurred to me was multiplying the comparison by -1, but I felt like that defeated the semi-opaque quality of the Comparison enum.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ultimately, this whole comparison abstraction makes it really nice to read at a high level what factors are considered in sorting/comparing, but very confusing to map onto the low level of what .reduce or .sort is going to do. Good for understanding the big picture, bad for fixing bugs like this.

Copy link
Member

Choose a reason for hiding this comment

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

thanks for explaining your thought process! 😊

function compareModuleSpecifiers(a: ImportFix, b: ImportFix, importingFile: SourceFile, program: Program, allowsImportingSpecifier: (specifier: string) => boolean): Comparison {
if (a.kind !== ImportFixKind.UseNamespace && b.kind !== ImportFixKind.UseNamespace) {
return compareBooleans(allowsImportingSpecifier(a.moduleSpecifier), allowsImportingSpecifier(b.moduleSpecifier))
return compareBooleans(allowsImportingSpecifier(b.moduleSpecifier), allowsImportingSpecifier(a.moduleSpecifier))
Copy link
Member

Choose a reason for hiding this comment

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

just for my own understanding: is there a reason why you chose to reverse a and b here instead of, say, changing from Comparison.LessThan to Comparison.GreaterThan?

@andrewbranch andrewbranch merged commit 22f37cd into microsoft:main Oct 21, 2021
@andrewbranch andrewbranch deleted the bug/46332 branch October 21, 2021 18:44
mprobst pushed a commit to mprobst/TypeScript that referenced this pull request Jan 10, 2022
…rosoft#46437)

* Add failing test

* Fix ordering of module specifiers based on package.json presence
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Milestone Bug PRs that fix a bug with a specific milestone
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Debug failure on auto-import completion entry details
3 participants