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-mixed-spaces-and-tabs.js 4.5KB

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