Skip to content

Strip "/index" from import fix module specifiers #19962

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
3 commits merged into from
Nov 16, 2017
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
4 changes: 4 additions & 0 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2169,6 +2169,10 @@ namespace ts {
return expectedPos >= 0 && str.indexOf(suffix, expectedPos) === expectedPos;
}

export function removeSuffix(str: string, suffix: string): string {
return endsWith(str, suffix) ? str.slice(0, str.length - suffix.length) : str;
}

export function stringContains(str: string, substring: string): boolean {
return str.indexOf(substring) !== -1;
}
Expand Down
5 changes: 4 additions & 1 deletion src/harness/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2549,6 +2549,9 @@ Actual: ${stringify(fullActual)}`);
}
const sortedExpectedArray = expectedTextArray.sort();
const sortedActualArray = actualTextArray.sort();
if (sortedExpectedArray.length !== sortedActualArray.length) {
this.raiseError(`Expected ${sortedExpectedArray.length} import fixes, got ${sortedActualArray.length}`);
}
ts.zipWith(sortedExpectedArray, sortedActualArray, (expected, actual, index) => {
if (expected !== actual) {
this.raiseError(`Import fix at index ${index} doesn't match.\n${showTextDiff(expected, actual)}`);
Expand Down Expand Up @@ -2867,7 +2870,7 @@ Actual: ${stringify(fullActual)}`);

if (negative) {
if (codeFixes.length) {
this.raiseError(`verifyCodeFixAvailable failed - expected no fixes but found one.`);
this.raiseError(`verifyCodeFixAvailable failed - expected no fixes but found ${codeFixes.map(c => c.description)}.`);
}
return;
}
Expand Down
15 changes: 6 additions & 9 deletions src/services/codefixes/importFixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ namespace ts.codefix {
tryGetModuleNameAsNodeModule(options, moduleFileName, host, getCanonicalFileName, sourceDirectory) ||
tryGetModuleNameFromBaseUrl(options, moduleFileName, getCanonicalFileName) ||
options.rootDirs && tryGetModuleNameFromRootDirs(options.rootDirs, moduleFileName, sourceDirectory, getCanonicalFileName) ||
removeFileExtension(getRelativePath(moduleFileName, sourceDirectory, getCanonicalFileName));
removeExtensionAndIndexPostFix(getRelativePath(moduleFileName, sourceDirectory, getCanonicalFileName), options);
}

function tryGetModuleNameFromAmbientModule(moduleSymbol: Symbol): string | undefined {
Expand All @@ -343,7 +343,7 @@ namespace ts.codefix {
}

const relativeNameWithIndex = removeFileExtension(relativeName);
relativeName = removeExtensionAndIndexPostFix(relativeName);
relativeName = removeExtensionAndIndexPostFix(relativeName, options);

if (options.paths) {
for (const key in options.paths) {
Expand Down Expand Up @@ -393,7 +393,7 @@ namespace ts.codefix {
return roots && firstDefined(roots, unNormalizedTypeRoot => {
const typeRoot = toPath(unNormalizedTypeRoot, /*basePath*/ undefined, getCanonicalFileName);
if (startsWith(moduleFileName, typeRoot)) {
return removeExtensionAndIndexPostFix(moduleFileName.substring(typeRoot.length + 1));
return removeExtensionAndIndexPostFix(moduleFileName.substring(typeRoot.length + 1), options);
}
});
}
Expand Down Expand Up @@ -527,12 +527,9 @@ namespace ts.codefix {
return firstDefined(rootDirs, rootDir => getRelativePathIfInDirectory(path, rootDir, getCanonicalFileName));
}

function removeExtensionAndIndexPostFix(fileName: string) {
fileName = removeFileExtension(fileName);
if (endsWith(fileName, "/index")) {
fileName = fileName.substr(0, fileName.length - 6/* "/index".length */);
}
return fileName;
function removeExtensionAndIndexPostFix(fileName: string, options: CompilerOptions): string {
const noExtension = removeFileExtension(fileName);
return getEmitModuleResolutionKind(options) === ModuleResolutionKind.NodeJs ? removeSuffix(noExtension, "/index") : noExtension;
}

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

// @Filename: /a/index.ts
////export const foo = 0;

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

goTo.file("/a/index.ts");
goTo.file("/b.ts");

verify.importFixAtPosition([
`import { foo } from "./a";

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

// @moduleResolution: classic

// @Filename: /a/index.ts
////export const foo = 0;

// @Filename: /node_modules/x/index.d.ts
////export const bar = 0;

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

// @Filename: /c.ts
////bar;

goTo.file("/a/index.ts");

goTo.file("/b.ts");
verify.importFixAtPosition([
`import { foo } from "./a/index";

foo;`
]);

goTo.file("/c.ts");
// TODO: GH#20050 verify.not.codeFixAvailable();
1 change: 1 addition & 0 deletions tests/cases/fourslash/importNameCodeFixUMDGlobalReact0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// @jsx: react
// @allowSyntheticDefaultImports: false
// @module: es2015
// @moduleResolution: node

// @Filename: /node_modules/@types/react/index.d.ts
////export = React;
Expand Down
1 change: 1 addition & 0 deletions tests/cases/fourslash/importNameCodeFixUMDGlobalReact1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// @jsx: react
// @allowSyntheticDefaultImports: false
// @module: es2015
// @moduleResolution: node

// @Filename: /node_modules/@types/react/index.d.ts
////export = React;
Expand Down