ESLint No Restricted Utility
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
-
TypeError: (0 , eslint_no_restricted_properties__WEBPACK_IMPORTED_MODULE_0__.create) is not a function
cause Incorrect CommonJS `require()` syntax or improper ESM setup when using a bundler (like Webpack) that tries to resolve a default export that doesn't exist, or when running in a Node.js environment that struggles with ESM interop for the module.fixIf using CommonJS, use `const { create } = require('eslint-no-restricted/properties');`. If using ESM, ensure `package.json` has `"type": "module"` or files end in `.mjs`, and verify bundler configuration correctly handles ESM imports. For TypeScript, ensure `moduleResolution` is correctly configured. -
ESLint: Definition for rule 'my-plugin/no-console-log' was not found.
cause The custom ESLint rule created with `eslint-no-restricted` was not correctly registered in the `.eslintrc` configuration file under the `plugins` or `rules` section, or the path to the custom plugin is incorrect.fixVerify that your custom plugin (e.g., `my-eslint-plugin/index.ts`) is correctly exported and that its path is specified in the `plugins` array of your `.eslintrc` file. Also, ensure the rule name in `rules` (`'my-plugin/no-console-log'`) matches the plugin's name and the rule's `name` property.
Warnings
- breaking Version 0.1.0 introduced a 'narrowing down' of the `create*` return type. This change might cause TypeScript compilation errors if existing code relied on a broader or less specific type for the plugin object returned by `create` functions.
- gotcha Prior to version 0.0.10, there were issues with TypeScript mistakenly reporting named exports for Node.js 16 when using ESM. Although fixed, developers should be mindful of correct module resolution settings, especially in mixed ESM/CJS environments or when using older Node.js versions, to avoid similar import or type-resolution problems.
- gotcha This package is a utility for creating ESLint rules and has `eslint` as a peer dependency. Incompatible `eslint` versions can lead to runtime errors or unexpected behavior.
Install
-
npm install eslint-no-restricted -
yarn add eslint-no-restricted -
pnpm add eslint-no-restricted
Imports
- create
import { create } from 'eslint-no-restricted';import { create } from 'eslint-no-restricted/globals'; - RuleBase
import type { RuleBase } from 'eslint-no-restricted/shared'; - RuleConfig
import type { RuleConfig } from 'eslint-no-restricted/properties';
Quickstart
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,
// },
// };