(oldLF: string)
| 85 | } |
| 86 | |
| 87 | function buildWhitespaceTolerantRegex(oldLF: string): RegExp { |
| 88 | if (oldLF === "") { |
| 89 | // Never match empty string |
| 90 | return new RegExp("(?!)", "g") |
| 91 | } |
| 92 | |
| 93 | const parts = oldLF.match(/(\s+|\S+)/g) ?? [] |
| 94 | const whitespacePatternForRun = (run: string): string => { |
| 95 | // If the whitespace run includes a newline, allow matching any whitespace (including newlines) |
| 96 | // to tolerate wrapping changes across lines. |
| 97 | if (run.includes("\n")) { |
| 98 | return "\\s+" |
| 99 | } |
| 100 | |
| 101 | // Otherwise, limit matching to horizontal whitespace so we don't accidentally consume |
| 102 | // line breaks that precede indentation. |
| 103 | return "[\\t ]+" |
| 104 | } |
| 105 | |
| 106 | const pattern = parts |
| 107 | .map((part) => { |
| 108 | if (/^\s+$/.test(part)) { |
| 109 | return whitespacePatternForRun(part) |
| 110 | } |
| 111 | return escapeRegExp(part) |
| 112 | }) |
| 113 | .join("") |
| 114 | |
| 115 | return new RegExp(pattern, "g") |
| 116 | } |
| 117 | |
| 118 | function buildTokenRegex(oldLF: string): RegExp { |
| 119 | const tokens = oldLF.split(/\s+/).filter(Boolean) |
no test coverage detected