sindresorhus/eslint-plugin-unicorn

Rule proposal: `prefer-string-repeat`

Closed

#1,708 opened on Jan 28, 2022

 (14 comments) (1 reaction) (0 assignees)JavaScript (468 forks)user submission
help wantednew rule

Repository metrics

Stars
 (5,022 stars)
PR merge metrics
 (Avg merge 1d 16h) (399 merged PRs in 30d)

Description

Description

There are legacy ways to repeat string, we should replace them with .repeat(), but this proposal is not about that. I want enforce .repeat() for unreadable repeating string literals.

Fail

const string = '    '; // I can't see how many are there

const string = 'unicorn unicorn unicorn unicorn';

Pass

const string = ' '.repeat(4);
const string = 'unicorn '.repeat(4).trim();  // This?
const string = Array.from({length: 4}).fill('unicorn').join(' '); // Or this?
const string = Array.from({length: 4}, () => 'unicorn').join(' '); // Or this?

Contributor guide