Skip to content

Feature Request: Typescript dead code cleanup #16939

@akashperfect

Description

@akashperfect

TypeScript Version: 2.4.0 / nightly (2.5.0-dev.201xxxxx)

Problem Statement
Unlike C# there is no utility for typescript to clean up the dead code. Here by dead code I mean the functions which are nowhere referenced or called anymore.

Programmers are mainly affected by this issue, like as an example I was cleaning up the old code after moving to different framework in our project. I saw it extremely cumbersome to remove existing files as just deleting them would cause some other classes public function and components to be left dangling with no references what so ever. Although I agree the compiler is optimized to include only used Js files in the bundle but for code search and maintainability it’s a major work.

Expected behavior:
Best way would be to delete all the obsolete files and run the utility to get all the dead functions formed as part of the files deletion. This will make everyone’s life much simpler as they wouldn’t have to be bothered about un-used functions.

Actual behavior:
Need to manually look for all the imports and need to delete those if not referenced anywhere else.

What didn't worked
From github issue someone mentions using noUnusedParameters and noUnusedLocals but these are only for if local variables or if any parameter is used or not. But there is no way to determine that in the entire project what all functions are never called.
Using static analysis from Visual Studio also didn't helped in this matter.

Proposed Change
Just a high level idea
Compiler would parse through all the code and maintain reference, so right after parsing(output from parser.ts) we can check what all functions are not referenced from any other part of the code in the entire project/solution. We can mark them as dead and good to be removed.
This could come either as part of an extension to run separately or a static analysis rule to be included in the build.

Example
Consider 2 files having some code of this type
file1.ts

export class file1 {
   public foo() {
      return;
   }
   // other functions
}

file.ts

import { file1 } from "./file1";
export class file {
   public bar() {
      var test = new file1();
      test.foo();
   }
}

Now suppose I no longer need file.ts, I will delete that and now since the function foo() was used only by function bar(), I should ideally delete foo() also from file1.ts. Now my point being that there is no such utility which achieves this today.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Out of ScopeThis idea sits outside of the TypeScript language design constraintsSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions