max-lines.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * @fileoverview enforce a maximum file length
  3. * @author Alberto Rodríguez
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const lodash = require("lodash");
  10. const astUtils = require("./utils/ast-utils");
  11. //------------------------------------------------------------------------------
  12. // Rule Definition
  13. //------------------------------------------------------------------------------
  14. module.exports = {
  15. meta: {
  16. type: "suggestion",
  17. docs: {
  18. description: "enforce a maximum number of lines per file",
  19. category: "Stylistic Issues",
  20. recommended: false,
  21. url: "https://eslint.org/docs/rules/max-lines"
  22. },
  23. schema: [
  24. {
  25. oneOf: [
  26. {
  27. type: "integer",
  28. minimum: 0
  29. },
  30. {
  31. type: "object",
  32. properties: {
  33. max: {
  34. type: "integer",
  35. minimum: 0
  36. },
  37. skipComments: {
  38. type: "boolean"
  39. },
  40. skipBlankLines: {
  41. type: "boolean"
  42. }
  43. },
  44. additionalProperties: false
  45. }
  46. ]
  47. }
  48. ],
  49. messages: {
  50. exceed: "File has too many lines ({{actual}}). Maximum allowed is {{max}}."
  51. }
  52. },
  53. create(context) {
  54. const option = context.options[0];
  55. let max = 300;
  56. if (typeof option === "object" && Object.prototype.hasOwnProperty.call(option, "max")) {
  57. max = option.max;
  58. } else if (typeof option === "number") {
  59. max = option;
  60. }
  61. const skipComments = option && option.skipComments;
  62. const skipBlankLines = option && option.skipBlankLines;
  63. const sourceCode = context.getSourceCode();
  64. /**
  65. * Returns whether or not a token is a comment node type
  66. * @param {Token} token The token to check
  67. * @returns {boolean} True if the token is a comment node
  68. */
  69. function isCommentNodeType(token) {
  70. return token && (token.type === "Block" || token.type === "Line");
  71. }
  72. /**
  73. * Returns the line numbers of a comment that don't have any code on the same line
  74. * @param {Node} comment The comment node to check
  75. * @returns {number[]} The line numbers
  76. */
  77. function getLinesWithoutCode(comment) {
  78. let start = comment.loc.start.line;
  79. let end = comment.loc.end.line;
  80. let token;
  81. token = comment;
  82. do {
  83. token = sourceCode.getTokenBefore(token, { includeComments: true });
  84. } while (isCommentNodeType(token));
  85. if (token && astUtils.isTokenOnSameLine(token, comment)) {
  86. start += 1;
  87. }
  88. token = comment;
  89. do {
  90. token = sourceCode.getTokenAfter(token, { includeComments: true });
  91. } while (isCommentNodeType(token));
  92. if (token && astUtils.isTokenOnSameLine(comment, token)) {
  93. end -= 1;
  94. }
  95. if (start <= end) {
  96. return lodash.range(start, end + 1);
  97. }
  98. return [];
  99. }
  100. return {
  101. "Program:exit"() {
  102. let lines = sourceCode.lines.map((text, i) => ({ lineNumber: i + 1, text }));
  103. if (skipBlankLines) {
  104. lines = lines.filter(l => l.text.trim() !== "");
  105. }
  106. if (skipComments) {
  107. const comments = sourceCode.getAllComments();
  108. const commentLines = lodash.flatten(comments.map(comment => getLinesWithoutCode(comment)));
  109. lines = lines.filter(l => !lodash.includes(commentLines, l.lineNumber));
  110. }
  111. if (lines.length > max) {
  112. context.report({
  113. loc: { line: 1, column: 0 },
  114. messageId: "exceed",
  115. data: {
  116. max,
  117. actual: lines.length
  118. }
  119. });
  120. }
  121. }
  122. };
  123. }
  124. };