CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-eslint-plugin-unicorn

More than 100 powerful ESLint rules for enforcing code quality, consistency, and modern JavaScript best practices

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

anti-pattern-prevention-rules.mddocs/

Anti-Pattern Prevention Rules

Rules that prevent common anti-patterns, discourage problematic constructs, and enforce safer coding practices.

Capabilities

Loop and Iteration Anti-Patterns

Rules preventing inefficient or problematic looping constructs.

/**
 * Discourages Array.forEach() in favor of for-of loops
 */
'unicorn/no-array-for-each': 'error' | 'warn' | 'off';

/**
 * Discourages traditional for loops in favor of modern iteration
 */
'unicorn/no-for-loop': 'error' | 'warn' | 'off';

/**
 * Prevents Array.reduce() usage for better readability
 */
'unicorn/no-array-reduce': 'error' | 'warn' | 'off' | [
  'error' | 'warn',
  {
    allowSimpleOperations?: boolean; // Default: true
  }
];

Usage Examples:

// ❌ Bad - forEach instead of for-of
items.forEach(item => {
  console.log(item);
});

// ✅ Good - for-of loop
for (const item of items) {
  console.log(item);
}

// ❌ Bad - traditional for loop
for (let i = 0; i < items.length; i++) {
  process(items[i]);
}

// ✅ Good - for-of loop
for (const item of items) {
  process(item);
}

// ❌ Bad - complex reduce usage
const result = items.reduce((acc, item) => {
  if (item.active) {
    acc.push(item.name);
  }
  return acc;
}, []);

// ✅ Good - clear filter and map
const result = items
  .filter(item => item.active)
  .map(item => item.name);

Constructor Anti-Patterns

Rules preventing problematic constructor usage.

/**
 * Prevents new Array() constructor usage
 */
'unicorn/no-new-array': 'error' | 'warn' | 'off';

/**
 * Prevents new Buffer() constructor usage
 */
'unicorn/no-new-buffer': 'error' | 'warn' | 'off';

/**
 * Requires new keyword for built-in constructors
 */
'unicorn/new-for-builtins': 'error' | 'warn' | 'off';

Usage Examples:

// ❌ Bad - Array constructor
const array = new Array(3);

// ✅ Good - array literal
const array = [undefined, undefined, undefined];
// or
const array = Array.from({ length: 3 });

// ❌ Bad - Buffer constructor
const buffer = new Buffer('hello');

// ✅ Good - Buffer.from()
const buffer = Buffer.from('hello');

// ❌ Bad - missing new keyword
const obj = Object();

// ✅ Good - using new keyword
const obj = new Object();

Type Checking Anti-Patterns

Rules preventing problematic type checking patterns.

/**
 * Prevents instanceof with built-in types
 */
'unicorn/no-instanceof-builtins': 'error' | 'warn' | 'off';

/**
 * Prevents typeof undefined comparisons
 */
'unicorn/no-typeof-undefined': 'error' | 'warn' | 'off' | [
  'error' | 'warn',
  {
    checkGlobalVariables?: boolean; // Default: false
  }
];

/**
 * Discourages null usage in favor of undefined
 */
'unicorn/no-null': 'error' | 'warn' | 'off' | [
  'error' | 'warn',
  {
    checkStrictEquality?: boolean; // Default: true
  }
];

Usage Examples:

// ❌ Bad - instanceof with built-ins
if (value instanceof Array) {
  // handle array
}

// ✅ Good - Array.isArray()
if (Array.isArray(value)) {
  // handle array
}

// ❌ Bad - typeof undefined check
if (typeof value === 'undefined') {
  // handle undefined
}

// ✅ Good - direct undefined check
if (value === undefined) {
  // handle undefined
}

// ❌ Bad - using null
const defaultValue = null;

// ✅ Good - using undefined
const defaultValue = undefined;

Class and Object Anti-Patterns

Rules preventing problematic class and object patterns.

/**
 * Prevents static-only classes
 */
'unicorn/no-static-only-class': 'error' | 'warn' | 'off';

/**
 * Prevents objects as default parameters
 */
'unicorn/no-object-as-default-parameter': 'error' | 'warn' | 'off';

/**
 * Prevents this assignment
 */
'unicorn/no-this-assignment': 'error' | 'warn' | 'off';

/**
 * Prevents thenable objects that aren't promises
 */
'unicorn/no-thenable': 'error' | 'warn' | 'off';

Usage Examples:

// ❌ Bad - static-only class
class Utils {
  static helper() {
    return 'help';
  }
}

// ✅ Good - plain object or functions
const utils = {
  helper() {
    return 'help';
  }
};

// ❌ Bad - object as default parameter
function process(options = {}) {
  // Same object reference used for all calls
}

// ✅ Good - null/undefined default
function process(options = null) {
  const opts = options || {};
  // New object for each call
}

// ❌ Bad - this assignment
const self = this;

// ✅ Good - arrow functions or proper binding
const boundMethod = this.method.bind(this);

Process and Environment Anti-Patterns

Rules preventing problematic process and environment usage.

/**
 * Prevents process.exit() usage
 */
'unicorn/no-process-exit': 'error' | 'warn' | 'off';

/**
 * Prevents document.cookie usage
 */
'unicorn/no-document-cookie': 'error' | 'warn' | 'off';

/**
 * Prevents abusive eslint-disable comments
 */
'unicorn/no-abusive-eslint-disable': 'error' | 'warn' | 'off';

Usage Examples:

// ❌ Bad - process.exit()
if (error) {
  process.exit(1);
}

