-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pass test for consecutive unnecessarily escaped characters in strings
See #1575 (comment) This looping is hacky. We might be able to emulate lookbehind instead.
- Loading branch information
1 parent
2e25f4b
commit 2323c8c
Showing
1 changed file
with
18 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3859,10 +3859,6 @@ function makeString(rawContent, enclosingQuote) { | |
// Matches _any_ escape and unescaped quotes (both single and double). | ||
const regex = /\\([\s\S])|(['"])/g; | ||
|
||
// Matches any unnecessarily escaped character. | ||
// Adapted from https://github.com/eslint/eslint/blob/de0b4ad7bd820ade41b1f606008bea68683dc11a/lib/rules/no-useless-escape.js#L27 | ||
const regexUnnecessaryStringEscapes = /((?:^|[^\\])(?:\\\\)*)\\([^\\nrvtbfux\r\n\u2028\u2029"'0-7])/g; | ||
|
||
// Escape and unescape single and double quotes as needed to be able to | ||
// enclose `rawContent` with `enclosingQuote`. | ||
const newContent = rawContent.replace(regex, (match, escaped, quote) => { | ||
|
@@ -3882,11 +3878,25 @@ function makeString(rawContent, enclosingQuote) { | |
|
||
// Otherwise return the escape or unescaped quote as-is. | ||
return match; | ||
}) | ||
// Unescape unnecessarily escaped characters. | ||
.replace(regexUnnecessaryStringEscapes, (match, prev, escaped) => prev + escaped); | ||
}); | ||
|
||
// Matches any unnecessarily escaped character. | ||
// Adapted from https://github.com/eslint/eslint/blob/de0b4ad7bd820ade41b1f606008bea68683dc11a/lib/rules/no-useless-escape.js#L27 | ||
const regexUnnecessaryStringEscapes = /((?:^|[^\\])(?:\\\\)*)\\([^\\nrvtbfux\r\n\u2028\u2029"'0-7])/g; | ||
|
||
let minimallyEscapedContent = newContent; | ||
let minimallyEscapedContentPrev; | ||
|
||
while (minimallyEscapedContent !== minimallyEscapedContentPrev) { | ||
minimallyEscapedContentPrev = minimallyEscapedContent; | ||
// Unescape unnecessarily escaped characters. | ||
minimallyEscapedContent = minimallyEscapedContentPrev.replace( | ||
regexUnnecessaryStringEscapes, | ||
(match, prev, escaped) => prev + escaped | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
josephfrazier
Author
Collaborator
|
||
); | ||
} | ||
|
||
return enclosingQuote + newContent + enclosingQuote; | ||
return enclosingQuote + minimallyEscapedContent + enclosingQuote; | ||
} | ||
|
||
function printRegex(node) { | ||
|
I'm not entirely sure it's worth doing - I don't have a good sense for how expensive string comparison is - but fwiw you can abuse the "replace" function to avoid having to do the string comparison at all; something like