max-lines-per-function.js 6.9 KB

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