Are there any good tools for static code analysis in typescript?

15.1k views Asked by At

We have been searching for good tools for measuring the quality of our TypeScript code. Mainly, we are interested in measuring Cyclomatic Complexity, LCOM, Instability and similar metrics. A tool for visualizing dependencies between modules would also be nice.

A sort of "NDepend for typescript" would be great - but we haven't been able to find anything like it...

Can anyone point us in the direction of any tools that provide some such functionality?

3

There are 3 answers

0
Fenton On

If you were to use an existing JavaScript analyser, such as JSHint (or any similar alternative if it doesn't have all the metrics you want), the results would not be far wrong for your TypeScript code. TypeScript makes very few code transformations, so you would find minor differences in the following areas:

  • If you use inheritance, there will be one additional method in your program.
  • If you use rest parameters, the mapping to an array is added to each method with rest parameters.
  • Enums generate into more complex JavaScript code.

But none of these would throw off the analysis by a great margin.

Sample metrics from the JSHint site:

Metrics

There is only one function in this file.

It takes no arguments.

This function contains only one statement.

Cyclomatic complexity number for this function is 1.

0
basarat On

We have been searching for good tools for measuring the quality of our TypeScript code

Similar to JSHint (mentioned by Steve) for TypeScript there is TSLint https://github.com/palantir/tslint (with a grunt plugin https://github.com/palantir/grunt-tslint)

The rules aren't that complex to create yourself based on TypeScript language service ASTWalker mechanism e.g. Indent rule : https://github.com/palantir/tslint/blob/master/src/rules/indentRule.ts

0
xmojmr On

For visualizing TypeScript module dependencies you can use JavaScript module dependency visualizers:

  • Kenneth Auchenberg's dependo. Creates single-file SPA with with interactive graph
  • Patrik Henningsson's MaDGe. Creates report and static image using Graphviz
  • ...