2024-08-09 12:04:48 +00:00
|
|
|
/**
|
|
|
|
* @fileoverview Rule to flag when regex literals are not wrapped in parens
|
|
|
|
* @author Matt DuVall <http://www.mattduvall.com>
|
2024-08-21 06:34:30 +00:00
|
|
|
* @deprecated in ESLint v8.53.0
|
2024-08-09 12:04:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Rule Definition
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2024-08-21 06:34:30 +00:00
|
|
|
/** @type {import('../shared/types').Rule} */
|
2024-08-09 12:04:48 +00:00
|
|
|
module.exports = {
|
|
|
|
meta: {
|
2024-08-21 06:34:30 +00:00
|
|
|
deprecated: true,
|
|
|
|
replacedBy: [],
|
2024-08-09 12:04:48 +00:00
|
|
|
type: "layout",
|
|
|
|
|
|
|
|
docs: {
|
2024-08-21 06:34:30 +00:00
|
|
|
description: "Require parenthesis around regex literals",
|
2024-08-09 12:04:48 +00:00
|
|
|
recommended: false,
|
2024-08-21 06:34:30 +00:00
|
|
|
url: "https://eslint.org/docs/latest/rules/wrap-regex"
|
2024-08-09 12:04:48 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
schema: [],
|
|
|
|
fixable: "code",
|
|
|
|
|
|
|
|
messages: {
|
|
|
|
requireParens: "Wrap the regexp literal in parens to disambiguate the slash."
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
create(context) {
|
2024-08-21 06:34:30 +00:00
|
|
|
const sourceCode = context.sourceCode;
|
2024-08-09 12:04:48 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
Literal(node) {
|
|
|
|
const token = sourceCode.getFirstToken(node),
|
|
|
|
nodeType = token.type;
|
|
|
|
|
|
|
|
if (nodeType === "RegularExpression") {
|
|
|
|
const beforeToken = sourceCode.getTokenBefore(node);
|
|
|
|
const afterToken = sourceCode.getTokenAfter(node);
|
2024-08-21 06:34:30 +00:00
|
|
|
const { parent } = node;
|
2024-08-09 12:04:48 +00:00
|
|
|
|
2024-08-21 06:34:30 +00:00
|
|
|
if (parent.type === "MemberExpression" && parent.object === node &&
|
2024-08-09 12:04:48 +00:00
|
|
|
!(beforeToken && beforeToken.value === "(" && afterToken && afterToken.value === ")")) {
|
|
|
|
context.report({
|
|
|
|
node,
|
|
|
|
messageId: "requireParens",
|
|
|
|
fix: fixer => fixer.replaceText(node, `(${sourceCode.getText(node)})`)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|