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.

Similar to methods organizeImports() , formatText(), fixMissingImports() I would like to have a method removeUnused() that removes any variable, parameter, named import, type parameter, method, etc. that is not used.

    /**
     *  Removes used declarations like variables, parameters, members, etc. from this node descendants.
     *
     * @param formatSettings - Format code settings.
     * @param userPreferences - User preferences for refactoring.
     */
    removeUnused(formatSettings: FormatCodeSettings = {}, userPreferences: UserPreferences = {}) {}

Alternatives The method could accept some options to control which kind of nodes should be removed or not. Also if we decide to put the method in SourceFile instead of Node, it could accept a targetnode option:

type RemoveUnusedExclude = 'variable'|'parameter'|'typeParameter'|'import'|'function'|'class'|'type'|'method'|'property'|'constructor'|'etc'
interface RemoveUnusedOptions{
    exclude?: RemoveUnusedExclude[]
    targetNode?: Node
}
removeUnused(options?: RemoveUnusedOptions):void

Doubts

  • Similarly to formatText() this applies to any node, this method could be declared in Node although users will usually call it in SourceFile. So I’m not sure where would be the best location…

  • Should we use TS built in code fixes that already implements this? or should we implement it from scratch so Nodes are not forgotten ?

In some cases it could be tricky to define if something is used or not, like type parameters, exports, etc, and the implementation must be 100% safe, but I could try implementing something like that and see…

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
dsherretcommented, Mar 30, 2019

Or actually maybe just removeUnusedDeclarations.

0reactions
dsherretcommented, Apr 3, 2019

After thinking this through, I think what you did in #595 is sufficient. This is normally a refactor that people do at the end and so keeping track of the nodes is not very necessary.

By the way, here’s an example of calling this until there’s no more changes:

let lastWidth: number;
do {
    lastWidth = sourceFile.getFullWidth();
    sourceFile.fixUnusedIdentifiers();
} while (lastWidth !== sourceFile.getFullWidth());
Read more comments on GitHub >

github_iconTop Results From Across the Web

Delete unused apps and free up space - Files by Google Help
Scroll to “Delete unused apps” card and tap Select apps. Select the apps you want to uninstall. Tap Uninstall Delete . On the...
Read more >
Remove unused code - web.dev
Then remove unused and unnecessary libraries. Impact on Core Web Vitals #. By removing unused code, you can improve your website's Core Web ......
Read more >
Remove unused JavaScript - Chrome Developers
# Build tool for support for removing unused code. Check out the following Tooling.Report tests to find out if your bundler supports features ......
Read more >
Remove Unused CSS | UnusedCSS
UnusedCSS is an online tool to remove unused CSS rules. It will check your pages, find unused CSS and let you download a...
Read more >
Remove Unused References | ReSharper Documentation
Remove Unused References. Last modified: 22 December 2022. This command allows cleaning up project and assembly references that have no actual usages in ......
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