prefer-string-repeat

June 5, 2026 ยท View on GitHub

๐Ÿ“ Prefer String#repeat() for repeated whitespace.

๐Ÿ’ผ This rule is enabled in the following configs: โœ… recommended, โ˜‘๏ธ unopinionated.

๐Ÿ”ง This rule is automatically fixable by the --fix CLI option.

Repeated whitespace in string literals and no-substitution template literals is hard to count. Use String#repeat() to make the count explicit.

This rule only reports string literals and no-substitution template literals made entirely of the same repeated whitespace character. It does not report repeated words or mixed whitespace.

Examples

// โŒ
const indentation = '    ';

// โœ…
const indentation = ' '.repeat(4);
// โŒ
const padding = '\t\t\t';

// โœ…
const padding = '\t'.repeat(3);
// โŒ
const spaces = '\u2003\u2003\u2003';

// โœ…
const spaces = '\u2003'.repeat(3);
// โœ…
const letters = 'aaa';

// โœ…
const words = 'unicorn unicorn unicorn';

// โœ…
const mixedWhitespace = ' \t ';

Options

minimumRepetitions

Type: integer
Minimum: 2
Default: 3

The minimum number of repeated whitespace characters before String#repeat() is enforced.

/* eslint unicorn/prefer-string-repeat: ["error", {"minimumRepetitions": 2}] */

// โŒ
const indentation = '  ';

// โœ…
const indentation = ' '.repeat(2);