Skip to content

Deprioritize import paths made up of only dots and slashes - #47432

Merged
Andrew Branch (andrewbranch) merged 4 commits into
microsoft:mainfrom
andrewbranch:bug/45953
Jan 14, 2022
Merged

Andrew Branch (andrewbranch) merged 4 commits into
microsoft:mainfrom
andrewbranch:bug/45953

Conversation

@andrewbranch

Copy link
Copy Markdown
Member

Fixes #45953

Paths like "." and ".." are still available in the codefix dropdown, but sorted to the end of the list. Completions takes only the first module specifier from the list, so they should be gone from completions entirely.

Comment thread src/services/codefixes/importFixes.ts Outdated
}

function isOnlyDotsAndSlashes(path: string) {
return !/[^.\/]/g.test(path);

@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) Jan 14, 2022 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

More coding superstition - just avoid the regex 😄

Suggested change
return !/[^.\/]/g.test(path);
for (let i = 0; i < path.length; i++) {
const ch = path.charCodeAt(i);
if (i !== CharacterCodes.dot && i !== CharacterCodes.slash) return false;
}
return true;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Set up a test on https://jsbench.me (can't share without giving them my email) and that's 25% slower.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Fair.

@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) Jan 14, 2022 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If you do care about perf though, consider hoisting the regex out.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I tested that too, and it was only 5% faster 😉 I’ll probably do it anyway.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

SanicTheHedgehobGIF

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

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.

Auto-import prefers parent index.ts which leads to circular reference

5 participants