Skip to content

Commit

Permalink
search: fix inconsistent regexes with quantified negated char classes
Browse files Browse the repository at this point in the history
Fixes #134268
  • Loading branch information
connor4312 committed Oct 14, 2021
1 parent 5f235f1 commit b6a2e69
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,15 @@ export function fixRegexNewline(pattern: string): string {
if (parent.negate) {
// negative bracket expr, [^a-z\n] -> (?![a-z]|\r?\n)
const otherContent = pattern.slice(parent.start + 2, char.start) + pattern.slice(char.end, parent.end - 1);
replace(parent.start, parent.end, '(?!\\r?\\n' + (otherContent ? `|[${otherContent}]` : '') + ')');
if (parent.parent?.type === 'Quantifier') {
// If quantified, we can't use a negative lookahead in a quantifier.
// But `.` already doesn't match new lines, so we can just use that
// (with any other negations) instead.
const quant = parent.parent;
replace(quant.start, quant.end, (otherContent ? `[^${otherContent}]` : '.') + (quant.greedy ? '+' : '*'));
} else {
replace(parent.start, parent.end, '(?!\\r?\\n' + (otherContent ? `|[${otherContent}]` : '') + ')');
}
} else {
// positive bracket expr, [a-z\n] -> (?:[a-z]|\r?\n)
const otherContent = pattern.slice(parent.start + 1, char.start) + pattern.slice(char.end, parent.end - 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ suite('RipgrepTextSearchEngine', () => {
['fo\\n+o', 'fo(?:\\r?\\n)+o'],
['fo[^\\n]o', 'fo(?!\\r?\\n)o'],
['fo[^\\na-z]o', 'fo(?!\\r?\\n|[a-z])o'],
['foo[^\\n]+o', 'foo.+o'],
['foo[^\\nzq]+o', 'foo[^zq]+o'],
];

for (const [input, expected] of ttable) {
Expand Down

0 comments on commit b6a2e69

Please sign in to comment.