question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Delete all unused declarations" on TS imports removes code in files

See original GitHub issue
  • VSCode Version: 1.31.1
  • OS Version: Windows 10 (not relevant)

Steps to Reproduce:

I feel that something is terribly wrong with the current behaviour of “Delete all unused declarations” codefix.

It is suggested to remove an unused import. In this context, its implicit meaning is (for me) something like "Delete all unused imports ".

But the actual behaviour can be quite dangerous, as it can remove actual code in the file body (which is definitely not something I would expect when refactoring dependencies):

anim

We experienced this in my team recently (and deleted parts of code that should not have been deleted), and I dont understand how this can be the default behaviour.

NB: I’m not suggesting to fix the “Delete all unused declaration” codefix, rather to use another codefix when refactoring the depenency context… so I was not sure if this issue has to be posted on the Typescript repository, or here.

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

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:8
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
two-bridgescommented, Nov 6, 2019

The feature is working as currently designed

So, I’m literally staring at code changes staged for production and testing reveals nothing is saving to the database… Turns out we used ‘Delete Unused Declarations’ to clean up some code… then I googled, then I found this thread.

2reactions
hbenlcommented, Apr 4, 2019

The feature is working as currently designed.

Seriously? I would not expect an automatic refactoring method to break my code quite so brazenly. Nor can I imagine that anyone else would expect that. I would expect an automatic refactoring to always try to be on the safe side.

i.e. let unusedVar = someMethod() should be transformed to someMethod().

That’s exactly what I would have expected.

So please change this behavior. The refactoring methods in Typescript are incredibly useful for cleaning up old javascript code (and there’s a lot of that out there), but I have to be able to rely on them to be reasonably safe.

Note that the other issue discussed here (separating the removal of all unused imports from the removal of all unused declarations) is also a very good idea, but totally independent of the issue of the removed code (and won’t fix that issue).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Automatically Remove All Unused Imports in a TypeScript ...
The following command will traverse, recursively through all *.ts files in the project and remove the unused imports. BE CAREFUL!
Read more >
How do I delete all unused imports in the active file ...
Current workaround is: click on unused import, CMD + . > Delete unused imports . enter image description here. Languages for which this...
Read more >
How to Remove Unused Imports and Declarations in Typescript
A simple guide that demonstrates how to remove unused imports and declarations in a typescript project using eslint.json.
Read more >
Remove Unused Imports - Visual Studio Marketplace
VS Code extension to remove unused ES6 imports inside JavaScript and TypeScript files ( .js , .jsx , .ts and .tsx extensions) without ......
Read more >
Easily sort imports and remove unused declarations on ...
... VS Code will automatically on each file save: run code formatting with default formatter; sort imports; remove all unused declarations ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found