-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
require-unicode-regexp
add suggestions (#17007)
* feat: `require-unicode-regexp` add suggestions * Review fixups: invalid patterns; sequence expressions * I promise I know how CJS modules work * Handled non-string literals * Update lib/rules/require-unicode-regexp.js Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com> * Don't concatenate + 'u' * Test the new cases * Touch up regular-expressions.js templating * Add myself as author to regular-expressions.js --------- Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
- Loading branch information
1 parent
4dd8d52
commit b6ab8b2
Showing
5 changed files
with
297 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* @fileoverview Common utils for regular expressions. | ||
* @author Josh Goldberg | ||
* @author Toru Nagashima | ||
*/ | ||
|
||
"use strict"; | ||
|
||
const { RegExpValidator } = require("@eslint-community/regexpp"); | ||
|
||
const REGEXPP_LATEST_ECMA_VERSION = 2022; | ||
|
||
/** | ||
* Checks if the given regular expression pattern would be valid with the `u` flag. | ||
* @param {number} ecmaVersion ECMAScript version to parse in. | ||
* @param {string} pattern The regular expression pattern to verify. | ||
* @returns {boolean} `true` if the pattern would be valid with the `u` flag. | ||
* `false` if the pattern would be invalid with the `u` flag or the configured | ||
* ecmaVersion doesn't support the `u` flag. | ||
*/ | ||
function isValidWithUnicodeFlag(ecmaVersion, pattern) { | ||
if (ecmaVersion <= 5) { // ecmaVersion <= 5 doesn't support the 'u' flag | ||
return false; | ||
} | ||
|
||
const validator = new RegExpValidator({ | ||
ecmaVersion: Math.min(ecmaVersion, REGEXPP_LATEST_ECMA_VERSION) | ||
}); | ||
|
||
try { | ||
validator.validatePattern(pattern, void 0, void 0, /* uFlag = */ true); | ||
} catch { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
module.exports = { | ||
isValidWithUnicodeFlag, | ||
REGEXPP_LATEST_ECMA_VERSION | ||
}; |
Oops, something went wrong.