Skip to content

In import fixes, use a ".js" extension if other imports do #20624

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
2 commits merged into from
Jan 3, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 16 additions & 6 deletions src/services/codefixes/importFixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,10 @@ namespace ts.codefix {
return literal;
}

function usesJsExtensionOnImports(sourceFile: SourceFile): boolean {
return firstDefined(sourceFile.imports, ({ text }) => pathIsRelative(text) ? fileExtensionIs(text, Extension.Js) : undefined) || false;
}

function createImportClauseOfKind(kind: ImportKind.Default | ImportKind.Named | ImportKind.Namespace, symbolName: string) {
const id = createIdentifier(symbolName);
switch (kind) {
Expand All @@ -329,18 +333,19 @@ namespace ts.codefix {
host: LanguageServiceHost,
): string[] {
const { baseUrl, paths, rootDirs } = options;
const addJsExtension = usesJsExtensionOnImports(sourceFile);
const choicesForEachExportingModule = flatMap(moduleSymbols, moduleSymbol =>
getAllModulePaths(program, moduleSymbol.valueDeclaration.getSourceFile()).map(moduleFileName => {
const sourceDirectory = getDirectoryPath(sourceFile.fileName);
const global = tryGetModuleNameFromAmbientModule(moduleSymbol)
|| tryGetModuleNameFromTypeRoots(options, host, getCanonicalFileName, moduleFileName)
|| tryGetModuleNameFromTypeRoots(options, host, getCanonicalFileName, moduleFileName, addJsExtension)
|| tryGetModuleNameAsNodeModule(options, moduleFileName, host, getCanonicalFileName, sourceDirectory)
|| rootDirs && tryGetModuleNameFromRootDirs(rootDirs, moduleFileName, sourceDirectory, getCanonicalFileName);
if (global) {
return [global];
}

const relativePath = removeExtensionAndIndexPostFix(getRelativePath(moduleFileName, sourceDirectory, getCanonicalFileName), options);
const relativePath = removeExtensionAndIndexPostFix(getRelativePath(moduleFileName, sourceDirectory, getCanonicalFileName), options, addJsExtension);
if (!baseUrl) {
return [relativePath];
}
Expand All @@ -350,7 +355,7 @@ namespace ts.codefix {
return [relativePath];
}

const importRelativeToBaseUrl = removeExtensionAndIndexPostFix(relativeToBaseUrl, options);
const importRelativeToBaseUrl = removeExtensionAndIndexPostFix(relativeToBaseUrl, options, addJsExtension);
if (paths) {
const fromPaths = tryGetModuleNameFromPaths(removeFileExtension(relativeToBaseUrl), importRelativeToBaseUrl, paths);
if (fromPaths) {
Expand Down Expand Up @@ -459,12 +464,13 @@ namespace ts.codefix {
host: GetEffectiveTypeRootsHost,
getCanonicalFileName: (file: string) => string,
moduleFileName: string,
addJsExtension: boolean,
): string | undefined {
const roots = getEffectiveTypeRoots(options, host);
return roots && firstDefined(roots, unNormalizedTypeRoot => {
const typeRoot = toPath(unNormalizedTypeRoot, /*basePath*/ undefined, getCanonicalFileName);
if (startsWith(moduleFileName, typeRoot)) {
return removeExtensionAndIndexPostFix(moduleFileName.substring(typeRoot.length + 1), options);
return removeExtensionAndIndexPostFix(moduleFileName.substring(typeRoot.length + 1), options, addJsExtension);
}
});
}
Expand Down Expand Up @@ -598,9 +604,13 @@ namespace ts.codefix {
return firstDefined(rootDirs, rootDir => getRelativePathIfInDirectory(path, rootDir, getCanonicalFileName));
}

function removeExtensionAndIndexPostFix(fileName: string, options: CompilerOptions): string {
function removeExtensionAndIndexPostFix(fileName: string, options: CompilerOptions, addJsExtension: boolean): string {
const noExtension = removeFileExtension(fileName);
return getEmitModuleResolutionKind(options) === ModuleResolutionKind.NodeJs ? removeSuffix(noExtension, "/index") : noExtension;
return addJsExtension
? noExtension + ".js"
: getEmitModuleResolutionKind(options) === ModuleResolutionKind.NodeJs
? removeSuffix(noExtension, "/index")
: noExtension;
}

function getRelativePathIfInDirectory(path: string, directoryPath: string, getCanonicalFileName: GetCanonicalFileName): string | undefined {
Expand Down
22 changes: 22 additions & 0 deletions tests/cases/fourslash/importNameCodeFix_jsExtension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/// <reference path="fourslash.ts" />

// @moduleResolution: node
// @noLib: true

// @Filename: /a.ts
////export function a() {}

// @Filename: /b.ts
////export function b() {}

// @Filename: /c.ts
////import * as g from "global"; // Global imports skipped
////import { a } from "./a.js";
////import { a as a2 } from "./a"; // Ignored, only the first relative import is considered
////[|b;|]

goTo.file("/c.ts");
verify.importFixAtPosition([
`import { b } from "./b.js";
b;`,
]);
2 changes: 1 addition & 1 deletion tests/cases/fourslash/importNameCodeFix_symlink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
////import { foo } from "link";

// @Filename: /b.ts
////[|foo/**/;|]
////[|foo;|]

// Uses "link" instead of "real" because `a` did.
goTo.file("/b.ts");
Expand Down