]> git.proxmox.com Git - pve-eslint.git/blame - eslint/lib/rules/utils/regular-expressions.js
import 8.41.0 source
[pve-eslint.git] / eslint / lib / rules / utils / regular-expressions.js
CommitLineData
f2a92ac6
DC
1/**
2 * @fileoverview Common utils for regular expressions.
3 * @author Josh Goldberg
4 * @author Toru Nagashima
5 */
6
7"use strict";
8
9const { RegExpValidator } = require("@eslint-community/regexpp");
10
11const REGEXPP_LATEST_ECMA_VERSION = 2022;
12
13/**
14 * Checks if the given regular expression pattern would be valid with the `u` flag.
15 * @param {number} ecmaVersion ECMAScript version to parse in.
16 * @param {string} pattern The regular expression pattern to verify.
17 * @returns {boolean} `true` if the pattern would be valid with the `u` flag.
18 * `false` if the pattern would be invalid with the `u` flag or the configured
19 * ecmaVersion doesn't support the `u` flag.
20 */
21function isValidWithUnicodeFlag(ecmaVersion, pattern) {
22 if (ecmaVersion <= 5) { // ecmaVersion <= 5 doesn't support the 'u' flag
23 return false;
24 }
25
26 const validator = new RegExpValidator({
27 ecmaVersion: Math.min(ecmaVersion, REGEXPP_LATEST_ECMA_VERSION)
28 });
29
30 try {
31 validator.validatePattern(pattern, void 0, void 0, /* uFlag = */ true);
32 } catch {
33 return false;
34 }
35
36 return true;
37}
38
39module.exports = {
40 isValidWithUnicodeFlag,
41 REGEXPP_LATEST_ECMA_VERSION
42};