no-useless-backreference.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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-utils");
  10. const { RegExpParser, visitRegExpAST } = require("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. module.exports = {
  50. meta: {
  51. type: "problem",
  52. docs: {
  53. description: "disallow useless backreferences in regular expressions",
  54. category: "Possible Errors",
  55. recommended: false,
  56. url: "https://eslint.org/docs/rules/no-useless-backreference"
  57. },
  58. schema: [],
  59. messages: {
  60. nested: "Backreference '{{ bref }}' will be ignored. It references group '{{ group }}' from within that group.",
  61. forward: "Backreference '{{ bref }}' will be ignored. It references group '{{ group }}' which appears later in the pattern.",
  62. backward: "Backreference '{{ bref }}' will be ignored. It references group '{{ group }}' which appears before in the same lookbehind.",
  63. disjunctive: "Backreference '{{ bref }}' will be ignored. It references group '{{ group }}' which is in another alternative.",
  64. intoNegativeLookaround: "Backreference '{{ bref }}' will be ignored. It references group '{{ group }}' which is in a negative lookaround."
  65. }
  66. },
  67. create(context) {
  68. /**
  69. * Checks and reports useless backreferences in the given regular expression.
  70. * @param {ASTNode} node Node that represents regular expression. A regex literal or RegExp constructor call.
  71. * @param {string} pattern Regular expression pattern.
  72. * @param {string} flags Regular expression flags.
  73. * @returns {void}
  74. */
  75. function checkRegex(node, pattern, flags) {
  76. let regExpAST;
  77. try {
  78. regExpAST = parser.parsePattern(pattern, 0, pattern.length, flags.includes("u"));
  79. } catch {
  80. // Ignore regular expressions with syntax errors
  81. return;
  82. }
  83. visitRegExpAST(regExpAST, {
  84. onBackreferenceEnter(bref) {
  85. const group = bref.resolved,
  86. brefPath = getPathToRoot(bref),
  87. groupPath = getPathToRoot(group);
  88. let messageId = null;
  89. if (brefPath.includes(group)) {
  90. // group is bref's ancestor => bref is nested ('nested reference') => group hasn't matched yet when bref starts to match.
  91. messageId = "nested";
  92. } else {
  93. // Start from the root to find the lowest common ancestor.
  94. let i = brefPath.length - 1,
  95. j = groupPath.length - 1;
  96. do {
  97. i--;
  98. j--;
  99. } while (brefPath[i] === groupPath[j]);
  100. const indexOfLowestCommonAncestor = j + 1,
  101. groupCut = groupPath.slice(0, indexOfLowestCommonAncestor),
  102. commonPath = groupPath.slice(indexOfLowestCommonAncestor),
  103. lowestCommonLookaround = commonPath.find(isLookaround),
  104. isMatchingBackward = lowestCommonLookaround && lowestCommonLookaround.kind === "lookbehind";
  105. if (!isMatchingBackward && bref.end <= group.start) {
  106. // bref is left, group is right ('forward reference') => group hasn't matched yet when bref starts to match.
  107. messageId = "forward";
  108. } else if (isMatchingBackward && group.end <= bref.start) {
  109. // the opposite of the previous when the regex is matching backward in a lookbehind context.
  110. messageId = "backward";
  111. } else if (groupCut[groupCut.length - 1].type === "Alternative") {
  112. // group's and bref's ancestor nodes below the lowest common ancestor are sibling alternatives => they're disjunctive.
  113. messageId = "disjunctive";
  114. } else if (groupCut.some(isNegativeLookaround)) {
  115. // group is in a negative lookaround which isn't bref's ancestor => group has already failed when bref starts to match.
  116. messageId = "intoNegativeLookaround";
  117. }
  118. }
  119. if (messageId) {
  120. context.report({
  121. node,
  122. messageId,
  123. data: {
  124. bref: bref.raw,
  125. group: group.raw
  126. }
  127. });
  128. }
  129. }
  130. });
  131. }
  132. return {
  133. "Literal[regex]"(node) {
  134. const { pattern, flags } = node.regex;
  135. checkRegex(node, pattern, flags);
  136. },
  137. Program() {
  138. const scope = context.getScope(),
  139. tracker = new ReferenceTracker(scope),
  140. traceMap = {
  141. RegExp: {
  142. [CALL]: true,
  143. [CONSTRUCT]: true
  144. }
  145. };
  146. for (const { node } of tracker.iterateGlobalReferences(traceMap)) {
  147. const [patternNode, flagsNode] = node.arguments,
  148. pattern = getStringIfConstant(patternNode, scope),
  149. flags = getStringIfConstant(flagsNode, scope);
  150. if (typeof pattern === "string") {
  151. checkRegex(node, pattern, flags || "");
  152. }
  153. }
  154. }
  155. };
  156. }
  157. };