// ✅ Good - throw error or return
if (error) {
  throw error;
}

// ❌ Bad - document.cookie
const userId = document.cookie.split('userId=')[1];

// ✅ Good - proper cookie library
const userId = getCookie('userId');

Promise Anti-Patterns

Rules preventing problematic Promise usage patterns.

/**
 * Prevents single Promise in Promise.all/race/etc
 */
'unicorn/no-single-promise-in-promise-methods': 'error' | 'warn' | 'off';

/**
 * Prevents await in Promise method callbacks
 */
'unicorn/no-await-in-promise-methods': 'error' | 'warn' | 'off';

/**
 * Prevents await on expression members
 */
'unicorn/no-await-expression-member': 'error' | 'warn' | 'off';

/**
 * Prevents unnecessary await
 */
'unicorn/no-unnecessary-await': 'error' | 'warn' | 'off';

Usage Examples:

// ❌ Bad - single Promise in Promise.all
const result = await Promise.all([fetchData()]);

// ✅ Good - direct await for single Promise
const result = await fetchData();

// ❌ Bad - await in Promise methods
Promise.all([
  async () => await fetchData(),
  async () => await fetchOtherData()
]);

// ✅ Good - return promises directly
Promise.all([
  fetchData(),
  fetchOtherData()
]);

// ❌ Bad - unnecessary await in return
async function getData() {
  return await fetchData();
}

// ✅ Good - return promise directly
async function getData() {
  return fetchData();
}

Array and String Anti-Patterns

Rules preventing inefficient array and string operations.

/**
 * Prevents array callback reference issues
 */
'unicorn/no-array-callback-reference': 'error' | 'warn' | 'off';

/**
 * Prevents array method this argument
 */
'unicorn/no-array-method-this-argument': 'error' | 'warn' | 'off';

/**
 * Prevents array reverse mutations
 */
'unicorn/no-array-reverse': 'error' | 'warn' | 'off';

/**
 * Prevents useless spread operations
 */
'unicorn/no-useless-spread': 'error' | 'warn' | 'off';

/**
 * Prevents useless fallback in spread
 */
'unicorn/no-useless-fallback-in-spread': 'error' | 'warn' | 'off';

Usage Examples:

// ❌ Bad - problematic callback reference
const numbers = ['1', '2', '3'].map(Number);

// ✅ Good - explicit callback
const numbers = ['1', '2', '3'].map(str => Number(str));

// ❌ Bad - array reverse mutation
const reversed = array.reverse();

// ✅ Good - non-mutating reverse
const reversed = [...array].reverse();

// ❌ Bad - useless spread
const result = [...[1, 2, 3]];

// ✅ Good - direct assignment
const result = [1, 2, 3];

Conditional Anti-Patterns

Rules preventing problematic conditional patterns.

/**
 * Prevents negated conditions
 */
'unicorn/no-negated-condition': 'error' | 'warn' | 'off';

/**
 * Prevents nested ternary operators
 */
'unicorn/no-nested-ternary': 'error' | 'warn' | 'off';

/**
 * Prevents lonely if statements
 */
'unicorn/no-lonely-if': 'error' | 'warn' | 'off';

/**
 * Prevents negation in equality checks
 */
'unicorn/no-negation-in-equality-check': 'error' | 'warn' | 'off';

Usage Examples:

// ❌ Bad - negated condition
if (!valid) {
  handleInvalid();
} else {
  handleValid();
}

// ✅ Good - positive condition
if (valid) {
  handleValid();
} else {
  handleInvalid();
}

// ❌ Bad - nested ternary
const result = condition1 ? value1 : condition2 ? value2 : value3;

// ✅ Good - if-else chain
let result;
if (condition1) {
  result = value1;
} else if (condition2) {
  result = value2;
} else {
  result = value3;
}

// ❌ Bad - lonely if in else
if (condition1) {
  handle1();
} else {
  if (condition2) {
    handle2();
  }
}

// ✅ Good - else if chain
if (condition1) {
  handle1();
} else if (condition2) {
  handle2();
}

Configuration Examples

Comprehensive Anti-Pattern Prevention

export default [
  {
    plugins: {
      unicorn: eslintPluginUnicorn,
    },
    rules: {
      // Loop anti-patterns
      'unicorn/no-array-for-each': 'error',
      'unicorn/no-for-loop': 'error',
      'unicorn/no-array-reduce': 'error',
      
      // Constructor anti-patterns
      'unicorn/no-new-array': 'error',
      'unicorn/no-new-buffer': 'error',
      'unicorn/new-for-builtins': 'error',
      
      // Type checking anti-patterns
      'unicorn/no-instanceof-builtins': 'error',
      'unicorn/no-typeof-undefined': 'error',
      'unicorn/no-null': 'error',
      
      // Class anti-patterns
      'unicorn/no-static-only-class': 'error',
      'unicorn/no-object-as-default-parameter': 'error',
      'unicorn/no-this-assignment': 'error',
      
      // Promise anti-patterns
      'unicorn/no-single-promise-in-promise-methods': 'error',
      'unicorn/no-await-in-promise-methods': 'error',
      'unicorn/no-unnecessary-await': 'error',
      
      // Conditional anti-patterns
      'unicorn/no-negated-condition': 'error',
      'unicorn/no-nested-ternary': 'error',
      'unicorn/no-lonely-if': 'error',
    },
  },
];

docs

anti-pattern-prevention-rules.md

array-object-rules.md

code-quality-rules.md

code-style-rules.md

dom-browser-rules.md

import-export-rules.md

index.md

modern-javascript-rules.md

plugin-configuration.md

tile.json