no-useless-backreference.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /**
  2. * @fileoverview Rule to disallow useless backreferences in regular expressions
  3. * @author Milos Djermanovic
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const { CALL, CONSTRUCT, ReferenceTracker, getStringIfConstant } = require("@eslint-community/eslint-utils");
  10. const { RegExpParser, visitRegExpAST } = require("@eslint-community/regexpp");
  11. //------------------------------------------------------------------------------
  12. // Helpers
  13. //------------------------------------------------------------------------------
  14. const parser = new RegExpParser();
  15. /**
  16. * Finds the path from the given `regexpp` AST node to the root node.
  17. * @param {regexpp.Node} node Node.
  18. * @returns {regexpp.Node[]} Array that starts with the given node and ends with the root node.
  19. */
  20. function getPathToRoot(node) {
  21. const path = [];
  22. let current = node;
  23. do {
  24. path.push(current);
  25. current = current.parent;
  26. } while (current);
  27. return path;
  28. }
  29. /**
  30. * Determines whether the given `regexpp` AST node is a lookaround node.
  31. * @param {regexpp.Node} node Node.
  32. * @returns {boolean} `true` if it is a lookaround node.
  33. */
  34. function isLookaround(node) {
  35. return node.type === "Assertion" &&
  36. (node.kind === "lookahead" || node.kind === "lookbehind");
  37. }
  38. /**
  39. * Determines whether the given `regexpp` AST node is a negative lookaround node.
  40. * @param {regexpp.Node} node Node.
  41. * @returns {boolean} `true` if it is a negative lookaround node.
  42. */
  43. function isNegativeLookaround(node) {
  44. return isLookaround(node) && node.negate;
  45. }
  46. //------------------------------------------------------------------------------
  47. // Rule Definition
  48. //------------------------------------------------------------------------------
  49. /** @type {import('../shared/types').Rule} */
  50. module.exports = {
  51. meta: {
  52. type: "problem",
  53. docs: {
  54. description: "Disallow useless backreferences in regular expressions",
  55. recommended: true,
  56. url: "https://eslint.org/docs/latest/rules/no-useless-backreference"
  57. },
  58. schema: [],
  59. messages: {
  60. nested: "Backreference '{{ bref }}' will be ignored. It references group '{{ group }}'{{ otherGroups }} from within that group.",
  61. forward: "Backreference '{{ bref }}' will be ignored. It references group '{{ group }}'{{ otherGroups }} which appears later in the pattern.",
  62. backward: "Backreference '{{ bref }}' will be ignored. It references group '{{ group }}'{{ otherGroups }} which appears before in the same lookbehind.",
  63. disjunctive: "Backreference '{{ bref }}' will be ignored. It references group '{{ group }}'{{ otherGroups }} which is in another alternative.",
  64. intoNegativeLookaround: "Backreference '{{ bref }}' will be ignored. It references group '{{ group }}'{{ otherGroups }} which is in a negative lookaround."
  65. }
  66. },
  67. create(context) {
  68. const sourceCode = context.sourceCode;
  69. /**
  70. * Checks and reports useless backreferences in the given regular expression.
  71. * @param {ASTNode} node Node that represents regular expression. A regex literal or RegExp constructor call.
  72. * @param {string} pattern Regular expression pattern.
  73. * @param {string} flags Regular expression flags.
  74. * @returns {void}
  75. */
  76. function checkRegex(node, pattern, flags) {
  77. let regExpAST;
  78. try {
  79. regExpAST = parser.parsePattern(pattern, 0, pattern.length, { unicode: flags.includes("u"), unicodeSets: flags.includes("v") });
  80. } catch {
  81. // Ignore regular expressions with syntax errors
  82. return;
  83. }
  84. visitRegExpAST(regExpAST, {
  85. onBackreferenceEnter(bref) {
  86. const groups = [bref.resolved].flat(),
  87. brefPath = getPathToRoot(bref);
  88. const problems = groups.map(group => {
  89. const groupPath = getPathToRoot(group);
  90. if (brefPath.includes(group)) {
  91. // group is bref's ancestor => bref is nested ('nested reference') => group hasn't matched yet when bref starts to match.
  92. return {
  93. messageId: "nested",
  94. group
  95. };
  96. }
  97. // Start from the root to find the lowest common ancestor.
  98. let i = brefPath.length - 1,
  99. j = groupPath.length - 1;
  100. do {
  101. i--;
  102. j--;
  103. } while (brefPath[i] === groupPath[j]);
  104. const indexOfLowestCommonAncestor = j + 1,
  105. groupCut = groupPath.slice(0, indexOfLowestCommonAncestor),
  106. commonPath = groupPath.slice(indexOfLowestCommonAncestor),
  107. lowestCommonLookaround = commonPath.find(isLookaround),
  108. isMatchingBackward = lowestCommonLookaround && lowestCommonLookaround.kind === "lookbehind";
  109. if (groupCut.at(-1).type === "Alternative") {
  110. // group's and bref's ancestor nodes below the lowest common ancestor are sibling alternatives => they're disjunctive.
  111. return {
  112. messageId: "disjunctive",
  113. group
  114. };
  115. }
  116. if (!isMatchingBackward && bref.end <= group.start) {
  117. // bref is left, group is right ('forward reference') => group hasn't matched yet when bref starts to match.
  118. return {
  119. messageId: "forward",
  120. group
  121. };
  122. }
  123. if (isMatchingBackward && group.end <= bref.start) {
  124. // the opposite of the previous when the regex is matching backward in a lookbehind context.
  125. return {
  126. messageId: "backward",
  127. group
  128. };
  129. }
  130. if (groupCut.some(isNegativeLookaround)) {
  131. // group is in a negative lookaround which isn't bref's ancestor => group has already failed when bref starts to match.
  132. return {
  133. messageId: "intoNegativeLookaround",
  134. group
  135. };
  136. }
  137. return null;
  138. });
  139. if (problems.length === 0 || problems.some(problem => !problem)) {
  140. // If there are no problems or no problems with any group then do not report it.
  141. return;
  142. }
  143. let problemsToReport;
  144. // Gets problems that appear in the same disjunction.
  145. const problemsInSameDisjunction = problems.filter(problem => problem.messageId !== "disjunctive");
  146. if (problemsInSameDisjunction.length) {
  147. // Only report problems that appear in the same disjunction.
  148. problemsToReport = problemsInSameDisjunction;
  149. } else {
  150. // If all groups appear in different disjunctions, report it.
  151. problemsToReport = problems;
  152. }
  153. const [{ messageId, group }, ...other] = problemsToReport;
  154. let otherGroups = "";
  155. if (other.length === 1) {
  156. otherGroups = " and another group";
  157. } else if (other.length > 1) {
  158. otherGroups = ` and other ${other.length} groups`;
  159. }
  160. context.report({
  161. node,
  162. messageId,
  163. data: {
  164. bref: bref.raw,
  165. group: group.raw,
  166. otherGroups
  167. }
  168. });
  169. }
  170. });
  171. }
  172. return {
  173. "Literal[regex]"(node) {
  174. const { pattern, flags } = node.regex;
  175. checkRegex(node, pattern, flags);
  176. },
  177. Program(node) {
  178. const scope = sourceCode.getScope(node),
  179. tracker = new ReferenceTracker(scope),
  180. traceMap = {
  181. RegExp: {
  182. [CALL]: true,
  183. [CONSTRUCT]: true
  184. }
  185. };
  186. for (const { node: refNode } of tracker.iterateGlobalReferences(traceMap)) {
  187. const [patternNode, flagsNode] = refNode.arguments,
  188. pattern = getStringIfConstant(patternNode, scope),
  189. flags = getStringIfConstant(flagsNode, scope);
  190. if (typeof pattern === "string") {
  191. checkRegex(refNode, pattern, flags || "");
  192. }
  193. }
  194. }
  195. };
  196. }
  197. };