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.

no-underscore-dangle.js 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**
  2. * @fileoverview Rule to flag trailing underscores in variable declarations.
  3. * @author Matt DuVall <http://www.mattduvall.com>
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. type: "suggestion",
  12. docs: {
  13. description: "disallow dangling underscores in identifiers",
  14. category: "Stylistic Issues",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/no-underscore-dangle"
  17. },
  18. schema: [
  19. {
  20. type: "object",
  21. properties: {
  22. allow: {
  23. type: "array",
  24. items: {
  25. type: "string"
  26. }
  27. },
  28. allowAfterThis: {
  29. type: "boolean"
  30. },
  31. allowAfterSuper: {
  32. type: "boolean"
  33. },
  34. enforceInMethodNames: {
  35. type: "boolean"
  36. }
  37. },
  38. additionalProperties: false
  39. }
  40. ]
  41. },
  42. create(context) {
  43. const options = context.options[0] || {};
  44. const ALLOWED_VARIABLES = options.allow ? options.allow : [];
  45. const allowAfterThis = typeof options.allowAfterThis !== "undefined" ? options.allowAfterThis : false;
  46. const allowAfterSuper = typeof options.allowAfterSuper !== "undefined" ? options.allowAfterSuper : false;
  47. const enforceInMethodNames = typeof options.enforceInMethodNames !== "undefined" ? options.enforceInMethodNames : false;
  48. //-------------------------------------------------------------------------
  49. // Helpers
  50. //-------------------------------------------------------------------------
  51. /**
  52. * Check if identifier is present inside the allowed option
  53. * @param {string} identifier name of the node
  54. * @returns {boolean} true if its is present
  55. * @private
  56. */
  57. function isAllowed(identifier) {
  58. return ALLOWED_VARIABLES.some(ident => ident === identifier);
  59. }
  60. /**
  61. * Check if identifier has a underscore at the end
  62. * @param {ASTNode} identifier node to evaluate
  63. * @returns {boolean} true if its is present
  64. * @private
  65. */
  66. function hasTrailingUnderscore(identifier) {
  67. const len = identifier.length;
  68. return identifier !== "_" && (identifier[0] === "_" || identifier[len - 1] === "_");
  69. }
  70. /**
  71. * Check if identifier is a special case member expression
  72. * @param {ASTNode} identifier node to evaluate
  73. * @returns {boolean} true if its is a special case
  74. * @private
  75. */
  76. function isSpecialCaseIdentifierForMemberExpression(identifier) {
  77. return identifier === "__proto__";
  78. }
  79. /**
  80. * Check if identifier is a special case variable expression
  81. * @param {ASTNode} identifier node to evaluate
  82. * @returns {boolean} true if its is a special case
  83. * @private
  84. */
  85. function isSpecialCaseIdentifierInVariableExpression(identifier) {
  86. // Checks for the underscore library usage here
  87. return identifier === "_";
  88. }
  89. /**
  90. * Check if function has a underscore at the end
  91. * @param {ASTNode} node node to evaluate
  92. * @returns {void}
  93. * @private
  94. */
  95. function checkForTrailingUnderscoreInFunctionDeclaration(node) {
  96. if (node.id) {
  97. const identifier = node.id.name;
  98. if (typeof identifier !== "undefined" && hasTrailingUnderscore(identifier) && !isAllowed(identifier)) {
  99. context.report({
  100. node,
  101. message: "Unexpected dangling '_' in '{{identifier}}'.",
  102. data: {
  103. identifier
  104. }
  105. });
  106. }
  107. }
  108. }
  109. /**
  110. * Check if variable expression has a underscore at the end
  111. * @param {ASTNode} node node to evaluate
  112. * @returns {void}
  113. * @private
  114. */
  115. function checkForTrailingUnderscoreInVariableExpression(node) {
  116. const identifier = node.id.name;
  117. if (typeof identifier !== "undefined" && hasTrailingUnderscore(identifier) &&
  118. !isSpecialCaseIdentifierInVariableExpression(identifier) && !isAllowed(identifier)) {
  119. context.report({
  120. node,
  121. message: "Unexpected dangling '_' in '{{identifier}}'.",
  122. data: {
  123. identifier
  124. }
  125. });
  126. }
  127. }
  128. /**
  129. * Check if member expression has a underscore at the end
  130. * @param {ASTNode} node node to evaluate
  131. * @returns {void}
  132. * @private
  133. */
  134. function checkForTrailingUnderscoreInMemberExpression(node) {
  135. const identifier = node.property.name,
  136. isMemberOfThis = node.object.type === "ThisExpression",
  137. isMemberOfSuper = node.object.type === "Super";
  138. if (typeof identifier !== "undefined" && hasTrailingUnderscore(identifier) &&
  139. !(isMemberOfThis && allowAfterThis) &&
  140. !(isMemberOfSuper && allowAfterSuper) &&
  141. !isSpecialCaseIdentifierForMemberExpression(identifier) && !isAllowed(identifier)) {
  142. context.report({
  143. node,
  144. message: "Unexpected dangling '_' in '{{identifier}}'.",
  145. data: {
  146. identifier
  147. }
  148. });
  149. }
  150. }
  151. /**
  152. * Check if method declaration or method property has a underscore at the end
  153. * @param {ASTNode} node node to evaluate
  154. * @returns {void}
  155. * @private
  156. */
  157. function checkForTrailingUnderscoreInMethod(node) {
  158. const identifier = node.key.name;
  159. const isMethod = node.type === "MethodDefinition" || node.type === "Property" && node.method;
  160. if (typeof identifier !== "undefined" && enforceInMethodNames && isMethod && hasTrailingUnderscore(identifier)) {
  161. context.report({
  162. node,
  163. message: "Unexpected dangling '_' in '{{identifier}}'.",
  164. data: {
  165. identifier
  166. }
  167. });
  168. }
  169. }
  170. //--------------------------------------------------------------------------
  171. // Public API
  172. //--------------------------------------------------------------------------
  173. return {
  174. FunctionDeclaration: checkForTrailingUnderscoreInFunctionDeclaration,
  175. VariableDeclarator: checkForTrailingUnderscoreInVariableExpression,
  176. MemberExpression: checkForTrailingUnderscoreInMemberExpression,
  177. MethodDefinition: checkForTrailingUnderscoreInMethod,
  178. Property: checkForTrailingUnderscoreInMethod
  179. };
  180. }
  181. };