Added skipDestructiveCodeActions argument to organize imports server command (#43184)

* Stopped removing unused imports in files with syntactic errors

* Added allowDestructiveCodeActions arg

* Updated .d.ts baselines

* Stop factoring syntax errors. Weird that no tests break...

* Have args extend scope so it is not a breaking change

* Update src/harness/harnessLanguageService.ts

Co-authored-by: Jesse Trinity <jetrinit@microsoft.com>

* Fixed API breaking change, and renamed to skip

* Always with the baselines

* One more .d.ts baseline to fix

* Remove blank line in src/harness/harnessLanguageService.ts

Co-authored-by: Jesse Trinity <jetrinit@microsoft.com>
This commit is contained in:
Josh GoldbergandJesse Trinity authored and GitHub committed 2021-04-20 09:04:17 -07:00
1 parent f67ee44379
commit a910c8df13
14 files changed
+200 -23

No files matched your search

@@ -339,6 +339,7 @@ export const Other = 1;
});
testOrganizeImports("Renamed_used",
/*skipDestructiveCodeActions*/ false,
{
path: "/test.ts",
content: `
@@ -349,6 +350,7 @@ EffOne();
libFile);
testOrganizeImports("Simple",
/*skipDestructiveCodeActions*/ false,
{
path: "/test.ts",
content: `
@@ -365,6 +367,7 @@ F2();
libFile);
testOrganizeImports("Unused_Some",
/*skipDestructiveCodeActions*/ false,
{
path: "/test.ts",
content: `
@@ -377,6 +380,70 @@ D();
},
libFile);
describe("skipDestructiveCodeActions=true", () => {
testOrganizeImports("Syntax_Error_Body_skipDestructiveCodeActions",
/*skipDestructiveCodeActions*/ true,
{
path: "/test.ts",
content: `
import { F1, F2 } from "lib";
import * as NS from "lib";
import D from "lib";
class class class;
D;
`,
},
libFile);
});
testOrganizeImports("Syntax_Error_Imports_skipDestructiveCodeActions",
/*skipDestructiveCodeActions*/ true,
{
path: "/test.ts",
content: `
import { F1, F2 class class class; } from "lib";
import * as NS from "lib";
class class class;
import D from "lib";
D;
`,
},
libFile);
describe("skipDestructiveCodeActions=false", () => {
testOrganizeImports("Syntax_Error_Body",
/*skipDestructiveCodeActions*/ false,
{
path: "/test.ts",
content: `
import { F1, F2 } from "lib";
import * as NS from "lib";
import D from "lib";
class class class;
D;
`,
},
libFile);
testOrganizeImports("Syntax_Error_Imports",
/*skipDestructiveCodeActions*/ false,
{
path: "/test.ts",
content: `
import { F1, F2 class class class; } from "lib";
import * as NS from "lib";
class class class;
import D from "lib";
D;
`,
},
libFile);
});
it("doesn't return any changes when the text would be identical", () => {
const testFile = {
path: "/a.ts",
@@ -388,6 +455,7 @@ D();
});
testOrganizeImports("Unused_All",
/*skipDestructiveCodeActions*/ false,
{
path: "/test.ts",
content: `
@@ -411,6 +479,7 @@ import { } from "lib";
});
testOrganizeImports("Unused_false_positive_module_augmentation",
/*skipDestructiveCodeActions*/ false,
{
path: "/test.d.ts",
content: `
@@ -426,6 +495,7 @@ declare module 'caseless' {
});
testOrganizeImports("Unused_preserve_imports_for_module_augmentation_in_non_declaration_file",
/*skipDestructiveCodeActions*/ false,
{
path: "/test.ts",
content: `
@@ -467,6 +537,7 @@ export { x };
});
testOrganizeImports("MoveToTop",
/*skipDestructiveCodeActions*/ false,
{
path: "/test.ts",
content: `
@@ -483,6 +554,7 @@ D();
/* eslint-disable no-template-curly-in-string */
testOrganizeImports("MoveToTop_Invalid",
/*skipDestructiveCodeActions*/ false,
{
path: "/test.ts",
content: `
@@ -501,6 +573,7 @@ D();
/* eslint-enable no-template-curly-in-string */
testOrganizeImports("TypeOnly",
/*skipDestructiveCodeActions*/ false,
{
path: "/test.ts",
content: `
@@ -513,6 +586,7 @@ export { A, B, X, Y, Z };`
});
testOrganizeImports("CoalesceMultipleModules",
/*skipDestructiveCodeActions*/ false,
{
path: "/test.ts",
content: `
@@ -527,6 +601,7 @@ a + b + c + d;
{ path: "/lib2.ts", content: "export const a = 3, c = 4;" });
testOrganizeImports("CoalesceTrivia",
/*skipDestructiveCodeActions*/ false,
{
path: "/test.ts",
content: `
@@ -540,6 +615,7 @@ F2();
libFile);
testOrganizeImports("SortTrivia",
/*skipDestructiveCodeActions*/ false,
{
path: "/test.ts",
content: `
@@ -551,6 +627,7 @@ F2();
{ path: "/lib2.ts", content: "" });
testOrganizeImports("UnusedTrivia1",
/*skipDestructiveCodeActions*/ false,
{
path: "/test.ts",
content: `
@@ -560,6 +637,7 @@ F2();
libFile);
testOrganizeImports("UnusedTrivia2",
/*skipDestructiveCodeActions*/ false,
{
path: "/test.ts",
content: `
@@ -571,6 +649,7 @@ F1();
libFile);
testOrganizeImports("UnusedHeaderComment",
/*skipDestructiveCodeActions*/ false,
{
path: "/test.ts",
content: `
@@ -581,6 +660,7 @@ import { F1 } from "lib";
libFile);
testOrganizeImports("SortHeaderComment",
/*skipDestructiveCodeActions*/ false,
{
path: "/test.ts",
content: `
@@ -593,6 +673,7 @@ import "lib1";
{ path: "/lib2.ts", content: "" });
testOrganizeImports("SortComments",
/*skipDestructiveCodeActions*/ false,
{
path: "/test.ts",
content: `
@@ -609,6 +690,7 @@ import "lib1";
{ path: "/lib3.ts", content: "" });
testOrganizeImports("AmbientModule",
/*skipDestructiveCodeActions*/ false,
{
path: "/test.ts",
content: `
@@ -624,6 +706,7 @@ declare module "mod" {
libFile);
testOrganizeImports("TopLevelAndAmbientModule",
/*skipDestructiveCodeActions*/ false,
{
path: "/test.ts",
content: `
@@ -646,6 +729,7 @@ D();
libFile);
testOrganizeImports("JsxFactoryUsedJsx",
/*skipDestructiveCodeActions*/ false,
{
path: "/test.jsx",
content: `
@@ -657,6 +741,7 @@ import { React, Other } from "react";
reactLibFile);
testOrganizeImports("JsxFactoryUsedJs",
/*skipDestructiveCodeActions*/ false,
{
path: "/test.js",
content: `
@@ -668,6 +753,7 @@ import { React, Other } from "react";
reactLibFile);
testOrganizeImports("JsxFactoryUsedTsx",
/*skipDestructiveCodeActions*/ false,
{
path: "/test.tsx",
content: `
@@ -681,6 +767,7 @@ import { React, Other } from "react";
// TS files are not JSX contexts, so the parser does not treat
// `<div/>` as a JSX element.
testOrganizeImports("JsxFactoryUsedTs",
/*skipDestructiveCodeActions*/ false,
{
path: "/test.ts",
content: `
@@ -692,6 +779,7 @@ import { React, Other } from "react";
reactLibFile);
testOrganizeImports("JsxFactoryUnusedJsx",
/*skipDestructiveCodeActions*/ false,
{
path: "/test.jsx",
content: `
@@ -703,6 +791,7 @@ import { React, Other } from "react";
// Note: Since the file extension does not end with "x", the jsx compiler option
// will not be enabled. The import should be retained regardless.
testOrganizeImports("JsxFactoryUnusedJs",
/*skipDestructiveCodeActions*/ false,
{
path: "/test.js",
content: `
@@ -712,6 +801,7 @@ import { React, Other } from "react";
reactLibFile);
testOrganizeImports("JsxFactoryUnusedTsx",
/*skipDestructiveCodeActions*/ false,
{
path: "/test.tsx",
content: `
@@ -721,6 +811,7 @@ import { React, Other } from "react";
reactLibFile);
testOrganizeImports("JsxFactoryUnusedTs",
/*skipDestructiveCodeActions*/ false,
{
path: "/test.ts",
content: `
@@ -730,6 +821,7 @@ import { React, Other } from "react";
reactLibFile);
testOrganizeImports("JsxPragmaTsx",
/*skipDestructiveCodeActions*/ false,
{
path: "/test.tsx",
content: `/** @jsx jsx */
@@ -758,6 +850,7 @@ export namespace React {
);
testOrganizeImports("JsxFragmentPragmaTsx",
/*skipDestructiveCodeActions*/ false,
{
path: "/test.tsx",
content: `/** @jsx h */
@@ -920,17 +1013,17 @@ export * from "lib";
});
function testOrganizeExports(testName: string, testFile: TestFSWithWatch.File, ...otherFiles: TestFSWithWatch.File[]) {
testOrganizeImports(`${testName}.exports`, testFile, ...otherFiles);
testOrganizeImports(`${testName}.exports`, /*skipDestructiveCodeActions*/ true, testFile, ...otherFiles);
}
function testOrganizeImports(testName: string, testFile: TestFSWithWatch.File, ...otherFiles: TestFSWithWatch.File[]) {
it(testName, () => runBaseline(`organizeImports/${testName}.ts`, testFile, ...otherFiles));
function testOrganizeImports(testName: string, skipDestructiveCodeActions: boolean, testFile: TestFSWithWatch.File, ...otherFiles: TestFSWithWatch.File[]) {
it(testName, () => runBaseline(`organizeImports/${testName}.ts`, skipDestructiveCodeActions, testFile, ...otherFiles));
}
function runBaseline(baselinePath: string, testFile: TestFSWithWatch.File, ...otherFiles: TestFSWithWatch.File[]) {
function runBaseline(baselinePath: string, skipDestructiveCodeActions: boolean, testFile: TestFSWithWatch.File, ...otherFiles: TestFSWithWatch.File[]) {
const { path: testPath, content: testContent } = testFile;
const languageService = makeLanguageService(testFile, ...otherFiles);
const changes = languageService.organizeImports({ type: "file", fileName: testPath }, testFormatSettings, emptyOptions);
const changes = languageService.organizeImports({ skipDestructiveCodeActions, type: "file", fileName: testPath }, testFormatSettings, emptyOptions);
assert.equal(changes.length, 1);
assert.equal(changes[0].fileName, testPath);