-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
At types prefix and import suffix #15545
Conversation
@@ -501,6 +501,14 @@ namespace ts.codefix { | |||
relativeFileName = getRelativePath(moduleFileName, sourceDirectory); | |||
} | |||
|
|||
if (startsWith(relativeFileName, "@types/")) { | |||
relativeFileName = relativeFileName.substr(/*"@types".length*/ 7); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"@types".length
is 6. Why not just use that expression? You could even add a stripStart(str, prefix): string | undefined
helper so you don't have to repeat this kind of code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be "@types/"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed.
@@ -501,6 +501,14 @@ namespace ts.codefix { | |||
relativeFileName = getRelativePath(moduleFileName, sourceDirectory); | |||
} | |||
|
|||
if (startsWith(relativeFileName, "@types/")) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to have this code next to mangleScopedPackage
in moduleNameResolver.ts
. Maybe call it getPackageNameFromAtTypesDirectory
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed.
src/compiler/core.ts
Outdated
@@ -1730,13 +1730,18 @@ namespace ts { | |||
|
|||
/* @internal */ | |||
export function startsWith(str: string, prefix: string): boolean { | |||
return str.lastIndexOf(prefix, 0) === 0; | |||
return str.indexOf(prefix) === 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will change the performance -- previously it would only look at the start, now it will look everywhere and then check whether the result was the start.
src/compiler/core.ts
Outdated
} | ||
|
||
/* @internal */ | ||
export function removePrefix(str: string, prefix: string | undefined): string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why allow an undefined prefix? This calls startsWith
which demands a defined prefix.
We can't get --strictNullChecks
on soon enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
startsWith
works with an undefined prefix. I'll change the annotation there as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevermind, it's unnecessary.
src/compiler/core.ts
Outdated
} | ||
|
||
/* @internal */ | ||
export function endsWith(str: string, suffix: string): boolean { | ||
const expectedPos = str.length - suffix.length; | ||
return expectedPos >= 0 && str.indexOf(suffix, expectedPos) === expectedPos; | ||
return expectedPos >= 0 && str.lastIndexOf(suffix, expectedPos) === expectedPos; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my comment on startsWith
.
@@ -522,6 +522,8 @@ namespace ts.codefix { | |||
catch (e) { } | |||
} | |||
|
|||
relativeFileName = getPackageNameFromAtTypesDirectory(relativeFileName); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't need an assignment statement if you're just returning that expression.
src/compiler/core.ts
Outdated
@@ -1747,7 +1752,7 @@ namespace ts { | |||
return path.length > extension.length && endsWith(path, extension); | |||
} | |||
|
|||
export function fileExtensionIsAny(path: string, extensions: string[]): boolean { | |||
export function fileExtensionIsOneOf(path: string, extensions: string[]): boolean { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably be @internal
too.
src/compiler/moduleNameResolver.ts
Outdated
@@ -973,6 +976,16 @@ namespace ts { | |||
return moduleName; | |||
} | |||
|
|||
export function getPackageNameFromAtTypesDirectory(mangledName: string): string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@internal
|
||
// @Filename: node_modules/@types/myLib/index.d.ts | ||
//// export function f1() {} | ||
//// export var v1 = 5; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem to be used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It exercises whether we look through the set of exported members correctly.
@aozgaa is this PR ready to go? |
Fixes #14157