Skip to content

At types prefix and import suffix #15545

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 9 commits into from
May 22, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 8 additions & 0 deletions src/services/codefixes/importFixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,14 @@ namespace ts.codefix {
relativeFileName = getRelativePath(moduleFileName, sourceDirectory);
}

if (startsWith(relativeFileName, "@types/")) {
Copy link

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed.

relativeFileName = relativeFileName.substr(/*"@types".length*/ 7);
Copy link

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should be "@types/"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed.

if (relativeFileName.indexOf("__") !== -1) {
// Double underscores are used in DefinitelyTyped to delimit scoped packages.
relativeFileName = "@" + relativeFileName.replace("__", "/");
}
}

relativeFileName = removeFileExtension(relativeFileName);
if (endsWith(relativeFileName, "/index")) {
relativeFileName = getDirectoryPath(relativeFileName);
Expand Down
13 changes: 13 additions & 0 deletions tests/cases/fourslash/importNameCodeFixNewImportFromAtTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference path="fourslash.ts" />

//// [|f1/*0*/();|]

// @Filename: node_modules/@types/myLib/index.d.ts
//// export function f1() {}
//// export var v1 = 5;
Copy link

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?

Copy link
Contributor Author

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.


verify.importFixAtPosition([
`import { f1 } from "myLib";

f1();`
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference path="fourslash.ts" />

//// [|f1/*0*/();|]

// @Filename: node_modules/@types/myLib__scoped/index.d.ts
//// export function f1() {}
//// export var v1 = 5;

verify.importFixAtPosition([
`import { f1 } from "@myLib/scoped";

f1();`
]);