close

no-array-front-mutation

Added in v0.9.1

Configuration

rslint.config.ts
import { defineConfig, unicornPlugin } from '@rslint/core';

export default defineConfig([
  unicornPlugin.configs.recommended,
  {
    rules: {
      'unicorn/no-array-front-mutation': 'error',
    },
  },
]);

Rule Details

Disallow mutating the front of an array with Array#shift() or Array#unshift(). Re-indexing the remaining elements can make these methods unexpectedly expensive for large arrays.

Examples of incorrect code for this rule:

const first = items.shift();
items.unshift(newItem);

Examples of correct code for this rule:

for (const item of items) {
  consume(item);
}

queue.enqueue(newItem);

The rule allows unshift() on common Node.js stream objects, where it has stream-specific semantics rather than array semantics:

stream.unshift(chunk);
process.stdin.unshift(chunk);

Original Documentation