Skip to content

Commit f684b1d

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

File tree

9 files changed

+55
-18
lines changed

9 files changed

+55
-18
lines changed

src/services/textChanges.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -1275,7 +1275,15 @@ 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;
1282+
const isEndOfFile = span.length !== 0 && (spanTextEnd - newLineCharacter.length) === targetSourceFile.text.length;
1283+
if (c.kind === ChangeKind.ReplaceWithMultipleNodes && isEndOfFile && endsWith(newText, newLineCharacter)) {
1284+
newText = newText.substring(0, newText.length - newLineCharacter.length);
1285+
}
1286+
12791287
// Filter out redundant changes.
12801288
if (span.length === newText.length && stringContainsAt(targetSourceFile.text, newText, span.start)) {
12811289
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+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @allowSyntheticDefaultImports: true
4+
// @moduleResolution: node
5+
// @noUnusedLocals: true
6+
// @target: es2018
7+
8+
//// export * from './file2.js';
9+
//// export * from './file1.js';
10+
11+
verify.organizeImports(
12+
`export * from './file1.js';
13+
export * from './file2.js';`
14+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @allowSyntheticDefaultImports: true
4+
// @moduleResolution: node
5+
// @noUnusedLocals: true
6+
// @target: es2018
7+
8+
//// export * from './file4.js';
9+
//// export * from './file3.js';
10+
////
11+
//// export * from './file2.js';
12+
//// export * from './file1.js';
13+
14+
verify.organizeImports(
15+
`export * from './file3.js';
16+
export * from './file4.js';
17+
18+
export * from './file1.js';
19+
export * from './file2.js';`
20+
);

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)