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