GithubHelp home page GithubHelp logo

infctr / ts-unused-exports Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pzavolinsky/ts-unused-exports

0.0 1.0 0.0 115 KB

ts-unused-exports finds unused exported symbols in your Typescript project

License: MIT License

JavaScript 22.62% TypeScript 77.38%

ts-unused-exports's Introduction

ts-unused-exports

ts-unused-exports finds unused exported symbols in your Typescript project.

Installation

npm install --save-dev ts-unused-exports

or, to install globally:

npm install -g ts-unused-exports

Usage

./node_modules/.bin/ts-unused-exports path/to/tsconfig.json [file1.ts ...]

or, if installed globally:

ts-unused-exports path/to/tsconfig.json [file1.ts ...]

or:

import analyzeTsConfig from 'ts-unused-exports';
const result = analyzeTsConfig('path/to/tsconfig.json');
// or const result = analyzeTsConfig('path/to/tsconfig.json', ['file1.ts']);

// result : { [index:string] : string[] }
// where the keys are file paths and the values are unused symbols

Note that if ts-unused-exports is called without files, the files will be read from the tsconfig's files or include key which must be present. If called with files, then those file paths should be relative to the tsconfig.json, just like you would specifie them in your tsconfig's files key.

ts-unused-exports also resolves path aliases specified in tsconfig's paths object.

Why should I use this?

If you've ever used tslint's no-unused-variable rule you already known how awesome it is. What this rule does is detect code in your modules that is not being used so that you can remove it.

For example, say that you refactored your math.ts module so that you no longer use add1:

function add1(x:number) { return x + 1; } // warning here

export default (x:number) => x + 1;

When run, tslint will complain that add1 is no longer in use.

Unfortunately, if your symbols are exported, tslint does not complain anymore. Effectively exporting a symbol anchors the symbol so that, even if nobody uses it, it will not be marked as dead code.

If you've ever found yourself mid-refactor fixing a particularly fiendish function only to realize later that nobody really uses it you know exactly what I mean.

ts-unused-exports fills this cross-module gap by complaining about exported symbols that no-one cares about.

In this sense, ts-unused-exports does not replace tslint but rather complements it by helping you detect unnecessary exports. Once those are fixed, tslint's no-unused-variable rule will kick in and tell you which code you can safely remove.

Example

There is a (very silly) example in the example/ directory.

If you want to run it you can:

git clone https://github.com/pzavolinsky/ts-unused-exports
cd ts-unused-exports
./bin/ts-unused-exports example/tsconfig.json
# or: node ./bin/ts-unused-exports example/tsconfig.json
# or: node bin\ts-unused-exports example\tsconfig.json

The output should be:

1 module with unused exports
math: add1

Also note the exit status (which equals the number of offending modules):

echo $?
# or: echo %ERRORLEVEL%
1

If not using files or include inside your tsconfig (e.g. using webpack with ts-loader), you can explicitly specify the files to check in the command line:

./bin/ts-unused-exports example/tsconfig.json app.ts math.ts

or, in a more generic way:

./bin/ts-unused-exports example/tsconfig.json $(cd example; find -name '*.ts')

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.