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.0 KiB

пре 3 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. docs: {
  64. description: "enforce a maximum number of line of code in a function",
  65. category: "Stylistic Issues",
  66. recommended: false,
  67. url: "https://eslint.org/docs/rules/max-lines-per-function"
  68. },
  69. schema: [
  70. OPTIONS_OR_INTEGER_SCHEMA
  71. ]
  72. },
  73. create(context) {
  74. const sourceCode = context.getSourceCode();
  75. const lines = sourceCode.lines;
  76. const option = context.options[0];
  77. let maxLines = 50;
  78. let skipComments = false;
  79. let skipBlankLines = false;
  80. let IIFEs = false;
  81. if (typeof option === "object") {
  82. if (typeof option.max === "number") {
  83. maxLines = option.max;
  84. }
  85. if (typeof option.skipComments === "boolean") {
  86. skipComments = option.skipComments;
  87. }
  88. if (typeof option.skipBlankLines === "boolean") {
  89. skipBlankLines = option.skipBlankLines;
  90. }
  91. if (typeof option.IIFEs === "boolean") {
  92. IIFEs = option.IIFEs;
  93. }
  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*$/)) {
  166. continue;
  167. }
  168. }
  169. lineCount++;
  170. }
  171. if (lineCount > maxLines) {
  172. const name = astUtils.getFunctionNameWithKind(funcNode);
  173. context.report({
  174. node,
  175. message: "{{name}} has too many lines ({{lineCount}}). Maximum allowed is {{maxLines}}.",
  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. };