no-mixed-spaces-and-tabs.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * @fileoverview Disallow mixed spaces and tabs for indentation
  3. * @author Jary Niebur
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. docs: {
  12. description: "disallow mixed spaces and tabs for indentation",
  13. category: "Stylistic Issues",
  14. recommended: true,
  15. url: "https://eslint.org/docs/rules/no-mixed-spaces-and-tabs"
  16. },
  17. schema: [
  18. {
  19. enum: ["smart-tabs", true, false]
  20. }
  21. ]
  22. },
  23. create(context) {
  24. const sourceCode = context.getSourceCode();
  25. let smartTabs;
  26. const ignoredLocs = [];
  27. switch (context.options[0]) {
  28. case true: // Support old syntax, maybe add deprecation warning here
  29. case "smart-tabs":
  30. smartTabs = true;
  31. break;
  32. default:
  33. smartTabs = false;
  34. }
  35. /**
  36. * Determines if a given line and column are before a location.
  37. * @param {Location} loc The location object from an AST node.
  38. * @param {int} line The line to check.
  39. * @param {int} column The column to check.
  40. * @returns {boolean} True if the line and column are before the location, false if not.
  41. * @private
  42. */
  43. function beforeLoc(loc, line, column) {
  44. if (line < loc.start.line) {
  45. return true;
  46. }
  47. return line === loc.start.line && column < loc.start.column;
  48. }
  49. /**
  50. * Determines if a given line and column are after a location.
  51. * @param {Location} loc The location object from an AST node.
  52. * @param {int} line The line to check.
  53. * @param {int} column The column to check.
  54. * @returns {boolean} True if the line and column are after the location, false if not.
  55. * @private
  56. */
  57. function afterLoc(loc, line, column) {
  58. if (line > loc.end.line) {
  59. return true;
  60. }
  61. return line === loc.end.line && column > loc.end.column;
  62. }
  63. //--------------------------------------------------------------------------
  64. // Public
  65. //--------------------------------------------------------------------------
  66. return {
  67. TemplateElement(node) {
  68. ignoredLocs.push(node.loc);
  69. },
  70. "Program:exit"(node) {
  71. /*
  72. * At least one space followed by a tab
  73. * or the reverse before non-tab/-space
  74. * characters begin.
  75. */
  76. let regex = /^(?=[\t ]*(\t | \t))/;
  77. const lines = sourceCode.lines,
  78. comments = sourceCode.getAllComments();
  79. comments.forEach(comment => {
  80. ignoredLocs.push(comment.loc);
  81. });
  82. ignoredLocs.sort((first, second) => {
  83. if (beforeLoc(first, second.start.line, second.start.column)) {
  84. return 1;
  85. }
  86. if (beforeLoc(second, first.start.line, second.start.column)) {
  87. return -1;
  88. }
  89. return 0;
  90. });
  91. if (smartTabs) {
  92. /*
  93. * At least one space followed by a tab
  94. * before non-tab/-space characters begin.
  95. */
  96. regex = /^(?=[\t ]* \t)/;
  97. }
  98. lines.forEach((line, i) => {
  99. const match = regex.exec(line);
  100. if (match) {
  101. const lineNumber = i + 1,
  102. column = match.index + 1;
  103. for (let j = 0; j < ignoredLocs.length; j++) {
  104. if (beforeLoc(ignoredLocs[j], lineNumber, column)) {
  105. continue;
  106. }
  107. if (afterLoc(ignoredLocs[j], lineNumber, column)) {
  108. continue;
  109. }
  110. return;
  111. }
  112. context.report({ node, loc: { line: lineNumber, column }, message: "Mixed spaces and tabs." });
  113. }
  114. });
  115. }
  116. };
  117. }
  118. };