Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

no-regex-spaces.js 3.7KB

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