Rules focused on preventing bugs, improving code reliability, and enforcing best practices for error handling and code structure.
Improves regular expressions for better performance and readability.
/**
* Optimizes regular expressions using clean-regexp
* Suggests improvements for regex patterns
*/
'unicorn/better-regex': 'error' | 'warn' | 'off' | [
'error' | 'warn',
{
sortCharacterClasses?: boolean; // Default: true
}
];Usage Examples:
// ❌ Bad - inefficient regex
const regex1 = /[0-9]/;
const regex2 = /[a-zA-Z0-9_]/;
// ✅ Good - optimized regex
const regex1 = /\d/;
const regex2 = /\w/;Enforces consistent error parameter naming in catch blocks.
/**
* Ensures catch block error parameters follow naming conventions
* Default name is 'error', customizable via options
*/
'unicorn/catch-error-name': 'error' | 'warn' | 'off' | [
'error' | 'warn',
{
name?: string; // Default: 'error'
ignore?: string[]; // Default: []
}
];Usage Examples:
// ❌ Bad - inconsistent error naming
try {
riskyOperation();
} catch (e) {
console.log(e.message);
}
// ✅ Good - consistent error naming
try {
riskyOperation();
} catch (error) {
console.log(error.message);
}Enforces consistent function scoping by moving functions to appropriate scopes.
/**
* Moves functions to higher scope when they don't reference local variables
* Improves performance and readability
*/
'unicorn/consistent-function-scoping': 'error' | 'warn' | 'off' | [
'error' | 'warn',
{
checkArrowFunctions?: boolean; // Default: true
}
];Usage Examples:
// ❌ Bad - function can be moved to module scope
function processData() {
function helper(item) {
return item.toString();
}
return data.map(helper);
}
// ✅ Good - helper function moved to module scope
function helper(item) {
return item.toString();
}
function processData() {
return data.map(helper);
}Enforces proper custom error class definitions.
/**
* Ensures custom error classes extend Error correctly
* Validates proper error constructor implementation
*/
'unicorn/custom-error-definition': 'error' | 'warn' | 'off';Usage Examples:
// ❌ Bad - incomplete custom error
class CustomError {
constructor(message) {
this.message = message;
}
}
// ✅ Good - proper custom error
class CustomError extends Error {
constructor(message) {
super(message);
this.name = 'CustomError';
}
}Enforces proper error message format and content.
/**
* Validates error message format and suggests improvements
* Encourages descriptive error messages
*/
'unicorn/error-message': 'error' | 'warn' | 'off';Usage Examples:
// ❌ Bad - generic error message
throw new Error('Error');
// ✅ Good - descriptive error message
throw new Error('Invalid user ID: expected positive integer, received ' + userId);Enforces using new when throwing errors.
/**
* Requires 'new' keyword when throwing Error constructors
* Ensures proper error instantiation
*/
'unicorn/throw-new-error': 'error' | 'warn' | 'off';Usage Examples:
// ❌ Bad - missing 'new' keyword
throw Error('Something went wrong');
// ✅ Good - using 'new' keyword
throw new Error('Something went wrong');Enforces consistent assertion message format.
/**
* Ensures consistent assertion message patterns
* Validates assert.ok, assert.equal, and similar calls
*/
'unicorn/consistent-assert': 'error' | 'warn' | 'off' | [
'error' | 'warn',
{
assertionStyle?: 'always' | 'never'; // Default: 'always'
}
];Usage Examples:
import assert from 'assert';
// ❌ Bad - inconsistent assertion style
assert(condition);
assert.equal(actual, expected, 'Values should be equal');
// ✅ Good - consistent assertion style
assert(condition, 'Condition should be true');
assert.equal(actual, expected, 'Values should be equal');Enforces consistent Date object cloning.
/**
* Ensures consistent patterns for cloning Date objects
* Prefers new Date(date) over other cloning methods
*/
'unicorn/consistent-date-clone': 'error' | 'warn' | 'off';Usage Examples:
// ❌ Bad - inconsistent date cloning
const cloned1 = new Date(date.getTime());
const cloned2 = new Date(+date);
// ✅ Good - consistent date cloning
const cloned = new Date(date);Enforces consistent destructuring patterns.
/**
* Promotes consistent destructuring usage
* Suggests destructuring when accessing multiple properties
*/
'unicorn/consistent-destructuring': 'error' | 'warn' | 'off';Usage Examples:
// ❌ Bad - repeated property access
function processUser(user) {
console.log(user.name);
console.log(user.email);
console.log(user.age);
}
// ✅ Good - consistent destructuring
function processUser(user) {
const { name, email, age } = user;
console.log(name);
console.log(email);
console.log(age);
}Enforces consistent empty array spreading patterns.
/**
* Ensures consistent patterns when spreading empty arrays
* Prevents unnecessary empty array spreads
*/
'unicorn/consistent-empty-array-spread': 'error' | 'warn' | 'off';Usage Examples:
// ❌ Bad - unnecessary empty array spread
const result = [...[], ...items];
// ✅ Good - clean array spread
const result = [...items];Enforces consistent patterns for checking array index existence.
/**
* Promotes consistent array index existence checking
* Suggests .includes() over indexOf() !== -1 patterns
*/
'unicorn/consistent-existence-index-check': 'error' | 'warn' | 'off';Usage Examples:
// ❌ Bad - inconsistent existence check
if (array.indexOf(item) !== -1) {
// handle found item
}
// ✅ Good - consistent existence check
if (array.includes(item)) {
// handle found item
}Enforces explicit length checks instead of implicit truthy checks.
/**
* Requires explicit length checks for arrays and strings
* Improves code clarity and prevents subtle bugs
*/
'unicorn/explicit-length-check': 'error' | 'warn' | 'off' | [
'error' | 'warn',
{
'non-zero'?: 'greater-than' | 'not-equal'; // Default: 'greater-than'
}
];Usage Examples:
// ❌ Bad - implicit length check
if (array) {
// Process array
}
// ✅ Good - explicit length check
if (array.length > 0) {
// Process array
}Prevents infinite recursion in getter/setter methods.
/**
* Detects potential infinite recursion in accessor methods
* Prevents common getter/setter implementation mistakes
*/
'unicorn/no-accessor-recursion': 'error' | 'warn' | 'off';Usage Examples:
class Example {
// ❌ Bad - infinite recursion
get value() {
return this.value;
}
// ✅ Good - proper getter implementation
get value() {
return this._value;
}
set value(newValue) {
this._value = newValue;
}
}Prevents unnecessary Error.captureStackTrace calls.
/**
* Identifies unnecessary Error.captureStackTrace usage
* Prevents redundant stack trace manipulation
*/
'unicorn/no-useless-error-capture-stack-trace': 'error' | 'warn' | 'off';Usage Examples:
// ❌ Bad - unnecessary captureStackTrace
class CustomError extends Error {
constructor(message) {
super(message);
Error.captureStackTrace(this, CustomError); // Unnecessary
}
}
// ✅ Good - clean error class
class CustomError extends Error {
constructor(message) {
super(message);
this.name = 'CustomError';
}
}export default [
{
plugins: {
unicorn: eslintPluginUnicorn,
},
rules: {
// Error prevention
'unicorn/better-regex': 'error',
'unicorn/catch-error-name': 'error',
'unicorn/custom-error-definition': 'error',
'unicorn/throw-new-error': 'error',
// Code consistency
'unicorn/consistent-function-scoping': 'error',
'unicorn/consistent-destructuring': 'error',
'unicorn/explicit-length-check': 'error',
// Error handling best practices
'unicorn/error-message': 'error',
'unicorn/no-accessor-recursion': 'error',
'unicorn/no-useless-error-capture-stack-trace': 'error',
},
},
];export default [
{
plugins: {
unicorn: eslintPluginUnicorn,
},
rules: {
// Custom error naming convention
'unicorn/catch-error-name': ['error', { name: 'err' }],
// Custom assertion style
'unicorn/consistent-assert': ['error', { assertionStyle: 'never' }],
// Custom length check style
'unicorn/explicit-length-check': ['error', { 'non-zero': 'not-equal' }],
// Disable function scoping for arrow functions
'unicorn/consistent-function-scoping': ['error', { checkArrowFunctions: false }],
},
},
];