Blame view
node_modules/eslint/lib/rules/wrap-regex.js
1.57 KB
f7563de62
|
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 |
/** * @fileoverview Rule to flag when regex literals are not wrapped in parens * @author Matt DuVall <http://www.mattduvall.com> */ "use strict"; //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ module.exports = { meta: { docs: { description: "require parenthesis around regex literals", category: "Stylistic Issues", recommended: false }, schema: [], fixable: "code" }, create(context) { const sourceCode = context.getSourceCode(); return { Literal(node) { const token = sourceCode.getFirstToken(node), nodeType = token.type; if (nodeType === "RegularExpression") { const source = sourceCode.getTokenBefore(node); const ancestors = context.getAncestors(); const grandparent = ancestors[ancestors.length - 1]; if (grandparent.type === "MemberExpression" && grandparent.object === node && (!source || source.value !== "(")) { context.report({ node, message: "Wrap the regexp literal in parens to disambiguate the slash.", fix: fixer => fixer.replaceText(node, `(${sourceCode.getText(node)})`) }); } } } }; } }; |