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 | |
Tim van der Lippe | 0ceb465 | 2022-01-06 14:23:36 +0100 | [diff] [blame^] | 12 | /** @type {import('../shared/types').Rule} */ |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 13 | module.exports = { |
| 14 | meta: { |
| 15 | type: "suggestion", |
| 16 | |
| 17 | docs: { |
| 18 | description: "disallow division operators explicitly at the beginning of regular expressions", |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 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 | }; |