• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * @fileoverview Rule to check for ambiguous div operator in regexes
3 * @author Matt DuVall <http://www.mattduvall.com>
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Rule Definition
10//------------------------------------------------------------------------------
11
12module.exports = {
13    meta: {
14        type: "suggestion",
15
16        docs: {
17            description: "disallow division operators explicitly at the beginning of regular expressions",
18            category: "Best Practices",
19            recommended: false,
20            url: "https://eslint.org/docs/rules/no-div-regex"
21        },
22
23        fixable: "code",
24
25        schema: [],
26
27        messages: {
28            unexpected: "A regular expression literal can be confused with '/='."
29        }
30    },
31
32    create(context) {
33        const sourceCode = context.getSourceCode();
34
35        return {
36
37            Literal(node) {
38                const token = sourceCode.getFirstToken(node);
39
40                if (token.type === "RegularExpression" && token.value[1] === "=") {
41                    context.report({
42                        node,
43                        messageId: "unexpected",
44                        fix(fixer) {
45                            return fixer.replaceTextRange([token.range[0] + 1, token.range[0] + 2], "[=]");
46                        }
47                    });
48                }
49            }
50        };
51
52    }
53};
54