prefer-named-capture-group.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. // Rule Definition
  22. //------------------------------------------------------------------------------
  23. module.exports = {
  24. meta: {
  25. type: "suggestion",
  26. docs: {
  27. description: "enforce using named capture group in regular expression",
  28. category: "Best Practices",
  29. recommended: false,
  30. url: "https://eslint.org/docs/rules/prefer-named-capture-group"
  31. },
  32. schema: [],
  33. messages: {
  34. required: "Capture group '{{group}}' should be converted to a named or non-capturing group."
  35. }
  36. },
  37. create(context) {
  38. /**
  39. * Function to check regular expression.
  40. *
  41. * @param {string} regex The regular expression to be check.
  42. * @param {ASTNode} node AST node which contains regular expression.
  43. * @param {boolean} uFlag Flag indicates whether unicode mode is enabled or not.
  44. * @returns {void}
  45. */
  46. function checkRegex(regex, node, uFlag) {
  47. let ast;
  48. try {
  49. ast = parser.parsePattern(regex, 0, regex.length, uFlag);
  50. } catch (_) {
  51. // ignore regex syntax errors
  52. return;
  53. }
  54. regexpp.visitRegExpAST(ast, {
  55. onCapturingGroupEnter(group) {
  56. if (!group.name) {
  57. const locNode = node.type === "Literal" ? node : node.arguments[0];
  58. context.report({
  59. node,
  60. messageId: "required",
  61. loc: {
  62. start: {
  63. line: locNode.loc.start.line,
  64. column: locNode.loc.start.column + group.start + 1
  65. },
  66. end: {
  67. line: locNode.loc.start.line,
  68. column: locNode.loc.start.column + group.end + 1
  69. }
  70. },
  71. data: {
  72. group: group.raw
  73. }
  74. });
  75. }
  76. }
  77. });
  78. }
  79. return {
  80. Literal(node) {
  81. if (node.regex) {
  82. checkRegex(node.regex.pattern, node, node.regex.flags.includes("u"));
  83. }
  84. },
  85. Program() {
  86. const scope = context.getScope();
  87. const tracker = new ReferenceTracker(scope);
  88. const traceMap = {
  89. RegExp: {
  90. [CALL]: true,
  91. [CONSTRUCT]: true
  92. }
  93. };
  94. for (const { node } of tracker.iterateGlobalReferences(traceMap)) {
  95. const regex = getStringIfConstant(node.arguments[0]);
  96. const flags = getStringIfConstant(node.arguments[1]);
  97. if (regex) {
  98. checkRegex(regex, node, flags && flags.includes("u"));
  99. }
  100. }
  101. }
  102. };
  103. }
  104. };