prefer-named-capture-group.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * @fileoverview Rule to enforce requiring named capture groups in regular expression.
  3. * @author Pig Fang <https://github.com/g-plane>
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const {
  10. CALL,
  11. CONSTRUCT,
  12. ReferenceTracker,
  13. getStringIfConstant
  14. } = require("eslint-utils");
  15. const regexpp = require("regexpp");
  16. //------------------------------------------------------------------------------
  17. // Helpers
  18. //------------------------------------------------------------------------------
  19. const parser = new regexpp.RegExpParser();
  20. /**
  21. * Creates fixer suggestions for the regex, if statically determinable.
  22. * @param {number} groupStart Starting index of the regex group.
  23. * @param {string} pattern The regular expression pattern to be checked.
  24. * @param {string} rawText Source text of the regexNode.
  25. * @param {ASTNode} regexNode AST node which contains the regular expression.
  26. * @returns {Array<SuggestionResult>} Fixer suggestions for the regex, if statically determinable.
  27. */
  28. function suggestIfPossible(groupStart, pattern, rawText, regexNode) {
  29. switch (regexNode.type) {
  30. case "Literal":
  31. if (typeof regexNode.value === "string" && rawText.includes("\\")) {
  32. return null;
  33. }
  34. break;
  35. case "TemplateLiteral":
  36. if (regexNode.expressions.length || rawText.slice(1, -1) !== pattern) {
  37. return null;
  38. }
  39. break;
  40. default:
  41. return null;
  42. }
  43. const start = regexNode.range[0] + groupStart + 2;
  44. return [
  45. {
  46. fix(fixer) {
  47. const existingTemps = pattern.match(/temp\d+/gu) || [];
  48. const highestTempCount = existingTemps.reduce(
  49. (previous, next) =>
  50. Math.max(previous, Number(next.slice("temp".length))),
  51. 0
  52. );
  53. return fixer.insertTextBeforeRange(
  54. [start, start],
  55. `?<temp${highestTempCount + 1}>`
  56. );
  57. },
  58. messageId: "addGroupName"
  59. },
  60. {
  61. fix(fixer) {
  62. return fixer.insertTextBeforeRange(
  63. [start, start],
  64. "?:"
  65. );
  66. },
  67. messageId: "addNonCapture"
  68. }
  69. ];
  70. }
  71. //------------------------------------------------------------------------------
  72. // Rule Definition
  73. //------------------------------------------------------------------------------
  74. /** @type {import('../shared/types').Rule} */
  75. module.exports = {
  76. meta: {
  77. type: "suggestion",
  78. docs: {
  79. description: "Enforce using named capture group in regular expression",
  80. recommended: false,
  81. url: "https://eslint.org/docs/rules/prefer-named-capture-group"
  82. },
  83. hasSuggestions: true,
  84. schema: [],
  85. messages: {
  86. addGroupName: "Add name to capture group.",
  87. addNonCapture: "Convert group to non-capturing.",
  88. required: "Capture group '{{group}}' should be converted to a named or non-capturing group."
  89. }
  90. },
  91. create(context) {
  92. const sourceCode = context.getSourceCode();
  93. /**
  94. * Function to check regular expression.
  95. * @param {string} pattern The regular expression pattern to be checked.
  96. * @param {ASTNode} node AST node which contains the regular expression or a call/new expression.
  97. * @param {ASTNode} regexNode AST node which contains the regular expression.
  98. * @param {boolean} uFlag Flag indicates whether unicode mode is enabled or not.
  99. * @returns {void}
  100. */
  101. function checkRegex(pattern, node, regexNode, uFlag) {
  102. let ast;
  103. try {
  104. ast = parser.parsePattern(pattern, 0, pattern.length, uFlag);
  105. } catch {
  106. // ignore regex syntax errors
  107. return;
  108. }
  109. regexpp.visitRegExpAST(ast, {
  110. onCapturingGroupEnter(group) {
  111. if (!group.name) {
  112. const rawText = sourceCode.getText(regexNode);
  113. const suggest = suggestIfPossible(group.start, pattern, rawText, regexNode);
  114. context.report({
  115. node,
  116. messageId: "required",
  117. data: {
  118. group: group.raw
  119. },
  120. suggest
  121. });
  122. }
  123. }
  124. });
  125. }
  126. return {
  127. Literal(node) {
  128. if (node.regex) {
  129. checkRegex(node.regex.pattern, node, node, node.regex.flags.includes("u"));
  130. }
  131. },
  132. Program() {
  133. const scope = context.getScope();
  134. const tracker = new ReferenceTracker(scope);
  135. const traceMap = {
  136. RegExp: {
  137. [CALL]: true,
  138. [CONSTRUCT]: true
  139. }
  140. };
  141. for (const { node } of tracker.iterateGlobalReferences(traceMap)) {
  142. const regex = getStringIfConstant(node.arguments[0]);
  143. const flags = getStringIfConstant(node.arguments[1]);
  144. if (regex) {
  145. checkRegex(regex, node, node.arguments[0], flags && flags.includes("u"));
  146. }
  147. }
  148. }
  149. };
  150. }
  151. };