Skip to content

prevented organizeImports appending an extra new line to declarations when end of a file #58986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/services/textChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1275,7 +1275,15 @@ namespace changesToText {
const targetSourceFile = c.kind === ChangeKind.ReplaceWithSingleNode ? getSourceFileOfNode(getOriginalNode(c.node)) ?? c.sourceFile :
c.kind === ChangeKind.ReplaceWithMultipleNodes ? getSourceFileOfNode(getOriginalNode(c.nodes[0])) ?? c.sourceFile :
c.sourceFile;
const newText = computeNewText(c, targetSourceFile, sourceFile, newLineCharacter, formatContext, validate);
let newText = computeNewText(c, targetSourceFile, sourceFile, newLineCharacter, formatContext, validate);

// prevent appending an extra new line to declarations at end of a file
const spanTextEnd = span.start + newText.length;
const isEndOfFile = span.length !== 0 && (spanTextEnd - newLineCharacter.length) === targetSourceFile.text.length;
if (c.kind === ChangeKind.ReplaceWithMultipleNodes && isEndOfFile && endsWith(newText, newLineCharacter)) {
newText = newText.substring(0, newText.length - newLineCharacter.length);
}

// Filter out redundant changes.
if (span.length === newText.length && stringContainsAt(targetSourceFile.text, newText, span.start)) {
return undefined;
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/organizeImports/TypeOnly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ import type Y from "lib";
import type { A, B } from "lib";
import { X, Z } from "lib";

export { A, B, X, Y, Z };
export { A, B, X, Y, Z };
4 changes: 2 additions & 2 deletions tests/cases/fourslash/organizeImports12.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
verify.organizeImports(
`declare export default class A {}
declare export * from "foo";
declare export { a, b };
`);
declare export { a, b };`
);
3 changes: 1 addition & 2 deletions tests/cases/fourslash/organizeImports17.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

verify.organizeImports(
`import { case, Insensitively, sorted } from "aardvark";
import { Both } from "module-specifiers-unsorted";
`,
import { Both } from "module-specifiers-unsorted";`,
ts.OrganizeImportsMode.SortAndCombine,
{
organizeImportsIgnoreCase: "auto",
Expand Down
4 changes: 2 additions & 2 deletions tests/cases/fourslash/organizeImports18.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ export { C } from "./C";

export { bFuncA } from "./A";
export { bFuncB } from "./B";
export { bFuncC } from "./C";
`);
export { bFuncC } from "./C";`
);
4 changes: 2 additions & 2 deletions tests/cases/fourslash/organizeImports19.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ const b = 1;
export { b };

const c = 1;
export { c };
`);
export { c };`
);
9 changes: 9 additions & 0 deletions tests/cases/fourslash/organizeImports24.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// <reference path='fourslash.ts' />

//// export * from './file2.js';
//// export * from './file1.js';

verify.organizeImports(
`export * from './file1.js';
export * from './file2.js';`
);
15 changes: 15 additions & 0 deletions tests/cases/fourslash/organizeImports25.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// <reference path='fourslash.ts' />

//// export * from './file4.js';
//// export * from './file3.js';
////
//// export * from './file2.js';
//// export * from './file1.js';

verify.organizeImports(
`export * from './file3.js';
export * from './file4.js';

export * from './file1.js';
export * from './file2.js';`
);
11 changes: 11 additions & 0 deletions tests/cases/fourslash/organizeImports26.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// <reference path='fourslash.ts' />

//// export * from './file1.js';
//// export * from './file2.js';
////

verify.organizeImports(
`export * from './file1.js';
export * from './file2.js';
`
);
12 changes: 4 additions & 8 deletions tests/cases/fourslash/organizeImportsType2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@ verify.organizeImports(
`type A = string;
type B = string;
const C = "hello";
export { A, C, type B };
`
export { A, C, type B };`
);

verify.organizeImports(
`type A = string;
type B = string;
const C = "hello";
export { A, type B, C };
`,
export { A, type B, C };`,
undefined,
{ organizeImportsTypeOrder : "inline" }
);
Expand All @@ -33,8 +31,7 @@ verify.organizeImports(
`type A = string;
type B = string;
const C = "hello";
export { type B, A, C };
`,
export { type B, A, C };`,
undefined,
{ organizeImportsTypeOrder : "first" }
);
Expand All @@ -43,8 +40,7 @@ verify.organizeImports(
`type A = string;
type B = string;
const C = "hello";
export { A, C, type B };
`,
export { A, C, type B };`,
undefined,
{ organizeImportsTypeOrder : "last" }
);