Blame view

node_modules/eslint/lib/rules/no-control-regex.js 3.11 KB
ce4c83ff   wxy   初始提交
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  /**
   * @fileoverview Rule to forbid control charactes from regular expressions.
   * @author Nicholas C. Zakas
   */
  
  "use strict";
  
  const RegExpValidator = require("regexpp").RegExpValidator;
  const collector = new class {
      constructor() {
          this.ecmaVersion = 2018;
          this._source = "";
          this._controlChars = [];
          this._validator = new RegExpValidator(this);
      }
  
      onPatternEnter() {
          this._controlChars = [];
      }
  
      onCharacter(start, end, cp) {
          if (cp >= 0x00 &&
              cp <= 0x1F &&
              (
                  this._source.codePointAt(start) === cp ||
                  this._source.slice(start, end).startsWith("\\x") ||
                  this._source.slice(start, end).startsWith("\\u")
              )
          ) {
              this._controlChars.push(`\\x${`0${cp.toString(16)}`.slice(-2)}`);
          }
      }
  
      collectControlChars(regexpStr) {
          try {
              this._source = regexpStr;
              this._validator.validatePattern(regexpStr); // Call onCharacter hook
          } catch (err) {
  
              // Ignore syntax errors in RegExp.
          }
          return this._controlChars;
      }
  }();
  
  //------------------------------------------------------------------------------
  // Rule Definition
  //------------------------------------------------------------------------------
  
  module.exports = {
      meta: {
          docs: {
              description: "disallow control characters in regular expressions",
              category: "Possible Errors",
              recommended: true,
              url: "https://eslint.org/docs/rules/no-control-regex"
          },
  
          schema: [],
  
          messages: {
              unexpected: "Unexpected control character(s) in regular expression: {{controlChars}}."
          }
      },
  
      create(context) {
  
          /**
           * Get the regex expression
           * @param {ASTNode} node node to evaluate
           * @returns {RegExp|null} Regex if found else null
           * @private
           */
          function getRegExpPattern(node) {
              if (node.regex) {
                  return node.regex.pattern;
              }
              if (typeof node.value === "string" &&
                  (node.parent.type === "NewExpression" || node.parent.type === "CallExpression") &&
                  node.parent.callee.type === "Identifier" &&
                  node.parent.callee.name === "RegExp" &&
                  node.parent.arguments[0] === node
              ) {
                  return node.value;
              }
  
              return null;
          }
  
          return {
              Literal(node) {
                  const pattern = getRegExpPattern(node);
  
                  if (pattern) {
                      const controlCharacters = collector.collectControlChars(pattern);
  
                      if (controlCharacters.length > 0) {
                          context.report({
                              node,
                              messageId: "unexpected",
                              data: {
                                  controlChars: controlCharacters.join(", ")
                              }
                          });
                      }
                  }
              }
          };
  
      }
  };