ESLint TypeScript Rule Utilities

5.2.1 · active · verified Sun Apr 19

eslint-etc provides a collection of utility functions designed primarily to aid in the implementation and testing of custom ESLint rules, particularly those targeting TypeScript codebases. The current stable version is 5.2.1. While there isn't a stated release cadence, updates are typically made as needed for new features, bug fixes, or compatibility with newer ESLint and TypeScript versions. A key differentiator is the `fromFixture` utility, which enables a TSLint-like fixture testing approach for ESLint rules. This simplifies the creation of test cases by allowing developers to directly annotate expected error locations and messages within multiline code strings, abstracting away the need for explicit line and column numbers. This library is largely used by its author for their own suite of ESLint rules, indicating a practical, developer-focused design, though documentation can be light for external users.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `fromFixture` with `@typescript-eslint/rule-tester` to simplify the testing of a custom ESLint rule that disallows `const` declarations. It shows how to define a rule and then write test cases using annotated code snippets instead of explicit line and column numbers.

import { RuleTester } from '@typescript-eslint/rule-tester';
import { fromFixture } from 'eslint-etc';
import { stripIndent } from 'common-tags'; // Often used for multiline strings in tests

// Define a simple mock ESLint rule
const noConstRule = {
  meta: {
    type: 'suggestion',
    messages: {
      noConst: 'Do not use `const` for variable declarations.',
    },
    schema: [],
  },
  create(context) {
    return {
      VariableDeclaration(node) {
        if (node.kind === 'const') {
          context.report({
            node,
            messageId: 'noConst',
          });
        }
      },
    };
  },
};

const ruleTester = new RuleTester({
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaVersion: 2020,
    sourceType: 'module',
    // Ensure tsconfig.json is properly configured for type-aware rules
    project: './tsconfig.json',
  },
});

ruleTester.run('no-const-rule', noConstRule as any, {
  valid: [
    { code: 'let name = "alice";' },
    { code: 'var age = 30;' },
  ],
  invalid: [
    fromFixture(stripIndent`
      const name = "alice";
      ~~~~~                   [noConst]
    `),
    fromFixture(stripIndent`
      const role = 'cto';
      ~~~~~                   [noConst]
      let value = 10;
    `),
    fromFixture(stripIndent`
      const x = 1;
      ~~~~~        [noConst]
      const y = 2;
      ~~~~~        [noConst]
    `)
  ],
});

view raw JSON →