Test for (and fix) order of import fixes (#21398)

This commit is contained in:
Andy
2018-01-24 15:06:52 -08:00
committed by GitHub
parent e58391d9c5
commit d333d889c1
7 changed files with 38 additions and 19 deletions

View File

@@ -2572,12 +2572,10 @@ Actual: ${stringify(fullActual)}`);
actualTextArray.push(text);
scriptInfo.updateContent(originalContent);
}
const sortedExpectedArray = expectedTextArray.sort();
const sortedActualArray = actualTextArray.sort();
if (sortedExpectedArray.length !== sortedActualArray.length) {
this.raiseError(`Expected ${sortedExpectedArray.length} import fixes, got ${sortedActualArray.length}`);
if (expectedTextArray.length !== actualTextArray.length) {
this.raiseError(`Expected ${expectedTextArray.length} import fixes, got ${actualTextArray.length}`);
}
ts.zipWith(sortedExpectedArray, sortedActualArray, (expected, actual, index) => {
ts.zipWith(expectedTextArray, actualTextArray, (expected, actual, index) => {
if (expected !== actual) {
this.raiseError(`Import fix at index ${index} doesn't match.\n${showTextDiff(expected, actual)}`);
}

View File

@@ -390,7 +390,7 @@ namespace ts.codefix {
In this case we should prefer using the relative path "../a" instead of the baseUrl path "foo/a".
*/
const pathFromSourceToBaseUrl = getRelativePath(baseUrl, sourceDirectory, getCanonicalFileName);
const relativeFirst = getRelativePathNParents(pathFromSourceToBaseUrl) < getRelativePathNParents(relativePath);
const relativeFirst = getRelativePathNParents(relativePath) < getRelativePathNParents(pathFromSourceToBaseUrl);
return relativeFirst ? [relativePath, importRelativeToBaseUrl] : [importRelativeToBaseUrl, relativePath];
}));
// Only return results for the re-export with the shortest possible path (and also give the other path even if that's long.)

View File

@@ -0,0 +1,21 @@
/// <reference path="fourslash.ts" />
// @Filename: /tsconfig.json
////{
//// "compilerOptions": {
//// "baseUrl": "."
//// }
////}
// @Filename: /src/a.ts
////export const foo = 0;
// @Filename: /src/b.ts
////fo/**/
// Test that it prefers a relative import (see sourceDisplay).
goTo.marker("");
verify.completionListContains({ name: "foo", source: "/src/a" }, "const foo: 0", "", "const", undefined, /*hasAction*/ true, {
includeExternalModuleExports: true,
sourceDisplay: "./a",
});

View File

@@ -9,8 +9,8 @@
verify.importFixAtPosition([
`import * as ns from "./module";
ns.f1();`,
`import * as ns from "./module";
import { f1 } from "./module";
f1();`,
`import * as ns from "./module";
ns.f1();`,
]);

View File

@@ -13,10 +13,10 @@
//// export function f1() { };
verify.importFixAtPosition([
`import { f1 } from "b";
f1();`,
`import { f1 } from "./a/b";
f1();`,
`import { f1 } from "b";
f1();`
]);

View File

@@ -12,9 +12,9 @@
verify.importFixAtPosition([
`import * as ns from "./foo";
import { foo } from "./foo";
foo();`,
ns.foo();`,
`import * as ns from "./foo";
ns.foo();`,
import { foo } from "./foo";
foo();`,
]);

View File

@@ -7,14 +7,14 @@
//// export function foo() {};
// @Filename: a/foo.ts
//// export { foo } from "bar";
//// export { foo } from "bar";
verify.importFixAtPosition([
`import { foo } from "./foo";
foo();`,
`import { foo } from "bar";
foo();`,
`import { foo } from "./foo";
foo();`,
]);