ESLint Rule Extender Utility

0.0.1 · active · verified Sun Apr 19

eslint-rule-extender is a utility library designed to facilitate the extension and modification of existing ESLint rules without needing to rewrite them entirely. It provides a programmatic API to override aspects of an `ESLintRule` object, allowing developers to customize `meta` properties, add new AST visitors, or modify the reporting behavior of a rule. This can be particularly useful for creating highly specific linting rules based on established ones, or for adapting third-party rules to fit unique project requirements. As of version 0.0.1, it focuses on core functionality, offering a lean approach to rule customization. Its release cadence is not explicitly defined, but as a utility, updates would likely follow ESLint API changes or community-driven feature requests. A key differentiator is its focus on *extending* rather than *composing* rules, providing granular control over specific aspects.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to use `eslint-rule-extender` to customize an existing rule by overriding its `meta` properties, adding new visitor callbacks, and conditionally suppressing reports.

const ruleExtender = require('eslint-rule-extender');
const { originalRule } = require('eslint-plugin-example'); // Replace with a real ESLint rule import

// Example: Define a placeholder originalRule for demonstration
const mockOriginalRule = {
  meta: {
    type: 'problem',
    docs: {
      description: 'Disallow direct use of ThisExpression (mock)',
      category: 'Possible Problems',
      recommended: true
    },
    schema: [],
    messages: {
      defaultMessage: 'Avoid ThisExpression.'
    }
  },
  create(context) {
    return {
      ThisExpression(node) {
        context.report({
          node,
          messageId: 'defaultMessage'
        });
      }
    };
  }
};

// In a real scenario, you'd import an actual rule, e.g., from 'eslint/lib/rules/no-console'
// const originalRule = require('eslint/lib/rules/no-console');

const extendedRule = ruleExtender(mockOriginalRule, {
  metaOverrides: {
    type: 'suggestion',
    fixable: false,
    messages: {
        anAdditionalSuggestion: 'Arrow functions are fine here!',
        ...mockOriginalRule.meta.messages
    }
  },
  createAdditionalVisitors(context) {
    return {
      ArrowFunctionExpression(node) {
        context.report({ node, messageId: 'anAdditionalSuggestion' });
      }
    };
  },
  reportOverrides(meta) {
    // Only report if the node type is NOT 'ThisExpression'
    return meta.node.type !== 'ThisExpression';
  }
});

// To use this extended rule, you would typically export it from an ESLint plugin:
// module.exports = {
//   rules: {
//     'my-extended-rule': extendedRule
//   }
// };

console.log('Extended Rule Meta:', extendedRule.meta);
// Simulate a rule execution for demonstration (not how ESLint actually runs it)
// In a real ESLint setup, this rule would be applied to source code.
// For now, we just show it's configured.

view raw JSON →