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-per-function.js 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**
  2. * @fileoverview A rule to set the maximum number of line of code in a function.
  3. * @author Pete Ward <peteward44@gmail.com>
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("../util/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Constants
  12. //------------------------------------------------------------------------------
  13. const OPTIONS_SCHEMA = {
  14. type: "object",
  15. properties: {
  16. max: {
  17. type: "integer",
  18. minimum: 0
  19. },
  20. skipComments: {
  21. type: "boolean"
  22. },
  23. skipBlankLines: {
  24. type: "boolean"
  25. },
  26. IIFEs: {
  27. type: "boolean"
  28. }
  29. },
  30. additionalProperties: false
  31. };
  32. const OPTIONS_OR_INTEGER_SCHEMA = {
  33. oneOf: [
  34. OPTIONS_SCHEMA,
  35. {
  36. type: "integer",
  37. minimum: 1
  38. }
  39. ]
  40. };
  41. /**
  42. * Given a list of comment nodes, return a map with numeric keys (source code line numbers) and comment token values.
  43. * @param {Array} comments An array of comment nodes.
  44. * @returns {Map.<string,Node>} A map with numeric keys (source code line numbers) and comment token values.
  45. */
  46. function getCommentLineNumbers(comments) {
  47. const map = new Map();
  48. if (!comments) {
  49. return map;
  50. }
  51. comments.forEach(comment => {
  52. for (let i = comment.loc.start.line; i <= comment.loc.end.line; i++) {
  53. map.set(i, comment);
  54. }
  55. });
  56. return map;
  57. }
  58. //------------------------------------------------------------------------------
  59. // Rule Definition
  60. //------------------------------------------------------------------------------
  61. module.exports = {
  62. meta: {
  63. type: "suggestion",
  64. docs: {
  65. description: "enforce a maximum number of line of code in a function",
  66. category: "Stylistic Issues",
  67. recommended: false,
  68. url: "https://eslint.org/docs/rules/max-lines-per-function"
  69. },
  70. schema: [
  71. OPTIONS_OR_INTEGER_SCHEMA
  72. ]
  73. },
  74. create(context) {
  75. const sourceCode = context.getSourceCode();
  76. const lines = sourceCode.lines;
  77. const option = context.options[0];
  78. let maxLines = 50;
  79. let skipComments = false;
  80. let skipBlankLines = false;
  81. let IIFEs = false;
  82. if (typeof option === "object") {
  83. if (typeof option.max === "number") {
  84. maxLines = option.max;
  85. }
  86. if (typeof option.skipComments === "boolean") {
  87. skipComments = option.skipComments;
  88. }
  89. if (typeof option.skipBlankLines === "boolean") {
  90. skipBlankLines = option.skipBlankLines;
  91. }
  92. if (typeof option.IIFEs === "boolean") {
  93. IIFEs = option.IIFEs;
  94. }
  95. } else if (typeof option === "number") {
  96. maxLines = option;
  97. }
  98. const commentLineNumbers = getCommentLineNumbers(sourceCode.getAllComments());
  99. //--------------------------------------------------------------------------
  100. // Helpers
  101. //--------------------------------------------------------------------------
  102. /**
  103. * Tells if a comment encompasses the entire line.
  104. * @param {string} line The source line with a trailing comment
  105. * @param {number} lineNumber The one-indexed line number this is on
  106. * @param {ASTNode} comment The comment to remove
  107. * @returns {boolean} If the comment covers the entire line
  108. */
  109. function isFullLineComment(line, lineNumber, comment) {
  110. const start = comment.loc.start,
  111. end = comment.loc.end,
  112. isFirstTokenOnLine = start.line === lineNumber && !line.slice(0, start.column).trim(),
  113. isLastTokenOnLine = end.line === lineNumber && !line.slice(end.column).trim();
  114. return comment &&
  115. (start.line < lineNumber || isFirstTokenOnLine) &&
  116. (end.line > lineNumber || isLastTokenOnLine);
  117. }
  118. /**
  119. * Identifies is a node is a FunctionExpression which is part of an IIFE
  120. * @param {ASTNode} node Node to test
  121. * @returns {boolean} True if it's an IIFE
  122. */
  123. function isIIFE(node) {
  124. return node.type === "FunctionExpression" && node.parent && node.parent.type === "CallExpression" && node.parent.callee === node;
  125. }
  126. /**
  127. * Identifies is a node is a FunctionExpression which is embedded within a MethodDefinition or Property
  128. * @param {ASTNode} node Node to test
  129. * @returns {boolean} True if it's a FunctionExpression embedded within a MethodDefinition or Property
  130. */
  131. function isEmbedded(node) {
  132. if (!node.parent) {
  133. return false;
  134. }
  135. if (node !== node.parent.value) {
  136. return false;
  137. }
  138. if (node.parent.type === "MethodDefinition") {
  139. return true;
  140. }
  141. if (node.parent.type === "Property") {
  142. return node.parent.method === true || node.parent.kind === "get" || node.parent.kind === "set";
  143. }
  144. return false;
  145. }
  146. /**
  147. * Count the lines in the function
  148. * @param {ASTNode} funcNode Function AST node
  149. * @returns {void}
  150. * @private
  151. */
  152. function processFunction(funcNode) {
  153. const node = isEmbedded(funcNode) ? funcNode.parent : funcNode;
  154. if (!IIFEs && isIIFE(node)) {
  155. return;
  156. }
  157. let lineCount = 0;
  158. for (let i = node.loc.start.line - 1; i < node.loc.end.line; ++i) {
  159. const line = lines[i];
  160. if (skipComments) {
  161. if (commentLineNumbers.has(i + 1) && isFullLineComment(line, i + 1, commentLineNumbers.get(i + 1))) {
  162. continue;
  163. }
  164. }
  165. if (skipBlankLines) {
  166. if (line.match(/^\s*$/)) {
  167. continue;
  168. }
  169. }
  170. lineCount++;
  171. }
  172. if (lineCount > maxLines) {
  173. const name = astUtils.getFunctionNameWithKind(funcNode);
  174. context.report({
  175. node,
  176. message: "{{name}} has too many lines ({{lineCount}}). Maximum allowed is {{maxLines}}.",
  177. data: { name, lineCount, maxLines }
  178. });
  179. }
  180. }
  181. //--------------------------------------------------------------------------
  182. // Public API
  183. //--------------------------------------------------------------------------
  184. return {
  185. FunctionDeclaration: processFunction,
  186. FunctionExpression: processFunction,
  187. ArrowFunctionExpression: processFunction
  188. };
  189. }
  190. };