eslint-plugin-unicorn
eslint-plugin-unicorn copied to clipboard
Enforce `.replaceAll()` over `.split().join()` in `prefer-string-replace-all` rule
Fail
foo.split('a').join('b');
Pass
foo.replaceAll('a', 'b');
// Not sure about this, maybe add `g` flag, result the same?
foo.split(/a+/).join('b');
// Different with foo.replaceAll('', 'b');
foo.split('').join('b');
This is accepted.
// Not sure about this, maybe add `g` flag, result the same? foo.split(/a+/).join('b');
It's important to remember that calling split with a regular expression has some gotchas, such as if the regex has a capturing group:
'a_b_c'.split(/_/);
//=> [ 'a', 'b', 'c' ]
'a_b_c'.split(/(_)/);
//=> [ 'a', '_', 'b', '_', 'c' ]
The second argument also little different https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_the_replacement
'1_2_3'.split('_').join('$&')
//=> '1$&2$&3'
'1_2_3'.replaceAll('_', '$&')
//=> '1_2_3'
'1_2_3'.split('_').join('$$')
//=> '1$$2$$3'
'1_2_3'.replaceAll('_', '$$')
//=> '1$2$3'
The fix can double each $ to $$ in the replacement argument if it’s known to be a string literal (e.g. .split('_').join('$$') → .replaceAll('_', '$$$$')), and otherwise use a function (e.g. .split('_').join(s) → .replaceAll('_', () => s)).