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.

max-lines.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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("../util/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. },
  50. create(context) {
  51. const option = context.options[0];
  52. let max = 300;
  53. if (typeof option === "object" && Object.prototype.hasOwnProperty.call(option, "max") && typeof option.max === "number") {
  54. max = option.max;
  55. }
  56. if (typeof option === "number") {
  57. max = option;
  58. }
  59. const skipComments = option && option.skipComments;
  60. const skipBlankLines = option && option.skipBlankLines;
  61. const sourceCode = context.getSourceCode();
  62. /**
  63. * Returns whether or not a token is a comment node type
  64. * @param {Token} token The token to check
  65. * @returns {boolean} True if the token is a comment node
  66. */
  67. function isCommentNodeType(token) {
  68. return token && (token.type === "Block" || token.type === "Line");
  69. }
  70. /**
  71. * Returns the line numbers of a comment that don't have any code on the same line
  72. * @param {Node} comment The comment node to check
  73. * @returns {int[]} The line numbers
  74. */
  75. function getLinesWithoutCode(comment) {
  76. let start = comment.loc.start.line;
  77. let end = comment.loc.end.line;
  78. let token;
  79. token = comment;
  80. do {
  81. token = sourceCode.getTokenBefore(token, { includeComments: true });
  82. } while (isCommentNodeType(token));
  83. if (token && astUtils.isTokenOnSameLine(token, comment)) {
  84. start += 1;
  85. }
  86. token = comment;
  87. do {
  88. token = sourceCode.getTokenAfter(token, { includeComments: true });
  89. } while (isCommentNodeType(token));
  90. if (token && astUtils.isTokenOnSameLine(comment, token)) {
  91. end -= 1;
  92. }
  93. if (start <= end) {
  94. return lodash.range(start, end + 1);
  95. }
  96. return [];
  97. }
  98. return {
  99. "Program:exit"() {
  100. let lines = sourceCode.lines.map((text, i) => ({ lineNumber: i + 1, text }));
  101. if (skipBlankLines) {
  102. lines = lines.filter(l => l.text.trim() !== "");
  103. }
  104. if (skipComments) {
  105. const comments = sourceCode.getAllComments();
  106. const commentLines = lodash.flatten(comments.map(comment => getLinesWithoutCode(comment)));
  107. lines = lines.filter(l => !lodash.includes(commentLines, l.lineNumber));
  108. }
  109. if (lines.length > max) {
  110. context.report({
  111. loc: { line: 1, column: 0 },
  112. message: "File must be at most {{max}} lines long. It's {{actual}} lines long.",
  113. data: {
  114. max,
  115. actual: lines.length
  116. }
  117. });
  118. }
  119. }
  120. };
  121. }
  122. };