Blame view
node_modules/eslint/lib/rules/no-self-assign.js
6.25 KB
f7563de62
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
/** * @fileoverview Rule to disallow assignments where both sides are exactly the same * @author Toru Nagashima */ "use strict"; //------------------------------------------------------------------------------ // Requirements //------------------------------------------------------------------------------ const astUtils = require("../ast-utils"); //------------------------------------------------------------------------------ // Helpers //------------------------------------------------------------------------------ const SPACES = /\s+/g; /** * Checks whether the property of 2 given member expression nodes are the same * property or not. * * @param {ASTNode} left - A member expression node to check. * @param {ASTNode} right - Another member expression node to check. * @returns {boolean} `true` if the member expressions have the same property. */ function isSameProperty(left, right) { if (left.property.type === "Identifier" && left.property.type === right.property.type && left.property.name === right.property.name && left.computed === right.computed ) { return true; } const lname = astUtils.getStaticPropertyName(left); const rname = astUtils.getStaticPropertyName(right); return lname !== null && lname === rname; } /** * Checks whether 2 given member expression nodes are the reference to the same * property or not. * * @param {ASTNode} left - A member expression node to check. * @param {ASTNode} right - Another member expression node to check. * @returns {boolean} `true` if the member expressions are the reference to the * same property or not. */ function isSameMember(left, right) { if (!isSameProperty(left, right)) { return false; } const lobj = left.object; const robj = right.object; if (lobj.type !== robj.type) { return false; } if (lobj.type === "MemberExpression") { return isSameMember(lobj, robj); } return lobj.type === "Identifier" && lobj.name === robj.name; } /** * Traverses 2 Pattern nodes in parallel, then reports self-assignments. * * @param {ASTNode|null} left - A left node to traverse. This is a Pattern or * a Property. * @param {ASTNode|null} right - A right node to traverse. This is a Pattern or * a Property. * @param {boolean} props - The flag to check member expressions as well. * @param {Function} report - A callback function to report. * @returns {void} */ function eachSelfAssignment(left, right, props, report) { if (!left || !right) { // do nothing } else if ( left.type === "Identifier" && right.type === "Identifier" && left.name === right.name ) { report(right); } else if ( left.type === "ArrayPattern" && right.type === "ArrayExpression" ) { const end = Math.min(left.elements.length, right.elements.length); for (let i = 0; i < end; ++i) { const rightElement = right.elements[i]; eachSelfAssignment(left.elements[i], rightElement, props, report); // After a spread element, those indices are unknown. if (rightElement && rightElement.type === "SpreadElement") { break; } } } else if ( left.type === "RestElement" && right.type === "SpreadElement" ) { eachSelfAssignment(left.argument, right.argument, props, report); } else if ( left.type === "ObjectPattern" && right.type === "ObjectExpression" && right.properties.length >= 1 ) { // Gets the index of the last spread property. // It's possible to overwrite properties followed by it. let startJ = 0; for (let i = right.properties.length - 1; i >= 0; --i) { if (right.properties[i].type === "ExperimentalSpreadProperty") { startJ = i + 1; break; } } for (let i = 0; i < left.properties.length; ++i) { for (let j = startJ; j < right.properties.length; ++j) { eachSelfAssignment( left.properties[i], right.properties[j], props, report ); } } } else if ( left.type === "Property" && right.type === "Property" && !left.computed && !right.computed && right.kind === "init" && !right.method && left.key.name === right.key.name ) { eachSelfAssignment(left.value, right.value, props, report); } else if ( props && left.type === "MemberExpression" && right.type === "MemberExpression" && isSameMember(left, right) ) { report(right); } } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ module.exports = { meta: { docs: { description: "disallow assignments where both sides are exactly the same", category: "Best Practices", recommended: true }, schema: [ { type: "object", properties: { props: { type: "boolean" } }, additionalProperties: false } ] }, create(context) { const sourceCode = context.getSourceCode(); const options = context.options[0]; const props = Boolean(options && options.props); /** * Reports a given node as self assignments. * * @param {ASTNode} node - A node to report. This is an Identifier node. * @returns {void} */ function report(node) { context.report({ node, message: "'{{name}}' is assigned to itself.", data: { name: sourceCode.getText(node).replace(SPACES, "") } }); } return { AssignmentExpression(node) { if (node.operator === "=") { eachSelfAssignment(node.left, node.right, props, report); } } }; } }; |