Package detail

babel-plugin-remove-unused-vars

steelbrain17.8kMIT2.2.0

Babel plugin to remove unused variables and imports (Babel autofix for ESLint's no-unused-vars)

babel, eslint, fix, unused

readme

Babel Plugin Remove Unused Vars

Babel plugin to remove unused variables and imports (Babel autofix for ESLint's no-unused-vars).

Installation

npm install --save-dev babel-plugin-remove-unused-vars

Usage

npx babel --no-babelrc --retain-lines --plugins babel-plugin-remove-unused-vars --out-dir src-2/ src/

This plugin is intended for source-to-source conversions. You can then replace original source with output source, and have a quick look over at git diff to see what was changed and revert any unwanted changes.

Features

Removes

  • Unused imports
  • Unused function arguments
  • Unused results of function calls (ie. requires, or any others.)
  • Unused variables (even destructured ones)

Caveats

This plugin is NOT perfect. It'll have false-positives and remove used variables or sometimes keep unused variables around. It's highly rare but still something you have to keep in mind, if you experience this behavior please open a bug report with steps to reproduce.

One case that has been intentionally not handled for code simplicity and maintainer sanity is deep assignment expressions, this plugin retains the right side statement if it's an assignment and is used and left side is unused, but if the assignment is somewhere deep inside, it'll treat it as a normal expression and not keep it around if left side is unused.

Explanation by example:

const a = b = something();
// ^ If a is unused, output:
b = something();
const a = something(b = jing());
// ^ If a is unused, output:
// - nothing.

It's a rare case, and a highly discouraged practice by most linters so support for it will not be added.

License

This project is licensed under the terms of MIT License. See the LICENSE file for more info.

changelog

v2.2.0

  • Improvements for Typescript support
  • Improvements in handling computed values
  • Does not remove import statement if only default specifier is unused

v2.1.1

  • Fix support for JSX Elements
  • Fix for variable object access
  • Fix variable tracking in await/try-catch statements
  • Handle unused destructured parameters in functions properly
  • Fix handling of some function parameters/body variables

v2.1.0

  • Improve handling of member expressions in assignment expressions

v2.0.0

  • Rewrite, behavior differs and more cases are handled now

v1.0.1

  • Mark refs in arrow function assignment expressions correctly

v1.0.0

  • Initial release