Skip to content

Commit d333d88

Browse files
author
Andy
authored
Test for (and fix) order of import fixes (#21398)
1 parent e58391d commit d333d88

7 files changed

+36
-17
lines changed

src/harness/fourslash.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -2572,12 +2572,10 @@ Actual: ${stringify(fullActual)}`);
25722572
actualTextArray.push(text);
25732573
scriptInfo.updateContent(originalContent);
25742574
}
2575-
const sortedExpectedArray = expectedTextArray.sort();
2576-
const sortedActualArray = actualTextArray.sort();
2577-
if (sortedExpectedArray.length !== sortedActualArray.length) {
2578-
this.raiseError(`Expected ${sortedExpectedArray.length} import fixes, got ${sortedActualArray.length}`);
2575+
if (expectedTextArray.length !== actualTextArray.length) {
2576+
this.raiseError(`Expected ${expectedTextArray.length} import fixes, got ${actualTextArray.length}`);
25792577
}
2580-
ts.zipWith(sortedExpectedArray, sortedActualArray, (expected, actual, index) => {
2578+
ts.zipWith(expectedTextArray, actualTextArray, (expected, actual, index) => {
25812579
if (expected !== actual) {
25822580
this.raiseError(`Import fix at index ${index} doesn't match.\n${showTextDiff(expected, actual)}`);
25832581
}

src/services/codefixes/importFixes.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ namespace ts.codefix {
390390
In this case we should prefer using the relative path "../a" instead of the baseUrl path "foo/a".
391391
*/
392392
const pathFromSourceToBaseUrl = getRelativePath(baseUrl, sourceDirectory, getCanonicalFileName);
393-
const relativeFirst = getRelativePathNParents(pathFromSourceToBaseUrl) < getRelativePathNParents(relativePath);
393+
const relativeFirst = getRelativePathNParents(relativePath) < getRelativePathNParents(pathFromSourceToBaseUrl);
394394
return relativeFirst ? [relativePath, importRelativeToBaseUrl] : [importRelativeToBaseUrl, relativePath];
395395
}));
396396
// Only return results for the re-export with the shortest possible path (and also give the other path even if that's long.)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @Filename: /tsconfig.json
4+
////{
5+
//// "compilerOptions": {
6+
//// "baseUrl": "."
7+
//// }
8+
////}
9+
10+
// @Filename: /src/a.ts
11+
////export const foo = 0;
12+
13+
// @Filename: /src/b.ts
14+
////fo/**/
15+
16+
// Test that it prefers a relative import (see sourceDisplay).
17+
goTo.marker("");
18+
verify.completionListContains({ name: "foo", source: "/src/a" }, "const foo: 0", "", "const", undefined, /*hasAction*/ true, {
19+
includeExternalModuleExports: true,
20+
sourceDisplay: "./a",
21+
});

tests/cases/fourslash/importNameCodeFixExistingImport2.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
verify.importFixAtPosition([
1111
`import * as ns from "./module";
12+
ns.f1();`,
13+
`import * as ns from "./module";
1214
import { f1 } from "./module";
1315
f1();`,
14-
`import * as ns from "./module";
15-
ns.f1();`,
1616
]);

tests/cases/fourslash/importNameCodeFixNewImportBaseUrl0.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
//// export function f1() { };
1414

1515
verify.importFixAtPosition([
16-
`import { f1 } from "./a/b";
16+
`import { f1 } from "b";
1717
1818
f1();`,
19-
`import { f1 } from "b";
19+
`import { f1 } from "./a/b";
2020
21-
f1();`
21+
f1();`,
2222
]);

tests/cases/fourslash/importNameCodeFixOptionalImport0.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212

1313
verify.importFixAtPosition([
1414
`import * as ns from "./foo";
15-
import { foo } from "./foo";
16-
foo();`,
15+
ns.foo();`,
1716

1817
`import * as ns from "./foo";
19-
ns.foo();`,
18+
import { foo } from "./foo";
19+
foo();`,
2020
]);

tests/cases/fourslash/importNameCodeFixOptionalImport1.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
//// export function foo() {};
88

99
// @Filename: a/foo.ts
10-
//// export { foo } from "bar";
10+
//// export { foo } from "bar";
1111

1212
verify.importFixAtPosition([
13-
`import { foo } from "./foo";
13+
`import { foo } from "bar";
1414
1515
foo();`,
1616

17-
`import { foo } from "bar";
17+
`import { foo } from "./foo";
1818
1919
foo();`,
2020
]);

0 commit comments

Comments
 (0)