Skip to content

Commit 24c8270

Browse files
committed
prevented organizeImports appending an extra new line to declarations at end of a file
fixes microsoft#48126
1 parent 5d70bf8 commit 24c8270

File tree

7 files changed

+20
-18
lines changed

7 files changed

+20
-18
lines changed

src/services/textChanges.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -1275,7 +1275,14 @@ namespace changesToText {
12751275
const targetSourceFile = c.kind === ChangeKind.ReplaceWithSingleNode ? getSourceFileOfNode(getOriginalNode(c.node)) ?? c.sourceFile :
12761276
c.kind === ChangeKind.ReplaceWithMultipleNodes ? getSourceFileOfNode(getOriginalNode(c.nodes[0])) ?? c.sourceFile :
12771277
c.sourceFile;
1278-
const newText = computeNewText(c, targetSourceFile, sourceFile, newLineCharacter, formatContext, validate);
1278+
let newText = computeNewText(c, targetSourceFile, sourceFile, newLineCharacter, formatContext, validate);
1279+
1280+
// prevent appending an extra new line to declarations at end of a file
1281+
const spanTextEnd = span.start + newText.length - newLineCharacter.length;
1282+
if (c.kind === ChangeKind.ReplaceWithMultipleNodes && spanTextEnd === targetSourceFile.text.length && endsWith(newText, newLineCharacter)) {
1283+
newText = newText.substring(0, newText.length - newLineCharacter.length);
1284+
}
1285+
12791286
// Filter out redundant changes.
12801287
if (span.length === newText.length && stringContainsAt(targetSourceFile.text, newText, span.start)) {
12811288
return undefined;

tests/baselines/reference/organizeImports/TypeOnly.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ import type Y from "lib";
1212
import type { A, B } from "lib";
1313
import { X, Z } from "lib";
1414

15-
export { A, B, X, Y, Z };
15+
export { A, B, X, Y, Z };

tests/cases/fourslash/organizeImports12.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
verify.organizeImports(
1010
`declare export default class A {}
1111
declare export * from "foo";
12-
declare export { a, b };
13-
`);
12+
declare export { a, b };`
13+
);

tests/cases/fourslash/organizeImports17.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66
verify.organizeImports(
77
`import { case, Insensitively, sorted } from "aardvark";
8-
import { Both } from "module-specifiers-unsorted";
9-
`,
8+
import { Both } from "module-specifiers-unsorted";`,
109
ts.OrganizeImportsMode.SortAndCombine,
1110
{
1211
organizeImportsIgnoreCase: "auto",

tests/cases/fourslash/organizeImports18.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ export { C } from "./C";
2929
3030
export { bFuncA } from "./A";
3131
export { bFuncB } from "./B";
32-
export { bFuncC } from "./C";
33-
`);
32+
export { bFuncC } from "./C";`
33+
);

tests/cases/fourslash/organizeImports19.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ const b = 1;
1717
export { b };
1818
1919
const c = 1;
20-
export { c };
21-
`);
20+
export { c };`
21+
);

tests/cases/fourslash/organizeImportsType2.ts

+4-8
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,14 @@ verify.organizeImports(
1515
`type A = string;
1616
type B = string;
1717
const C = "hello";
18-
export { A, C, type B };
19-
`
18+
export { A, C, type B };`
2019
);
2120

2221
verify.organizeImports(
2322
`type A = string;
2423
type B = string;
2524
const C = "hello";
26-
export { A, type B, C };
27-
`,
25+
export { A, type B, C };`,
2826
undefined,
2927
{ organizeImportsTypeOrder : "inline" }
3028
);
@@ -33,8 +31,7 @@ verify.organizeImports(
3331
`type A = string;
3432
type B = string;
3533
const C = "hello";
36-
export { type B, A, C };
37-
`,
34+
export { type B, A, C };`,
3835
undefined,
3936
{ organizeImportsTypeOrder : "first" }
4037
);
@@ -43,8 +40,7 @@ verify.organizeImports(
4340
`type A = string;
4441
type B = string;
4542
const C = "hello";
46-
export { A, C, type B };
47-
`,
43+
export { A, C, type B };`,
4844
undefined,
4945
{ organizeImportsTypeOrder : "last" }
5046
);

0 commit comments

Comments
 (0)