ESLint Rule Composer

0.3.0 · maintenance · verified Sun Apr 19

ESLint Rule Composer is a utility library designed to facilitate the creation and modification of ESLint rules by composing them from existing ones. Currently at version 0.3.0, it allows developers to `filterReports`, `mapReports`, or `joinReports` from one or more base rules, enabling highly customized linting behavior without needing to rewrite entire rule definitions. This approach is particularly useful for adding exceptions to existing rules (e.g., ignoring specific patterns in `no-unused-expressions`) or combining the logic of multiple rules. Its primary differentiator is the programmatic manipulation of reported problems and rule definitions, offering a flexible layer over ESLint's core rule API. Given its 0.x.y version, API stability might still be evolving, and its release cadence is likely slow or on-demand, as the last update on NPM was in April 2018.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to create a custom ESLint rule using `eslint-rule-composer` to modify the behavior of an existing core rule, specifically `no-unused-expressions`, to ignore specific patterns. It also shows the necessary setup for integrating the composed rule into an ESLint configuration.

const ruleComposer = require('eslint-rule-composer');
const { Linter } = require('eslint');

// Instantiate ESLint's Linter to get access to core rules
const linter = new Linter();
const noUnusedExpressionsRule = linter.getRules().get('no-unused-expressions');

// Create a modified version of 'no-unused-expressions' that ignores lines starting with 'expect'
module.exports = ruleComposer.filterReports(
  noUnusedExpressionsRule,
  (problem, metadata) => {
    // Ensure the problem node and its first token exist before accessing properties
    if (!problem.node || !metadata.sourceCode || !metadata.sourceCode.getFirstToken(problem.node)) {
      return true; // Keep the report if parsing issue or no token
    }
    return metadata.sourceCode.getFirstToken(problem.node).value !== 'expect';
  }
);

/* To use this rule:
  1. Save this code as a rule file (e.g., `rules/custom-no-unused-expressions.js`)
  2. In your `.eslintrc.js` or equivalent config, define a plugin:
     module.exports = {
       plugins: {
         'my-plugin': {
           rules: {
             'custom-no-unused-expressions': require('./rules/custom-no-unused-expressions'),
           },
         },
       },
       rules: {
         'my-plugin/custom-no-unused-expressions': 'error',
       },
     };
*/

view raw JSON →