ESLint No Restricted Utility

0.1.1 · active · verified Tue Apr 21

eslint-no-restricted is a utility for generating highly customizable ESLint rules, serving as a powerful alternative to the core `no-restricted-syntax`, `no-restricted-globals`, and `no-restricted-properties` rules. Currently at version 0.1.1, the package is actively maintained with frequent minor releases, typically for feature enhancements and bug fixes. Its key differentiators include the ability to create individual ESLint rules for each specific restricted item (AST selector, global variable, or object property), allowing for granular control over severity levels and more precise disabling via comments. This contrasts with the core rules which lump all restrictions into a single configurable rule. Additionally, it supports message placeholders, enabling developers to create more targeted and informative error messages based on the code context. It supports Node.js `^20.9.0 || >=22.0.0` and is compatible with `eslint` versions `^8.57.0 || ^9 || ^10`, shipping with full TypeScript type definitions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates creating a custom ESLint rule that disallows `console.log` using `eslint-no-restricted/properties`, and integrating it into an ESLint configuration. It shows the `create` function for rule definition and how to expose it through a custom ESLint plugin.

import { create } from 'eslint-no-restricted/properties';
import type { RuleConfig } from 'eslint-no-restricted/properties';
import path from 'path';

// my-eslint-plugin/rules/no-console-log.ts
const noConsoleLogRuleConfig: RuleConfig = {
  name: 'no-console-log',
  message: 'Using {{property}} on {{object}} is not allowed. Please use a dedicated logger instead.',
  properties: {
    object: 'console',
    property: 'log',
  },
  defaultLevel: 'error',
  docUrl: 'https://example.com/docs/no-console-log',
};

const noConsoleLog = create(noConsoleLogRuleConfig);

// my-eslint-plugin/index.ts (main plugin file)
export = {
  rules: {
    'no-console-log': noConsoleLog,
  },
  // Optionally, you can add recommended configs
  // configs: {
  //   recommended: {
  //     plugins: ['my-plugin'],
  //     rules: {
  //       'my-plugin/no-console-log': 'error',
  //     },
  //   },
  // },
};

// .eslintrc.js (example ESLint configuration)
// module.exports = {
//   root: true,
//   plugins: [
//     // Assuming your custom plugin is in a directory relative to .eslintrc.js
//     path.resolve(__dirname, 'my-eslint-plugin'),
//   ],
//   rules: {
//     'my-eslint-plugin/no-console-log': 'error',
//     'no-debugger': 'error',
//   },
//   parserOptions: {
//     ecmaVersion: 'latest',
//     sourceType: 'module',
//   },
//   env: {
//     node: true,
//     browser: true,
//   },
// };

view raw JSON →