no-regex-spaces.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * @fileoverview Rule to count multiple spaces in regular expressions
  3. * @author Matt DuVall <http://www.mattduvall.com/>
  4. */
  5. "use strict";
  6. const astUtils = require("../ast-utils");
  7. //------------------------------------------------------------------------------
  8. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. module.exports = {
  11. meta: {
  12. docs: {
  13. description: "disallow multiple spaces in regular expressions",
  14. category: "Possible Errors",
  15. recommended: true,
  16. url: "https://eslint.org/docs/rules/no-regex-spaces"
  17. },
  18. schema: [],
  19. fixable: "code"
  20. },
  21. create(context) {
  22. const sourceCode = context.getSourceCode();
  23. /**
  24. * Validate regular expressions
  25. * @param {ASTNode} node node to validate
  26. * @param {string} value regular expression to validate
  27. * @param {number} valueStart The start location of the regex/string literal. It will always be the case that
  28. * `sourceCode.getText().slice(valueStart, valueStart + value.length) === value`
  29. * @returns {void}
  30. * @private
  31. */
  32. function checkRegex(node, value, valueStart) {
  33. const multipleSpacesRegex = /( {2,})( [+*{?]|[^+*{?]|$)/,
  34. regexResults = multipleSpacesRegex.exec(value);
  35. if (regexResults !== null) {
  36. const count = regexResults[1].length;
  37. context.report({
  38. node,
  39. message: "Spaces are hard to count. Use {{{count}}}.",
  40. data: { count },
  41. fix(fixer) {
  42. return fixer.replaceTextRange(
  43. [valueStart + regexResults.index, valueStart + regexResults.index + count],
  44. ` {${count}}`
  45. );
  46. }
  47. });
  48. /*
  49. * TODO: (platinumazure) Fix message to use rule message
  50. * substitution when api.report is fixed in lib/eslint.js.
  51. */
  52. }
  53. }
  54. /**
  55. * Validate regular expression literals
  56. * @param {ASTNode} node node to validate
  57. * @returns {void}
  58. * @private
  59. */
  60. function checkLiteral(node) {
  61. const token = sourceCode.getFirstToken(node),
  62. nodeType = token.type,
  63. nodeValue = token.value;
  64. if (nodeType === "RegularExpression") {
  65. checkRegex(node, nodeValue, token.range[0]);
  66. }
  67. }
  68. /**
  69. * Check if node is a string
  70. * @param {ASTNode} node node to evaluate
  71. * @returns {boolean} True if its a string
  72. * @private
  73. */
  74. function isString(node) {
  75. return node && node.type === "Literal" && typeof node.value === "string";
  76. }
  77. /**
  78. * Validate strings passed to the RegExp constructor
  79. * @param {ASTNode} node node to validate
  80. * @returns {void}
  81. * @private
  82. */
  83. function checkFunction(node) {
  84. const scope = context.getScope();
  85. const regExpVar = astUtils.getVariableByName(scope, "RegExp");
  86. const shadowed = regExpVar && regExpVar.defs.length > 0;
  87. if (node.callee.type === "Identifier" && node.callee.name === "RegExp" && isString(node.arguments[0]) && !shadowed) {
  88. checkRegex(node, node.arguments[0].value, node.arguments[0].range[0] + 1);
  89. }
  90. }
  91. return {
  92. Literal: checkLiteral,
  93. CallExpression: checkFunction,
  94. NewExpression: checkFunction
  95. };
  96. }
  97. };