UNPKG

1.19 kBJavaScriptView Raw
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 schema: [],
24
25 messages: {
26 unexpected: "A regular expression literal can be confused with '/='."
27 }
28 },
29
30 create(context) {
31 const sourceCode = context.getSourceCode();
32
33 return {
34
35 Literal(node) {
36 const token = sourceCode.getFirstToken(node);
37
38 if (token.type === "RegularExpression" && token.value[1] === "=") {
39 context.report({ node, messageId: "unexpected" });
40 }
41 }
42 };
43
44 }
45};