Skip to content

Commit 7c2c1ff

Browse files
authored
prefer-string-raw: Refactor implementation of helper function (#2692)
1 parent 944cb50 commit 7c2c1ff

File tree

1 file changed

+3
-18
lines changed

1 file changed

+3
-18
lines changed

rules/prefer-string-raw.js

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,9 @@ const BACKSLASH = '\\';
1111
function unescapeBackslash(raw) {
1212
const quote = raw.charAt(0);
1313

14-
raw = raw.slice(1, -1);
15-
16-
let result = '';
17-
for (let position = 0; position < raw.length; position++) {
18-
const character = raw[position];
19-
if (character === BACKSLASH) {
20-
const nextCharacter = raw[position + 1];
21-
if (nextCharacter === BACKSLASH || nextCharacter === quote) {
22-
result += nextCharacter;
23-
position++;
24-
continue;
25-
}
26-
}
27-
28-
result += character;
29-
}
30-
31-
return result;
14+
return raw
15+
.slice(1, -1)
16+
.replaceAll(new RegExp(String.raw`\\(?<escapedCharacter>[\\${quote}])`, 'g'), '$<escapedCharacter>');
3217
}
3318

3419
/** @param {import('eslint').Rule.RuleContext} context */

0 commit comments

Comments
 (0)