close

no-new-buffer

Unreleased

Configuration

PresetConfigured Value
✅ unicornPlugin.configs.recommended"error"
rslint.config.ts
import { defineConfig, unicornPlugin } from '@rslint/core';

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

Enforce Buffer.from() and Buffer.alloc() instead of the deprecated new Buffer() constructor.

Examples

// Incorrect
const bytes = new Buffer([0x62, 0x75, 0x66]);
const empty = new Buffer(10);

// Correct
const bytes = Buffer.from([0x62, 0x75, 0x66]);
const empty = Buffer.alloc(10);

When the argument does not establish whether it is a size or source data, the rule reports a diagnostic with Buffer.from() and Buffer.alloc() suggestions.

Further reading