Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-transform-prune-unreachable-imports

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-transform-prune-unreachable-imports

## Install

  • 0.0.0-alpha.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

babel-plugin-transform-prune-unused-imports

Install

Using npm:

npm install babel-plugin-transform-prune-unused-imports --save-dev

or using yarn:

yarn add babel-plugin-transform-prune-unused-imports --dev

Usage

Options

By default, only true and false identifiers are considered truthy/falsy.

  • falsyExpressions : Array<string> - Expressions (in addition to false) to be treated as falsy
  • truthyExpressions : Array<string> - Expressions (in addition to true) to be treated as truthy
  • ignoredModules : Array<string> - Packages/modules to ignore (i.e. will never be pruned by this plugin)

Examples

{
  "plugins": [
    [
      "transform-prune-unused-imports",
      {
        "falsyExpressions": ["process.env.NODE_ENV !=='production'"]
      }
    ]
  ]
}
{
  "plugins": [
    [
      "transform-prune-unused-imports",
      {
        "falsyExpressions": ["__NODE__"],
        "truthyExpressions": ["__BROWSER__"]
      }
    ]
  ]
}

Features

This plugin is able identify unused imports in the following scenarios:

Unreachable conditionals

import { unreachable, reachable } from "some-pkg";

if (false) {
  unreachable;
}

if (true) {
  reachable;
} else {
  unreachable;
}

true ? reachable : unreachable;

false ? unreachable : reachable;

Unreachable chained logical expressions

import { unreachable } from "some-pkg";

foo && false && bar && unreachable;

Variable shadowing

Variable scope is correctly handled.

// Before
import { unused } from "some-pkg";

function foo(unused) {
  unused;
}
// After
function foo(unused) {
  unused;
}

Caveats

While this plugin works for most use cases, the static analysis is performed by this plugin is ultimately limited and won't work in some scenarios.

Unused assignment expressions

Currently this plugin will not prune unused imports that are assigned to variables, even if those new variables are unused. For example:

import { unreachable } from "some-pkg";

const foo = unreachable;

if (false) {
  foo;
}

FAQs

Package last updated on 08 Apr 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc