123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- "use strict";
- const { CALL, CONSTRUCT, ReferenceTracker, getStringIfConstant } = require("@eslint-community/eslint-utils");
- const { RegExpParser, visitRegExpAST } = require("@eslint-community/regexpp");
- const parser = new RegExpParser();
- function getPathToRoot(node) {
- const path = [];
- let current = node;
- do {
- path.push(current);
- current = current.parent;
- } while (current);
- return path;
- }
- function isLookaround(node) {
- return node.type === "Assertion" &&
- (node.kind === "lookahead" || node.kind === "lookbehind");
- }
- function isNegativeLookaround(node) {
- return isLookaround(node) && node.negate;
- }
- module.exports = {
- meta: {
- type: "problem",
- docs: {
- description: "Disallow useless backreferences in regular expressions",
- recommended: true,
- url: "https://eslint.org/docs/latest/rules/no-useless-backreference"
- },
- schema: [],
- messages: {
- nested: "Backreference '{{ bref }}' will be ignored. It references group '{{ group }}'{{ otherGroups }} from within that group.",
- forward: "Backreference '{{ bref }}' will be ignored. It references group '{{ group }}'{{ otherGroups }} which appears later in the pattern.",
- backward: "Backreference '{{ bref }}' will be ignored. It references group '{{ group }}'{{ otherGroups }} which appears before in the same lookbehind.",
- disjunctive: "Backreference '{{ bref }}' will be ignored. It references group '{{ group }}'{{ otherGroups }} which is in another alternative.",
- intoNegativeLookaround: "Backreference '{{ bref }}' will be ignored. It references group '{{ group }}'{{ otherGroups }} which is in a negative lookaround."
- }
- },
- create(context) {
- const sourceCode = context.sourceCode;
-
- function checkRegex(node, pattern, flags) {
- let regExpAST;
- try {
- regExpAST = parser.parsePattern(pattern, 0, pattern.length, { unicode: flags.includes("u"), unicodeSets: flags.includes("v") });
- } catch {
-
- return;
- }
- visitRegExpAST(regExpAST, {
- onBackreferenceEnter(bref) {
- const groups = [bref.resolved].flat(),
- brefPath = getPathToRoot(bref);
- const problems = groups.map(group => {
- const groupPath = getPathToRoot(group);
- if (brefPath.includes(group)) {
-
- return {
- messageId: "nested",
- group
- };
- }
-
- let i = brefPath.length - 1,
- j = groupPath.length - 1;
- do {
- i--;
- j--;
- } while (brefPath[i] === groupPath[j]);
- const indexOfLowestCommonAncestor = j + 1,
- groupCut = groupPath.slice(0, indexOfLowestCommonAncestor),
- commonPath = groupPath.slice(indexOfLowestCommonAncestor),
- lowestCommonLookaround = commonPath.find(isLookaround),
- isMatchingBackward = lowestCommonLookaround && lowestCommonLookaround.kind === "lookbehind";
- if (groupCut.at(-1).type === "Alternative") {
-
- return {
- messageId: "disjunctive",
- group
- };
- }
- if (!isMatchingBackward && bref.end <= group.start) {
-
- return {
- messageId: "forward",
- group
- };
- }
- if (isMatchingBackward && group.end <= bref.start) {
-
- return {
- messageId: "backward",
- group
- };
- }
- if (groupCut.some(isNegativeLookaround)) {
-
- return {
- messageId: "intoNegativeLookaround",
- group
- };
- }
- return null;
- });
- if (problems.length === 0 || problems.some(problem => !problem)) {
-
- return;
- }
- let problemsToReport;
-
- const problemsInSameDisjunction = problems.filter(problem => problem.messageId !== "disjunctive");
- if (problemsInSameDisjunction.length) {
-
- problemsToReport = problemsInSameDisjunction;
- } else {
-
- problemsToReport = problems;
- }
- const [{ messageId, group }, ...other] = problemsToReport;
- let otherGroups = "";
- if (other.length === 1) {
- otherGroups = " and another group";
- } else if (other.length > 1) {
- otherGroups = ` and other ${other.length} groups`;
- }
- context.report({
- node,
- messageId,
- data: {
- bref: bref.raw,
- group: group.raw,
- otherGroups
- }
- });
- }
- });
- }
- return {
- "Literal[regex]"(node) {
- const { pattern, flags } = node.regex;
- checkRegex(node, pattern, flags);
- },
- Program(node) {
- const scope = sourceCode.getScope(node),
- tracker = new ReferenceTracker(scope),
- traceMap = {
- RegExp: {
- [CALL]: true,
- [CONSTRUCT]: true
- }
- };
- for (const { node: refNode } of tracker.iterateGlobalReferences(traceMap)) {
- const [patternNode, flagsNode] = refNode.arguments,
- pattern = getStringIfConstant(patternNode, scope),
- flags = getStringIfConstant(flagsNode, scope);
- if (typeof pattern === "string") {
- checkRegex(refNode, pattern, flags || "");
- }
- }
- }
- };
- }
- };
|