Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 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 | |
| 12 | module.exports = { |
| 13 | meta: { |
| 14 | type: "suggestion", |
| 15 | |
| 16 | docs: { |
| 17 | description: "disallow division operators explicitly at the beginning of regular expressions", |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 18 | recommended: false, |
| 19 | url: "https://eslint.org/docs/rules/no-div-regex" |
| 20 | }, |
| 21 | |
| 22 | fixable: "code", |
| 23 | |
| 24 | schema: [], |
| 25 | |
| 26 | messages: { |
| 27 | unexpected: "A regular expression literal can be confused with '/='." |
| 28 | } |
| 29 | }, |
| 30 | |
| 31 | create(context) { |
| 32 | const sourceCode = context.getSourceCode(); |
| 33 | |
| 34 | return { |
| 35 | |
| 36 | Literal(node) { |
| 37 | const token = sourceCode.getFirstToken(node); |
| 38 | |
| 39 | if (token.type === "RegularExpression" && token.value[1] === "=") { |
| 40 | context.report({ |
| 41 | node, |
| 42 | messageId: "unexpected", |
| 43 | fix(fixer) { |
| 44 | return fixer.replaceTextRange([token.range[0] + 1, token.range[0] + 2], "[=]"); |
| 45 | } |
| 46 | }); |
| 47 | } |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | } |
| 52 | }; |