Ohm-Management - Projektarbeit B-ME
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.

func-name-matching.js 9.9KB

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