no-tabs.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * @fileoverview Rule to check for tabs inside a file
  3. * @author Gyandeep Singh
  4. * @deprecated in ESLint v8.53.0
  5. */
  6. "use strict";
  7. //------------------------------------------------------------------------------
  8. // Helpers
  9. //------------------------------------------------------------------------------
  10. const tabRegex = /\t+/gu;
  11. const anyNonWhitespaceRegex = /\S/u;
  12. //------------------------------------------------------------------------------
  13. // Public Interface
  14. //------------------------------------------------------------------------------
  15. /** @type {import('../types').Rule.RuleModule} */
  16. module.exports = {
  17. meta: {
  18. deprecated: {
  19. message: "Formatting rules are being moved out of ESLint core.",
  20. url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/",
  21. deprecatedSince: "8.53.0",
  22. availableUntil: "11.0.0",
  23. replacedBy: [
  24. {
  25. message:
  26. "ESLint Stylistic now maintains deprecated stylistic core rules.",
  27. url: "https://eslint.style/guide/migration",
  28. plugin: {
  29. name: "@stylistic/eslint-plugin",
  30. url: "https://eslint.style",
  31. },
  32. rule: {
  33. name: "no-tabs",
  34. url: "https://eslint.style/rules/no-tabs",
  35. },
  36. },
  37. ],
  38. },
  39. type: "layout",
  40. docs: {
  41. description: "Disallow all tabs",
  42. recommended: false,
  43. url: "https://eslint.org/docs/latest/rules/no-tabs",
  44. },
  45. schema: [
  46. {
  47. type: "object",
  48. properties: {
  49. allowIndentationTabs: {
  50. type: "boolean",
  51. default: false,
  52. },
  53. },
  54. additionalProperties: false,
  55. },
  56. ],
  57. messages: {
  58. unexpectedTab: "Unexpected tab character.",
  59. },
  60. },
  61. create(context) {
  62. const sourceCode = context.sourceCode;
  63. const allowIndentationTabs =
  64. context.options &&
  65. context.options[0] &&
  66. context.options[0].allowIndentationTabs;
  67. return {
  68. Program(node) {
  69. sourceCode.getLines().forEach((line, index) => {
  70. let match;
  71. while ((match = tabRegex.exec(line)) !== null) {
  72. if (
  73. allowIndentationTabs &&
  74. !anyNonWhitespaceRegex.test(
  75. line.slice(0, match.index),
  76. )
  77. ) {
  78. continue;
  79. }
  80. context.report({
  81. node,
  82. loc: {
  83. start: {
  84. line: index + 1,
  85. column: match.index,
  86. },
  87. end: {
  88. line: index + 1,
  89. column: match.index + match[0].length,
  90. },
  91. },
  92. messageId: "unexpectedTab",
  93. });
  94. }
  95. });
  96. },
  97. };
  98. },
  99. };