585
586
export function fixRegexNewline(pattern: string): string {
588
>
let re: ReAST.Pattern;
589
>
try {
590
>
re = new RegExpParser().parsePattern(pattern);
591
>
} catch {
592
return pattern;
593
}
595
>
let output = '';
596
>
let lastEmittedIndex = 0;
597
>
const replace = (start: number, end: number, text: string) => {
598
>
output += pattern.slice(lastEmittedIndex, start) + text;
599
>
lastEmittedIndex = end;
600
>
};
601
>
602
>
const context: ReAST.Node[] = [];
603
>
const visitor = new RegExpVisitor({
604
>
onCharacterEnter(char) {
605
>
if (char.raw !== '\\n') {
606
>
return;
607
>
}
608
>
609
>
const parent = context[0];
610
>
if (!parent) {
611
>
// simple char, \n -> \r?\n
612
>
replace(char.start, char.end, '\\r?\\n');
613
>
} else if (context.some(isLookBehind)) {
614
// no-op in a lookbehind, see #100569
616
if (parent.negate) {
617
// negative bracket expr, [^a-z\n] -> (?![a-z]|\r?\n)