/node_modules/eslint/lib/rules/no-multi-spaces.js

https://gitlab.com/sdabi/execom · JavaScript · 144 lines · 91 code · 24 blank · 29 comment · 16 complexity · 2ecb3f1d39aa86c0daf0456b365ba817 MD5 · raw file

  1. /**
  2. * @fileoverview Disallow use of multiple spaces.
  3. * @author Nicholas C. Zakas
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. docs: {
  12. description: "disallow multiple spaces",
  13. category: "Best Practices",
  14. recommended: false
  15. },
  16. fixable: "whitespace",
  17. schema: [
  18. {
  19. type: "object",
  20. properties: {
  21. exceptions: {
  22. type: "object",
  23. patternProperties: {
  24. "^([A-Z][a-z]*)+$": {
  25. type: "boolean"
  26. }
  27. },
  28. additionalProperties: false
  29. }
  30. },
  31. additionalProperties: false
  32. }
  33. ]
  34. },
  35. create: function(context) {
  36. // the index of the last comment that was checked
  37. const exceptions = { Property: true },
  38. options = context.options[0];
  39. let hasExceptions = true,
  40. lastCommentIndex = 0;
  41. if (options && options.exceptions) {
  42. Object.keys(options.exceptions).forEach(function(key) {
  43. if (options.exceptions[key]) {
  44. exceptions[key] = true;
  45. } else {
  46. delete exceptions[key];
  47. }
  48. });
  49. hasExceptions = Object.keys(exceptions).length > 0;
  50. }
  51. /**
  52. * Determines if a given source index is in a comment or not by checking
  53. * the index against the comment range. Since the check goes straight
  54. * through the file, once an index is passed a certain comment, we can
  55. * go to the next comment to check that.
  56. * @param {int} index The source index to check.
  57. * @param {ASTNode[]} comments An array of comment nodes.
  58. * @returns {boolean} True if the index is within a comment, false if not.
  59. * @private
  60. */
  61. function isIndexInComment(index, comments) {
  62. while (lastCommentIndex < comments.length) {
  63. const comment = comments[lastCommentIndex];
  64. if (comment.range[0] <= index && index < comment.range[1]) {
  65. return true;
  66. } else if (index > comment.range[1]) {
  67. lastCommentIndex++;
  68. } else {
  69. break;
  70. }
  71. }
  72. return false;
  73. }
  74. //--------------------------------------------------------------------------
  75. // Public
  76. //--------------------------------------------------------------------------
  77. return {
  78. Program: function() {
  79. const sourceCode = context.getSourceCode(),
  80. source = sourceCode.getText(),
  81. allComments = sourceCode.getAllComments(),
  82. pattern = /[^\n\r\u2028\u2029\t ].? {2,}/g; // note: repeating space
  83. let parent;
  84. /**
  85. * Creates a fix function that removes the multiple spaces between the two tokens
  86. * @param {RuleFixer} leftToken left token
  87. * @param {RuleFixer} rightToken right token
  88. * @returns {Function} fix function
  89. * @private
  90. */
  91. function createFix(leftToken, rightToken) {
  92. return function(fixer) {
  93. return fixer.replaceTextRange([leftToken.range[1], rightToken.range[0]], " ");
  94. };
  95. }
  96. while (pattern.test(source)) {
  97. // do not flag anything inside of comments
  98. if (!isIndexInComment(pattern.lastIndex, allComments)) {
  99. const token = sourceCode.getTokenByRangeStart(pattern.lastIndex);
  100. if (token) {
  101. const previousToken = sourceCode.getTokenBefore(token);
  102. if (hasExceptions) {
  103. parent = sourceCode.getNodeByRangeIndex(pattern.lastIndex - 1);
  104. }
  105. if (!parent || !exceptions[parent.type]) {
  106. context.report({
  107. node: token,
  108. loc: token.loc.start,
  109. message: "Multiple spaces found before '{{value}}'.",
  110. data: { value: token.value },
  111. fix: createFix(previousToken, token)
  112. });
  113. }
  114. }
  115. }
  116. }
  117. }
  118. };
  119. }
  120. };