Skip to content

TypeScript Organize Imports inserts extra newline #36102

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

Closed
xROAL opened this issue Jan 9, 2020 · 15 comments · Fixed by #36911
Closed

TypeScript Organize Imports inserts extra newline #36102

xROAL opened this issue Jan 9, 2020 · 15 comments · Fixed by #36911
Assignees
Labels
Bug A bug in TypeScript Domain: Organize Imports Issues with the organize imports feature Good First Issue Well scoped, documented and has the green light Help Wanted You can do this

Comments

@xROAL
Copy link

xROAL commented Jan 9, 2020

TS Template added by @mjbvz

TypeScript Version: 3.8.0-dev.20200108

Search Terms

  • Organize imports

In TypeScript / JavaScript, the command Organize Imports inserts up to two newlines if export { ... }; is at the end of a file. This does not happen with other forms of export.

  • VSCode Version: 1.41.1
  • OS Version: Windows 10 (1803)

Steps to Reproduce:

  1. Have a list / renamed export as the last line of your file, e.g.:
import { Router } from 'express';
const router = Router();
// ... other code
export { router as namedRouter }; // also happens with `export { router };`
  1. Run the Organize Imports command. It will insert a newline after the export { ... };
  2. Run the Organize Imports command again. It will insert another newline, resulting in two empty lines at the end of the file.

Expected behavior
Only one newline is inserted / kept after the export { ... };.

Does this issue occur when all extensions are disabled?: Yes

@vscodebot vscodebot bot assigned mjbvz Jan 9, 2020
@mjbvz mjbvz transferred this issue from microsoft/vscode Jan 9, 2020
@mjbvz mjbvz removed their assignment Jan 9, 2020
@DanielRosenwasser DanielRosenwasser added Bug A bug in TypeScript Domain: Organize Imports Issues with the organize imports feature Good First Issue Well scoped, documented and has the green light Help Wanted You can do this labels Jan 15, 2020
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 3.9.0 milestone Jan 15, 2020
@Kinrany
Copy link

Kinrany commented Jan 25, 2020

Should this be in 3.8?

@captain-yossarian
Copy link

captain-yossarian commented Feb 9, 2020

@DanielRosenwasser @elibarzilay Can I pick it up?

@DanielRosenwasser
Copy link
Member

Yes

@DanielRosenwasser
Copy link
Member

(Unless @elibarzilay has already started, but I assume you can go ahead)

@captain-yossarian
Copy link

@DanielRosenwasser if @elibarzilay has already started then I will only follow this. Anyway I don't know where to start

@Kinrany
Copy link

Kinrany commented Feb 10, 2020

I believe you'll need to fix coalesceExports in src/services/organizeImports.ts. Aside from tests, that's the only file that shows up in search.

@captain-yossarian
Copy link

@Kinrany Thank you

@captain-yossarian
Copy link

@DanielRosenwasser @elibarzilay @Kinrany
I'm able to reproduce the bug, but I'm unable to see any of my logs.
I've tried:

  • link typescript local build to my project (npm link)
  • change "typescript.tsdk":"D:/js/typescript/TypeScript-master/lib"
  • add logs directly into node_modules source code (without any links)
  • to use console.log and logger.log
    it did not help :/

How should I do it right?

@elibarzilay
Copy link
Contributor

@captain-yossarian I'm new so little experience with trying to run things live, instead I start with a test case for the problem and then use the debugger.

@ahtee
Copy link

ahtee commented Feb 11, 2020

Do you have any auto formatters installed currently? Something like prettier may be adding the new line.

sacgrover added a commit to sacgrover/TypeScript that referenced this issue Feb 20, 2020
Organize Imports was inserting extra newline.

Fixes microsoft#36102
sacgrover added a commit to sacgrover/TypeScript that referenced this issue Feb 20, 2020
Organize Imports was inserting extra newline.

Fixes microsoft#36102
sacgrover added a commit to sacgrover/TypeScript that referenced this issue Mar 4, 2020
Organize Imports was inserting extra newline.

Fixes microsoft#36102
elibarzilay pushed a commit to sacgrover/TypeScript that referenced this issue Mar 12, 2020
Organize Imports was inserting extra newline.

Fixes microsoft#36102
elibarzilay pushed a commit that referenced this issue Mar 12, 2020
Organize Imports was inserting extra newline.

Fixes #36102
@RudeySH
Copy link

RudeySH commented Nov 18, 2021

If someone out there is still having this issue, try disabling the "EditorConfig for VS Code" extension or removing insert_final_newline = true rule from .editorconfig.

@MM1132
Copy link

MM1132 commented Jul 17, 2023

I did set "source.insertFinalNewline": false. Nothing changed.
"source.organizeImports": true still inserts a newline at the end of imports.

@RudeySH
Copy link

RudeySH commented Jul 17, 2023

@MM1132 This issue is about a problem where VS Code inserts two newlines at the end of imports, which can be solved by disabling insertFinalNewline. This solution will not make VS Code stop inserting a single newline at the end of imports, it just fixes the duplicate newline problem.

@MM1132
Copy link

MM1132 commented Jul 17, 2023

@RudeySH insertFinalNewline changes absolutely nothing. For now, disabling source.organizeImports is the only thing that makes VsCode not insert double newline after imports.

@bene-we
Copy link

bene-we commented Sep 26, 2023

I didn't test it extensively, but for me adding the following to settings.json worked so far:

"files.insertFinalNewline": true,
"files.trimFinalNewlines": true,

With that, multiple newlines at the end of a file are being trimmed to only have a single newline, and it seems that this setting runs last.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Organize Imports Issues with the organize imports feature Good First Issue Well scoped, documented and has the green light Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants