Skip to content
LogoLogo

unused-exports

Category: nursery. Default: off. Fixable: yes. Since: 0.1.0.

What it does

Reports exports that no other module imports. The rule also reports:

  • members of enums and namespaces that no code reads
  • bindings that a module exports under two names

Why is this bad?

An unused export is dead code, and it keeps the code that it uses alive. It also hides the real interface of a module.

How it works

Each static import marks the names that it uses. For example import { a }, ns.a of a namespace import, and E.X of an enum. A mark travels through export { a } from and export * to the module that defines the name. Any other use counts as use of everything. Examples are a require, a dynamic import(), and a namespace that code passes around as a value.

The rule does not report the exports of entry points, because they are public API. The entry points are:

  • modules that no other module imports
  • the files that package.json lists as entry points
  • the files that the tools you use load by convention
  • the modules that the entries option lists

Options

  • production: leave tests out. An export that only a test uses is then a violation. oxcruise --production enables it.
  • test: globs of test modules. These globs add to *.test.*, *.spec.* and test directories.
  • entries: globs of more public modules.
  • conventions: false, or a list of names, to use only some tool conventions.
  • includeEntryExports: also check the exports of entry points.
  • ignoreExportsUsedInFile: skip an export that its own file uses.
  • types: set to false to skip unused types and interfaces.
  • members: set to false to skip unused members of enums and namespaces.
  • ignore: globs of modules to skip.

To suppress one export, put // oxcruise-ignore-next-line unused-exports above it.

Fix

This rule is fixable. oxcruise --fix removes the export keyword, or removes the name from an export list.

Configure the rule

{
  "rules": {
    "unused-exports": "error"
  }
}

To set options, use an object: the level, then the options of the rule.