More than 100 powerful ESLint rules for enforcing code quality, consistency, and modern JavaScript best practices
npx @tessl/cli install tessl/npm-eslint-plugin-unicorn@60.0.0ESLint Plugin Unicorn is a comprehensive ESLint plugin that provides 134 powerful ESLint rules for enforcing code quality, consistency, and modern JavaScript best practices. It offers extensive linting capabilities that go beyond ESLint's core rules, covering areas such as array and object manipulation, string formatting, error handling, security patterns, performance optimizations, and API usage improvements.
npm install --save-dev eslint eslint-plugin-unicornimport eslintPluginUnicorn from 'eslint-plugin-unicorn';import eslintPluginUnicorn from 'eslint-plugin-unicorn';
import globals from 'globals';
export default [
{
languageOptions: {
globals: globals.builtin,
},
plugins: {
unicorn: eslintPluginUnicorn,
},
rules: {
'unicorn/better-regex': 'error',
'unicorn/prefer-array-flat': 'error',
'unicorn/no-array-for-each': 'error',
// ... other rules
},
},
];ESLint Plugin Unicorn is built around several key components:
Core plugin object containing all rules and pre-configured ESLint configurations for immediate use.
interface ESLintPluginUnicorn {
meta: {
name: string;
version: string;
};
rules: Record<string, ESLintRule>;
configs: {
recommended: ESLintFlatConfig;
all: ESLintFlatConfig;
'flat/recommended': ESLintFlatConfig; // deprecated
'flat/all': ESLintFlatConfig; // deprecated
};
}
interface ESLintFlatConfig {
name: string;
plugins: Record<string, ESLintPlugin>;
rules: Record<string, string | [string, any]>;
languageOptions: {
globals: Record<string, boolean>;
};
}Rules focused on preventing bugs, improving code reliability, and enforcing best practices for error handling and code structure.
// Example rule configurations
const qualityRules = {
'unicorn/better-regex': 'error',
'unicorn/catch-error-name': 'error',
'unicorn/consistent-function-scoping': 'error',
'unicorn/custom-error-definition': 'error',
'unicorn/error-message': 'error',
'unicorn/throw-new-error': 'error',
};Rules that promote the use of modern JavaScript features, APIs, and patterns over legacy alternatives.
// Example modern JavaScript rule configurations
const modernJsRules = {
'unicorn/prefer-array-flat': 'error',
'unicorn/prefer-array-flat-map': 'error',
'unicorn/prefer-at': 'error',
'unicorn/prefer-code-point': 'error',
'unicorn/prefer-date-now': 'error',
'unicorn/prefer-global-this': 'error',
'unicorn/prefer-includes': 'error',
'unicorn/prefer-modern-dom-apis': 'error',
'unicorn/prefer-node-protocol': 'error',
'unicorn/prefer-string-replace-all': 'error',
'unicorn/prefer-top-level-await': 'error',
};Rules that prevent common anti-patterns, discourage problematic constructs, and enforce safer coding practices.
// Example anti-pattern prevention rule configurations
const antiPatternRules = {
'unicorn/no-array-for-each': 'error',
'unicorn/no-for-loop': 'error',
'unicorn/no-instanceof-builtins': 'error',
'unicorn/no-new-array': 'error',
'unicorn/no-new-buffer': 'error',
'unicorn/no-null': 'error',
'unicorn/no-process-exit': 'error',
'unicorn/no-static-only-class': 'error',
'unicorn/no-useless-undefined': 'error',
};Rules for consistent code formatting, naming conventions, and stylistic preferences that improve code readability.
// Example code style rule configurations
const styleRules = {
'unicorn/empty-brace-spaces': 'error',
'unicorn/escape-case': 'error',
'unicorn/filename-case': 'error',
'unicorn/number-literal-case': 'error',
'unicorn/numeric-separators-style': 'error',
'unicorn/prevent-abbreviations': 'error',
'unicorn/string-content': 'error',
'unicorn/switch-case-braces': 'error',
'unicorn/template-indent': 'error',
};Specialized rules for array and object manipulation, promoting efficient and readable data structure operations.
// Example array and object rule configurations
const arrayObjectRules = {
'unicorn/no-array-callback-reference': 'error',
'unicorn/no-array-method-this-argument': 'error',
'unicorn/no-array-reduce': 'error',
'unicorn/prefer-array-find': 'error',
'unicorn/prefer-array-some': 'error',
'unicorn/prefer-object-from-entries': 'error',
'unicorn/prefer-set-has': 'error',
'unicorn/prefer-spread': 'error',
};Rules governing module imports, exports, and dependency management for better module organization.
// Example import/export rule configurations
const importExportRules = {
'unicorn/import-style': 'error',
'unicorn/no-anonymous-default-export': 'error',
'unicorn/prefer-export-from': 'error',
'unicorn/prefer-module': 'error',
'unicorn/require-module-specifiers': 'error',
};Rules specific to DOM manipulation and browser environment patterns for web development.
// Example DOM and browser rule configurations
const domBrowserRules = {
'unicorn/no-document-cookie': 'error',
'unicorn/prefer-add-event-listener': 'error',
'unicorn/prefer-dom-node-append': 'error',
'unicorn/prefer-dom-node-dataset': 'error',
'unicorn/prefer-dom-node-remove': 'error',
'unicorn/prefer-dom-node-text-content': 'error',
'unicorn/prefer-keyboard-event-key': 'error',
'unicorn/prefer-query-selector': 'error',
};interface ESLintRule {
create(context: ESLint.RuleContext): ESLint.RuleListener;
meta: {
type: 'problem' | 'suggestion' | 'layout';
docs: {
description: string;
recommended: boolean;
url: string;
};
fixable?: 'code' | 'whitespace';
schema: JSONSchema[];
messages: Record<string, string>;
};
}
interface ESLint {
Plugin: {
meta: {
name: string;
version: string;
};
rules: Record<string, ESLintRule>;
configs?: Record<string, ESLintFlatConfig>;
};
}
interface JSONSchema {
type?: string;
properties?: Record<string, any>;
additionalProperties?: boolean;
items?: any;
}