Skip to content

Commit

Permalink
organizeImports makes no changes if there are parse errors in the s…
Browse files Browse the repository at this point in the history
…ourceFile (#58903)
  • Loading branch information
iisaduan authored Jul 25, 2024
1 parent 941d154 commit 107a007
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 53 deletions.
3 changes: 2 additions & 1 deletion src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2674,8 +2674,9 @@ export function createLanguageService(
synchronizeHostData();
Debug.assert(args.type === "file");
const sourceFile = getValidSourceFile(args.fileName);
const formatContext = formatting.getFormatContext(formatOptions, host);
if (containsParseError(sourceFile)) return emptyArray;

const formatContext = formatting.getFormatContext(formatOptions, host);
const mode = args.mode ?? (args.skipDestructiveCodeActions ? OrganizeImportsMode.SortAndCombine : OrganizeImportsMode.All);
return OrganizeImports.organizeImports(sourceFile, formatContext, host, program, preferences, mode);
}
Expand Down
64 changes: 49 additions & 15 deletions src/testRunner/unittests/services/organizeImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,28 @@ console.log(A, B, a, b);`,
console.log(A, B, a, b);`,
});

testOrganizeImports("parseErrors", /*skipDestructiveCodeActions*/ false, {
path: "/test.js",
content: `declare module 'mod1' {
declare export type P = {|
|};
declare export type F = {|
...$Exact<Node>,
await?: Span,
|};
declare export type S = {|
|};
declare export type C = {|
|};
}
declare module 'mod2' {
import type {
U,
} from 'mod1';
}`,
});

testOrganizeImports("Renamed_used", /*skipDestructiveCodeActions*/ false, {
path: "/test.ts",
content: `
Expand Down Expand Up @@ -822,8 +844,7 @@ import { React, Other } from "react";
`,
}, reactLibFile);

// TS files are not JSX contexts, so the parser does not treat
// `<div/>` as a JSX element.
// 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: `
Expand Down Expand Up @@ -1049,19 +1070,32 @@ export * from "lib";
const { path: testPath, content: testContent } = testFile;
const languageService = makeLanguageService(testFile, ...otherFiles);
const changes = languageService.organizeImports({ skipDestructiveCodeActions, type: "file", fileName: testPath }, ts.testFormatSettings, ts.emptyOptions);
assert.equal(changes.length, 1);
assert.equal(changes[0].fileName, testPath);

const newText = ts.textChanges.applyChanges(testContent, changes[0].textChanges);
Harness.Baseline.runBaseline(
baselinePath,
[
"// ==ORIGINAL==",
testContent,
"// ==ORGANIZED==",
newText,
].join(newLineCharacter),
);

if (changes.length !== 0) {
assert.equal(changes.length, 1);
assert.equal(changes[0].fileName, testPath);

const newText = ts.textChanges.applyChanges(testContent, changes[0].textChanges);
Harness.Baseline.runBaseline(
baselinePath,
[
"// ==ORIGINAL==",
testContent,
"// ==ORGANIZED==",
newText,
].join(newLineCharacter),
);
}
else {
Harness.Baseline.runBaseline(
baselinePath,
[
"// ==ORIGINAL==",
"// ==NO CHANGES==",
testContent,
].join(newLineCharacter),
);
}
}

function testDetectionBaseline(testName: string, skipDestructiveCodeActions: boolean, testFile: File, ...otherFiles: File[]) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
// ==ORIGINAL==
// ==NO CHANGES==

import { React, Other } from "react";

<div/>;
// ==ORGANIZED==


<div/>;
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
// ==ORIGINAL==
// ==NO CHANGES==

import { F1, F2 } from "lib";
import * as NS from "lib";
import D from "lib";

class class class;
D;

// ==ORGANIZED==

import D from "lib";

class class class;
D;
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
// ==ORIGINAL==
// ==NO CHANGES==

import { F1, F2 } from "lib";
import * as NS from "lib";
import D from "lib";

class class class;
D;

// ==ORGANIZED==

import * as NS from "lib";
import D, { F1, F2 } from "lib";

class class class;
D;
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
// ==ORIGINAL==
// ==NO CHANGES==

import { F1, F2 class class class; } from "lib";
import * as NS from "lib";
class class class;
import D from "lib";

D;

// ==ORGANIZED==

import D from "lib";
class class class;

D;
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
// ==ORIGINAL==
// ==NO CHANGES==

import { F1, F2 class class class; } from "lib";
import * as NS from "lib";
class class class;
import D from "lib";

D;

// ==ORGANIZED==

import * as NS from "lib";
import D, { F1, F2, class, class, class } from "lib";
class class class;

D;
20 changes: 20 additions & 0 deletions tests/baselines/reference/organizeImports/parseErrors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// ==ORIGINAL==
// ==NO CHANGES==
declare module 'mod1' {
declare export type P = {|
|};
declare export type F = {|
...$Exact<Node>,
await?: Span,
|};
declare export type S = {|
|};
declare export type C = {|
|};
}

declare module 'mod2' {
import type {
U,
} from 'mod1';
}
4 changes: 2 additions & 2 deletions tests/cases/fourslash/organizeImports17.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/// <reference path="fourslash.ts" />

//// import { Both } from "module-specifiers-unsorted";
//// import { case, Insensitively, sorted } from "aardvark";
//// import { aa, CaseInsensitively, sorted } from "aardvark";

verify.organizeImports(
`import { case, Insensitively, sorted } from "aardvark";
`import { aa, CaseInsensitively, sorted } from "aardvark";
import { Both } from "module-specifiers-unsorted";
`,
ts.OrganizeImportsMode.SortAndCombine,
Expand Down

0 comments on commit 107a007

Please sign in to comment.