Skip to content

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

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
1 commit merged into from
Jan 25, 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
8 changes: 3 additions & 5 deletions src/harness/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2587,12 +2587,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)}`);
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/importFixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.)
Expand Down
21 changes: 21 additions & 0 deletions tests/cases/fourslash/completionsImportBaseUrl.ts
Original file line number Diff line number Diff line change
@@ -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",
});
4 changes: 2 additions & 2 deletions tests/cases/fourslash/importNameCodeFixExistingImport2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();`,
]);
6 changes: 3 additions & 3 deletions tests/cases/fourslash/importNameCodeFixNewImportBaseUrl0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
//// export function f1() { };

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

f1();`,
`import { f1 } from "b";
`import { f1 } from "./a/b";

f1();`
f1();`,
]);
6 changes: 3 additions & 3 deletions tests/cases/fourslash/importNameCodeFixOptionalImport0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();`,
]);
6 changes: 3 additions & 3 deletions tests/cases/fourslash/importNameCodeFixOptionalImport1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
`import { foo } from "bar";

foo();`,

`import { foo } from "bar";
`import { foo } from "./foo";

foo();`,
]);