]> git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-empty-character-class.js
import 8.41.0 source
[pve-eslint.git] / eslint / lib / rules / no-empty-character-class.js
1 /**
2 * @fileoverview Rule to flag the use of empty character classes in regular expressions
3 * @author Ian Christian Myers
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Helpers
10 //------------------------------------------------------------------------------
11
12 /*
13 * plain-English description of the following regexp:
14 * 0. `^` fix the match at the beginning of the string
15 * 1. `([^\\[]|\\.|\[([^\\\]]|\\.)+\])*`: regexp contents; 0 or more of the following
16 * 1.0. `[^\\[]`: any character that's not a `\` or a `[` (anything but escape sequences and character classes)
17 * 1.1. `\\.`: an escape sequence
18 * 1.2. `\[([^\\\]]|\\.)+\]`: a character class that isn't empty
19 * 2. `$`: fix the match at the end of the string
20 */
21 const regex = /^([^\\[]|\\.|\[([^\\\]]|\\.)+\])*$/u;
22
23 //------------------------------------------------------------------------------
24 // Rule Definition
25 //------------------------------------------------------------------------------
26
27 /** @type {import('../shared/types').Rule} */
28 module.exports = {
29 meta: {
30 type: "problem",
31
32 docs: {
33 description: "Disallow empty character classes in regular expressions",
34 recommended: true,
35 url: "https://eslint.org/docs/latest/rules/no-empty-character-class"
36 },
37
38 schema: [],
39
40 messages: {
41 unexpected: "Empty class."
42 }
43 },
44
45 create(context) {
46 return {
47 "Literal[regex]"(node) {
48 if (!regex.test(node.regex.pattern)) {
49 context.report({ node, messageId: "unexpected" });
50 }
51 }
52 };
53
54 }
55 };