|
| 1 | +'use strict' |
| 2 | + |
| 3 | +/** |
| 4 | + * @fileoverview Rule to enforce return statements in callbacks of ElementArrayFinder's methods |
| 5 | + * @author Alexander Afanasyev (based on Toru Nagashima's work) |
| 6 | + */ |
| 7 | + |
| 8 | +var astUtils = require('eslint/lib/ast-utils') |
| 9 | +var isElementArrayFinder = require('../is-element-array-finder') |
| 10 | + |
| 11 | +var TARGET_NODE_TYPE = /^(?:Arrow)?FunctionExpression$/ |
| 12 | +var TARGET_METHODS = /^(?:filter|map|reduce)$/ |
| 13 | + |
| 14 | +/** |
| 15 | + * Checks a given code path segment is reachable. |
| 16 | + * |
| 17 | + * @param {CodePathSegment} segment - A segment to check. |
| 18 | + * @returns {boolean} `true` if the segment is reachable. |
| 19 | + */ |
| 20 | +function isReachable (segment) { |
| 21 | + return segment.reachable |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Gets a readable location. |
| 26 | + * |
| 27 | + * - FunctionExpression -> the function name or `function` keyword. |
| 28 | + * - ArrowFunctionExpression -> `=>` token. |
| 29 | + * |
| 30 | + * @param {ASTNode} node - A function node to get. |
| 31 | + * @param {SourceCode} sourceCode - A source code to get tokens. |
| 32 | + * @returns {ASTNode|Token} The node or the token of a location. |
| 33 | + */ |
| 34 | +function getLocation (node, sourceCode) { |
| 35 | + if (node.type === 'ArrowFunctionExpression') { |
| 36 | + return sourceCode.getTokenBefore(node.body) |
| 37 | + } |
| 38 | + return node.id || node |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Checks a given node is a MemberExpression node which has the specified name's |
| 43 | + * property. |
| 44 | + * |
| 45 | + * @param {ASTNode} node - A node to check. |
| 46 | + * @returns {boolean} `true` if the node is a MemberExpression node which has |
| 47 | + * the specified name's property |
| 48 | + */ |
| 49 | +function isTargetMethod (node) { |
| 50 | + return (isElementArrayFinder(node) && |
| 51 | + node.type === 'MemberExpression' && |
| 52 | + node.property && |
| 53 | + TARGET_METHODS.test(node.property.name) |
| 54 | + ) |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Checks whether or not a given node is a function expression which is the |
| 59 | + * callback of an array method. |
| 60 | + * |
| 61 | + * @param {ASTNode} node - A node to check. This is one of |
| 62 | + * FunctionExpression or ArrowFunctionExpression. |
| 63 | + * @returns {boolean} `true` if the node is the callback of an array method. |
| 64 | + */ |
| 65 | +function isCallbackOfArrayMethod (node) { |
| 66 | + while (node) { |
| 67 | + var parent = node.parent |
| 68 | + |
| 69 | + switch (parent.type) { |
| 70 | + case 'LogicalExpression': |
| 71 | + case 'ConditionalExpression': |
| 72 | + node = parent |
| 73 | + break |
| 74 | + |
| 75 | + case 'ReturnStatement': |
| 76 | + var func = astUtils.getUpperFunction(parent) |
| 77 | + |
| 78 | + if (func === null || !astUtils.isCallee(func)) { |
| 79 | + return false |
| 80 | + } |
| 81 | + node = func.parent |
| 82 | + break |
| 83 | + |
| 84 | + case 'CallExpression': |
| 85 | + if (isTargetMethod(parent.callee)) { |
| 86 | + return (parent.arguments.length >= 1 && parent.arguments[0] === node) |
| 87 | + } |
| 88 | + return false |
| 89 | + |
| 90 | + // Otherwise this node is not target. |
| 91 | + /* istanbul ignore next: unreachable */ |
| 92 | + default: |
| 93 | + return false |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /* istanbul ignore next: unreachable */ |
| 98 | + return false |
| 99 | +} |
| 100 | + |
| 101 | +module.exports = { |
| 102 | + meta: { |
| 103 | + schema: [] |
| 104 | + }, |
| 105 | + |
| 106 | + create: function (context) { |
| 107 | + var funcInfo = { |
| 108 | + upper: null, |
| 109 | + codePath: null, |
| 110 | + hasReturn: false, |
| 111 | + shouldCheck: false |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Checks whether or not the last code path segment is reachable. |
| 116 | + * Then reports this function if the segment is reachable. |
| 117 | + * |
| 118 | + * If the last code path segment is reachable, there are paths which are not |
| 119 | + * returned or thrown. |
| 120 | + * |
| 121 | + * @param {ASTNode} node - A node to check. |
| 122 | + * @returns {void} |
| 123 | + */ |
| 124 | + function checkLastSegment (node) { |
| 125 | + if (funcInfo.shouldCheck && |
| 126 | + funcInfo.codePath.currentSegments.some(isReachable) |
| 127 | + ) { |
| 128 | + context.report({ |
| 129 | + node: node, |
| 130 | + loc: getLocation(node, context.getSourceCode()).loc.start, |
| 131 | + message: funcInfo.hasReturn |
| 132 | + ? 'Expected to return a value at the end of this function' |
| 133 | + : 'Expected to return a value in this function' |
| 134 | + }) |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + return { |
| 139 | + // Stacks this function's information. |
| 140 | + 'onCodePathStart': function (codePath, node) { |
| 141 | + funcInfo = { |
| 142 | + upper: funcInfo, |
| 143 | + codePath: codePath, |
| 144 | + hasReturn: false, |
| 145 | + shouldCheck: TARGET_NODE_TYPE.test(node.type) && |
| 146 | + node.body.type === 'BlockStatement' && |
| 147 | + isCallbackOfArrayMethod(node) |
| 148 | + } |
| 149 | + }, |
| 150 | + |
| 151 | + // Pops this function's information. |
| 152 | + 'onCodePathEnd': function () { |
| 153 | + funcInfo = funcInfo.upper |
| 154 | + }, |
| 155 | + |
| 156 | + // Checks the return statement is valid. |
| 157 | + 'ReturnStatement': function (node) { |
| 158 | + if (funcInfo.shouldCheck) { |
| 159 | + funcInfo.hasReturn = true |
| 160 | + |
| 161 | + if (!node.argument) { |
| 162 | + context.report({ |
| 163 | + node: node, |
| 164 | + message: 'Expected a return value' |
| 165 | + }) |
| 166 | + } |
| 167 | + } |
| 168 | + }, |
| 169 | + |
| 170 | + // Reports a given function if the last path is reachable. |
| 171 | + 'FunctionExpression:exit': checkLastSegment, |
| 172 | + 'ArrowFunctionExpression:exit': checkLastSegment |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments