prefer-named-capture-group.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. * @param {string} pattern The regular expression pattern to be check.
  41. * @param {ASTNode} node AST node which contains regular expression.
  42. * @param {boolean} uFlag Flag indicates whether unicode mode is enabled or not.
  43. * @returns {void}
  44. */
  45. function checkRegex(pattern, node, uFlag) {
  46. let ast;
  47. try {
  48. ast = parser.parsePattern(pattern, 0, pattern.length, uFlag);
  49. } catch {
  50. // ignore regex syntax errors
  51. return;
  52. }
  53. regexpp.visitRegExpAST(ast, {
  54. onCapturingGroupEnter(group) {
  55. if (!group.name) {
  56. context.report({
  57. node,
  58. messageId: "required",
  59. data: {
  60. group: group.raw
  61. }
  62. });
  63. }
  64. }
  65. });
  66. }
  67. return {
  68. Literal(node) {
  69. if (node.regex) {
  70. checkRegex(node.regex.pattern, node, node.regex.flags.includes("u"));
  71. }
  72. },
  73. Program() {
  74. const scope = context.getScope();
  75. const tracker = new ReferenceTracker(scope);
  76. const traceMap = {
  77. RegExp: {
  78. [CALL]: true,
  79. [CONSTRUCT]: true
  80. }
  81. };
  82. for (const { node } of tracker.iterateGlobalReferences(traceMap)) {
  83. const regex = getStringIfConstant(node.arguments[0]);
  84. const flags = getStringIfConstant(node.arguments[1]);
  85. if (regex) {
  86. checkRegex(regex, node, flags && flags.includes("u"));
  87. }
  88. }
  89. }
  90. };
  91. }
  92. };