no-tabs.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * @fileoverview Rule to check for tabs inside a file
  3. * @author Gyandeep Singh
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Helpers
  8. //------------------------------------------------------------------------------
  9. const tabRegex = /\t+/gu;
  10. const anyNonWhitespaceRegex = /\S/u;
  11. //------------------------------------------------------------------------------
  12. // Public Interface
  13. //------------------------------------------------------------------------------
  14. /** @type {import('../shared/types').Rule} */
  15. module.exports = {
  16. meta: {
  17. type: "layout",
  18. docs: {
  19. description: "Disallow all tabs",
  20. recommended: false,
  21. url: "https://eslint.org/docs/rules/no-tabs"
  22. },
  23. schema: [{
  24. type: "object",
  25. properties: {
  26. allowIndentationTabs: {
  27. type: "boolean",
  28. default: false
  29. }
  30. },
  31. additionalProperties: false
  32. }],
  33. messages: {
  34. unexpectedTab: "Unexpected tab character."
  35. }
  36. },
  37. create(context) {
  38. const sourceCode = context.getSourceCode();
  39. const allowIndentationTabs = context.options && context.options[0] && context.options[0].allowIndentationTabs;
  40. return {
  41. Program(node) {
  42. sourceCode.getLines().forEach((line, index) => {
  43. let match;
  44. while ((match = tabRegex.exec(line)) !== null) {
  45. if (allowIndentationTabs && !anyNonWhitespaceRegex.test(line.slice(0, match.index))) {
  46. continue;
  47. }
  48. context.report({
  49. node,
  50. loc: {
  51. start: {
  52. line: index + 1,
  53. column: match.index
  54. },
  55. end: {
  56. line: index + 1,
  57. column: match.index + match[0].length
  58. }
  59. },
  60. messageId: "unexpectedTab"
  61. });
  62. }
  63. });
  64. }
  65. };
  66. }
  67. };