Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @fileoverview Rule to require function names to match the name of the variable or property to which they are assigned. |
| 3 | * @author Annie Zhang, Pavel Strashkin |
| 4 | */ |
| 5 | |
| 6 | "use strict"; |
| 7 | |
| 8 | //-------------------------------------------------------------------------- |
| 9 | // Requirements |
| 10 | //-------------------------------------------------------------------------- |
| 11 | |
| 12 | const astUtils = require("./utils/ast-utils"); |
| 13 | const esutils = require("esutils"); |
| 14 | |
| 15 | //-------------------------------------------------------------------------- |
| 16 | // Helpers |
| 17 | //-------------------------------------------------------------------------- |
| 18 | |
| 19 | /** |
| 20 | * Determines if a pattern is `module.exports` or `module["exports"]` |
| 21 | * @param {ASTNode} pattern The left side of the AssignmentExpression |
| 22 | * @returns {boolean} True if the pattern is `module.exports` or `module["exports"]` |
| 23 | */ |
| 24 | function isModuleExports(pattern) { |
| 25 | if (pattern.type === "MemberExpression" && pattern.object.type === "Identifier" && pattern.object.name === "module") { |
| 26 | |
| 27 | // module.exports |
| 28 | if (pattern.property.type === "Identifier" && pattern.property.name === "exports") { |
| 29 | return true; |
| 30 | } |
| 31 | |
| 32 | // module["exports"] |
| 33 | if (pattern.property.type === "Literal" && pattern.property.value === "exports") { |
| 34 | return true; |
| 35 | } |
| 36 | } |
| 37 | return false; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Determines if a string name is a valid identifier |
| 42 | * @param {string} name The string to be checked |
| 43 | * @param {int} ecmaVersion The ECMAScript version if specified in the parserOptions config |
| 44 | * @returns {boolean} True if the string is a valid identifier |
| 45 | */ |
| 46 | function isIdentifier(name, ecmaVersion) { |
| 47 | if (ecmaVersion >= 6) { |
| 48 | return esutils.keyword.isIdentifierES6(name); |
| 49 | } |
| 50 | return esutils.keyword.isIdentifierES5(name); |
| 51 | } |
| 52 | |
| 53 | //------------------------------------------------------------------------------ |
| 54 | // Rule Definition |
| 55 | //------------------------------------------------------------------------------ |
| 56 | |
| 57 | const alwaysOrNever = { enum: ["always", "never"] }; |
| 58 | const optionsObject = { |
| 59 | type: "object", |
| 60 | properties: { |
| 61 | considerPropertyDescriptor: { |
| 62 | type: "boolean" |
| 63 | }, |
| 64 | includeCommonJSModuleExports: { |
| 65 | type: "boolean" |
| 66 | } |
| 67 | }, |
| 68 | additionalProperties: false |
| 69 | }; |
| 70 | |
| 71 | module.exports = { |
| 72 | meta: { |
| 73 | type: "suggestion", |
| 74 | |
| 75 | docs: { |
| 76 | description: "require function names to match the name of the variable or property to which they are assigned", |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 77 | recommended: false, |
| 78 | url: "https://eslint.org/docs/rules/func-name-matching" |
| 79 | }, |
| 80 | |
| 81 | schema: { |
| 82 | anyOf: [{ |
| 83 | type: "array", |
| 84 | additionalItems: false, |
| 85 | items: [alwaysOrNever, optionsObject] |
| 86 | }, { |
| 87 | type: "array", |
| 88 | additionalItems: false, |
| 89 | items: [optionsObject] |
| 90 | }] |
| 91 | }, |
| 92 | |
| 93 | messages: { |
| 94 | matchProperty: "Function name `{{funcName}}` should match property name `{{name}}`.", |
| 95 | matchVariable: "Function name `{{funcName}}` should match variable name `{{name}}`.", |
| 96 | notMatchProperty: "Function name `{{funcName}}` should not match property name `{{name}}`.", |
| 97 | notMatchVariable: "Function name `{{funcName}}` should not match variable name `{{name}}`." |
| 98 | } |
| 99 | }, |
| 100 | |
| 101 | create(context) { |
| 102 | const options = (typeof context.options[0] === "object" ? context.options[0] : context.options[1]) || {}; |
| 103 | const nameMatches = typeof context.options[0] === "string" ? context.options[0] : "always"; |
| 104 | const considerPropertyDescriptor = options.considerPropertyDescriptor; |
| 105 | const includeModuleExports = options.includeCommonJSModuleExports; |
| 106 | const ecmaVersion = context.parserOptions && context.parserOptions.ecmaVersion ? context.parserOptions.ecmaVersion : 5; |
| 107 | |
| 108 | /** |
| 109 | * Check whether node is a certain CallExpression. |
| 110 | * @param {string} objName object name |
| 111 | * @param {string} funcName function name |
| 112 | * @param {ASTNode} node The node to check |
| 113 | * @returns {boolean} `true` if node matches CallExpression |
| 114 | */ |
| 115 | function isPropertyCall(objName, funcName, node) { |
| 116 | if (!node) { |
| 117 | return false; |
| 118 | } |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 119 | return node.type === "CallExpression" && astUtils.isSpecificMemberAccess(node.callee, objName, funcName); |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Compares identifiers based on the nameMatches option |
| 124 | * @param {string} x the first identifier |
| 125 | * @param {string} y the second identifier |
| 126 | * @returns {boolean} whether the two identifiers should warn. |
| 127 | */ |
| 128 | function shouldWarn(x, y) { |
| 129 | return (nameMatches === "always" && x !== y) || (nameMatches === "never" && x === y); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Reports |
| 134 | * @param {ASTNode} node The node to report |
| 135 | * @param {string} name The variable or property name |
| 136 | * @param {string} funcName The function name |
| 137 | * @param {boolean} isProp True if the reported node is a property assignment |
| 138 | * @returns {void} |
| 139 | */ |
| 140 | function report(node, name, funcName, isProp) { |
| 141 | let messageId; |
| 142 | |
| 143 | if (nameMatches === "always" && isProp) { |
| 144 | messageId = "matchProperty"; |
| 145 | } else if (nameMatches === "always") { |
| 146 | messageId = "matchVariable"; |
| 147 | } else if (isProp) { |
| 148 | messageId = "notMatchProperty"; |
| 149 | } else { |
| 150 | messageId = "notMatchVariable"; |
| 151 | } |
| 152 | context.report({ |
| 153 | node, |
| 154 | messageId, |
| 155 | data: { |
| 156 | name, |
| 157 | funcName |
| 158 | } |
| 159 | }); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Determines whether a given node is a string literal |
| 164 | * @param {ASTNode} node The node to check |
| 165 | * @returns {boolean} `true` if the node is a string literal |
| 166 | */ |
| 167 | function isStringLiteral(node) { |
| 168 | return node.type === "Literal" && typeof node.value === "string"; |
| 169 | } |
| 170 | |
| 171 | //-------------------------------------------------------------------------- |
| 172 | // Public |
| 173 | //-------------------------------------------------------------------------- |
| 174 | |
| 175 | return { |
| 176 | VariableDeclarator(node) { |
| 177 | if (!node.init || node.init.type !== "FunctionExpression" || node.id.type !== "Identifier") { |
| 178 | return; |
| 179 | } |
| 180 | if (node.init.id && shouldWarn(node.id.name, node.init.id.name)) { |
| 181 | report(node, node.id.name, node.init.id.name, false); |
| 182 | } |
| 183 | }, |
| 184 | |
| 185 | AssignmentExpression(node) { |
| 186 | if ( |
| 187 | node.right.type !== "FunctionExpression" || |
| 188 | (node.left.computed && node.left.property.type !== "Literal") || |
| 189 | (!includeModuleExports && isModuleExports(node.left)) || |
| 190 | (node.left.type !== "Identifier" && node.left.type !== "MemberExpression") |
| 191 | ) { |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | const isProp = node.left.type === "MemberExpression"; |
| 196 | const name = isProp ? astUtils.getStaticPropertyName(node.left) : node.left.name; |
| 197 | |
Tim van der Lippe | 0fb4780 | 2021-11-08 16:23:10 +0000 | [diff] [blame] | 198 | if (node.right.id && name && isIdentifier(name) && shouldWarn(name, node.right.id.name)) { |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 199 | report(node, name, node.right.id.name, isProp); |
| 200 | } |
| 201 | }, |
| 202 | |
Tim van der Lippe | 0fb4780 | 2021-11-08 16:23:10 +0000 | [diff] [blame] | 203 | "Property, PropertyDefinition[value]"(node) { |
| 204 | if (!(node.value.type === "FunctionExpression" && node.value.id)) { |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 205 | return; |
| 206 | } |
| 207 | |
Tim van der Lippe | 0fb4780 | 2021-11-08 16:23:10 +0000 | [diff] [blame] | 208 | if (node.key.type === "Identifier" && !node.computed) { |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 209 | const functionName = node.value.id.name; |
| 210 | let propertyName = node.key.name; |
| 211 | |
Tim van der Lippe | 0fb4780 | 2021-11-08 16:23:10 +0000 | [diff] [blame] | 212 | if ( |
| 213 | considerPropertyDescriptor && |
| 214 | propertyName === "value" && |
| 215 | node.parent.type === "ObjectExpression" |
| 216 | ) { |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 217 | if (isPropertyCall("Object", "defineProperty", node.parent.parent) || isPropertyCall("Reflect", "defineProperty", node.parent.parent)) { |
| 218 | const property = node.parent.parent.arguments[1]; |
| 219 | |
| 220 | if (isStringLiteral(property) && shouldWarn(property.value, functionName)) { |
| 221 | report(node, property.value, functionName, true); |
| 222 | } |
| 223 | } else if (isPropertyCall("Object", "defineProperties", node.parent.parent.parent.parent)) { |
| 224 | propertyName = node.parent.parent.key.name; |
| 225 | if (!node.parent.parent.computed && shouldWarn(propertyName, functionName)) { |
| 226 | report(node, propertyName, functionName, true); |
| 227 | } |
| 228 | } else if (isPropertyCall("Object", "create", node.parent.parent.parent.parent)) { |
| 229 | propertyName = node.parent.parent.key.name; |
| 230 | if (!node.parent.parent.computed && shouldWarn(propertyName, functionName)) { |
| 231 | report(node, propertyName, functionName, true); |
| 232 | } |
| 233 | } else if (shouldWarn(propertyName, functionName)) { |
| 234 | report(node, propertyName, functionName, true); |
| 235 | } |
| 236 | } else if (shouldWarn(propertyName, functionName)) { |
| 237 | report(node, propertyName, functionName, true); |
| 238 | } |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | if ( |
| 243 | isStringLiteral(node.key) && |
| 244 | isIdentifier(node.key.value, ecmaVersion) && |
| 245 | shouldWarn(node.key.value, node.value.id.name) |
| 246 | ) { |
| 247 | report(node, node.key.value, node.value.id.name, true); |
| 248 | } |
| 249 | } |
| 250 | }; |
| 251 | } |
| 252 | }; |