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);