Skip to content

"Organize imports" eats comments #60431

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
mkantor opened this issue Nov 6, 2024 · 2 comments
Open

"Organize imports" eats comments #60431

mkantor opened this issue Nov 6, 2024 · 2 comments
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@mkantor
Copy link
Contributor

mkantor commented Nov 6, 2024

πŸ”Ž Search Terms

source.organizeImports, VSCode, "source action", semicolon, remove, change

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about organize imports

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAcgM4RAUxgC2AOwOYIBQA9MXAMaro4yEDcQA

πŸ’» Code

import 'something'
// comment
;

πŸ™ Actual behavior

When organizing inputs in the given file, the output is this:

import 'something';

πŸ™‚ Expected behavior

I expected the comment to be preserved.

Additional information about the issue

The semicolon is important here. Without it the comment is preserved.

#48126 is perhaps tangentially related.

@mkantor
Copy link
Contributor Author

mkantor commented Nov 6, 2024

The given example might seem like silly code to write, so here's some background:

I have the "Organize Imports" action and Prettier configured to run on save in VSCode. I was debugging something in a file with many imports and was commenting/uncommenting large blocks of code, but kept annoyingly losing imports after commenting stuff out, which meant I had to manually bring them back when uncommenting.

As a quick workaround I wrote this at the top of the file:

import { a, b, c, d } from './something.js'

// TODO: remove this; it's here to preserve imports
[a, b, c, d]

Which Prettier turns into this (because I use "semi": false):

import { a, b, c, d } from './something.js'

// TODO: remove this; it's here to preserve imports
;[a, b, c, d]

Which Organize Imports turns into this:

import { a, b, c, d } from './something.js';
[a, b, c, d]

What's extra annoying about this is that Prettier runs after Organize Imports, so what actually happens is the first time I save the file I just get the semicolon added, and then the second time I save the file (without making any changes) the comment vanishes.

If this bug realistically only affects people with this specific setup then I can understand if it's not prioritized, but it seemed worth opening an issue in any case.

@mkantor
Copy link
Contributor Author

mkantor commented Nov 6, 2024

Here's some more-plausibly-useful code which exhibits the same problem:

import { firstTask, secondTask, thirdTask } from './tasks.js'

// run all the tasks
;[firstTask, secondTask, thirdTask].forEach(task => task())

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this labels Nov 6, 2024
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

2 participants