Have the ChangeTracker filter out edits that are no-ops (#38123)

* Filter out edits that are no-ops in 'organize imports'.

* Updated tests for 'organize imports'.

* Always remove no-op changes from the change tracker.

* Add a new `stringContainsAt` helper function to avoid traversing the entire file contents.

* Combine `map`/`filter` sequence into `mapDefined`.

* Fix up documentation.
This commit is contained in:
Daniel Rosenwasser
2020-04-23 12:54:49 -07:00
committed by GitHub
parent 9569e8aaa4
commit d7e437a409
3 changed files with 87 additions and 15 deletions

View File

@@ -285,6 +285,7 @@ namespace ts {
});
});
describe("Baselines", () => {
const libFile = {
@@ -327,6 +328,16 @@ export const Other = 1;
assert.isEmpty(changes);
});
it("doesn't return any changes when the text would be identical", () => {
const testFile = {
path: "/a.ts",
content: `import { f } from 'foo';\nf();`
};
const languageService = makeLanguageService(testFile);
const changes = languageService.organizeImports({ type: "file", fileName: testFile.path }, testFormatSettings, emptyOptions);
assert.isEmpty(changes);
});
testOrganizeImports("Renamed_used",
{
path: "/test.ts",
@@ -366,6 +377,16 @@ D();
},
libFile);
it("doesn't return any changes when the text would be identical", () => {
const testFile = {
path: "/a.ts",
content: `import { f } from 'foo';\nf();`
};
const languageService = makeLanguageService(testFile);
const changes = languageService.organizeImports({ type: "file", fileName: testFile.path }, testFormatSettings, emptyOptions);
assert.isEmpty(changes);
});
testOrganizeImports("Unused_All",
{
path: "/test.ts",
@@ -377,14 +398,17 @@ import D from "lib";
},
libFile);
testOrganizeImports("Unused_Empty",
{
it("Unused_Empty", () => {
const testFile = {
path: "/test.ts",
content: `
import { } from "lib";
`,
},
libFile);
};
const languageService = makeLanguageService(testFile);
const changes = languageService.organizeImports({ type: "file", fileName: testFile.path }, testFormatSettings, emptyOptions);
assert.isEmpty(changes);
});
testOrganizeImports("Unused_false_positive_module_augmentation",
{
@@ -414,25 +438,33 @@ declare module 'caseless' {
test(name: KeyType): boolean;
}
}`
});
});
testOrganizeImports("Unused_false_positive_shorthand_assignment",
{
it("Unused_false_positive_shorthand_assignment", () => {
const testFile = {
path: "/test.ts",
content: `
import { x } from "a";
const o = { x };
`
});
};
const languageService = makeLanguageService(testFile);
const changes = languageService.organizeImports({ type: "file", fileName: testFile.path }, testFormatSettings, emptyOptions);
assert.isEmpty(changes);
});
testOrganizeImports("Unused_false_positive_export_shorthand",
{
it("Unused_false_positive_export_shorthand", () => {
const testFile = {
path: "/test.ts",
content: `
import { x } from "a";
export { x };
`
});
};
const languageService = makeLanguageService(testFile);
const changes = languageService.organizeImports({ type: "file", fileName: testFile.path }, testFormatSettings, emptyOptions);
assert.isEmpty(changes);
});
testOrganizeImports("MoveToTop",
{