Skip to content

Commit 20b5ad0

Browse files
authored
fix: quadratic-time regex in prefer-template (#21096)
1 parent 8b6f6c0 commit 20b5ad0

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

‎lib/rules/prefer-template.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ module.exports = {
204204
*/
205205
return `\`${currentNode.raw
206206
.slice(1, -1)
207-
.replace(/\\*(\$\{|`)/gu, matched => {
207+
.replace(/(?<!\\)\\*(\$\{|`)/gu, matched => {
208208
if (matched.lastIndexOf("\\") % 2) {
209209
return `\\${matched}`;
210210
}

‎tests/lib/rules/prefer-template.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,5 +408,16 @@ ruleTester.run("prefer-template", rule, {
408408
output: "`Hello ` + `'world' ${ test}`",
409409
errors,
410410
},
411+
412+
/*
413+
* Performance test for handling escape characters.
414+
* A quadratic-time regex would need several minutes,
415+
* thus exceeding the timeout for tests.
416+
*/
417+
{
418+
code: `"${"\\".repeat(1_000_000)}" + test`,
419+
output: `\`${"\\".repeat(1_000_000)}\${ test}\``,
420+
errors,
421+
},
411422
],
412423
});

0 commit comments

Comments
 (0)