Refactor duration regex and remove RegExp.escape polyfill

Removed polyfill for RegExp.escape and updated duration regex.

Signed-off-by: ADITYA TIWARI <142050150+ADITYATIWARI342005@users.noreply.github.com>
This commit is contained in:
ADITYA TIWARI 2025-11-27 16:40:33 +05:30 committed by GitHub
parent 42418660d3
commit 49427cfcd2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -166,22 +166,9 @@ function arrayToCompletionResult(data: Completion[], from: number, to: number, i
} as CompletionResult;
}
// Polyfill RegExp.escape for compatibility with ES2024 and TypeScript.
// Ensures safe, standards-based regex escaping in all environments.
declare global {
interface RegExpConstructor {
escape?: (s: string) => string;
}
}
if (!RegExp.escape) {
RegExp.escape = function (s: string): string {
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
};
}
// Matches complete PromQL durations, including compound units (e.g., 5m, 1d2h, 1h30m, etc.)
export const durationWithUnitRegexp = new RegExp(`^(\\d+(${durationTerms.map((term) => RegExp.escape!(term.label)).join('|')}))+$`);
// Matches complete PromQL durations, including compound units (e.g., 5m, 1d2h, 1h30m, etc.).
// Duration units are a fixed, safe set (no regex metacharacters), so no escaping is needed.
export const durationWithUnitRegexp = new RegExp(`^(\\d+(${durationTerms.map((term) => term.label).join('|')}))+$`);
// Determines if a duration already has a complete time unit to prevent autocomplete insertion (issue #15452)
function hasCompleteDurationUnit(state: EditorState, node: SyntaxNode): boolean {