Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /**
  2. * @fileoverview Rule to flag dangling 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. default: false
  31. },
  32. allowAfterSuper: {
  33. type: "boolean",
  34. default: false
  35. },
  36. allowAfterThisConstructor: {
  37. type: "boolean",
  38. default: false
  39. },
  40. enforceInMethodNames: {
  41. type: "boolean",
  42. default: false
  43. },
  44. allowFunctionParams: {
  45. type: "boolean",
  46. default: true
  47. }
  48. },
  49. additionalProperties: false
  50. }
  51. ],
  52. messages: {
  53. unexpectedUnderscore: "Unexpected dangling '_' in '{{identifier}}'."
  54. }
  55. },
  56. create(context) {
  57. const options = context.options[0] || {};
  58. const ALLOWED_VARIABLES = options.allow ? options.allow : [];
  59. const allowAfterThis = typeof options.allowAfterThis !== "undefined" ? options.allowAfterThis : false;
  60. const allowAfterSuper = typeof options.allowAfterSuper !== "undefined" ? options.allowAfterSuper : false;
  61. const allowAfterThisConstructor = typeof options.allowAfterThisConstructor !== "undefined" ? options.allowAfterThisConstructor : false;
  62. const enforceInMethodNames = typeof options.enforceInMethodNames !== "undefined" ? options.enforceInMethodNames : false;
  63. const allowFunctionParams = typeof options.allowFunctionParams !== "undefined" ? options.allowFunctionParams : true;
  64. //-------------------------------------------------------------------------
  65. // Helpers
  66. //-------------------------------------------------------------------------
  67. /**
  68. * Check if identifier is present inside the allowed option
  69. * @param {string} identifier name of the node
  70. * @returns {boolean} true if its is present
  71. * @private
  72. */
  73. function isAllowed(identifier) {
  74. return ALLOWED_VARIABLES.some(ident => ident === identifier);
  75. }
  76. /**
  77. * Check if identifier has a dangling underscore
  78. * @param {string} identifier name of the node
  79. * @returns {boolean} true if its is present
  80. * @private
  81. */
  82. function hasDanglingUnderscore(identifier) {
  83. const len = identifier.length;
  84. return identifier !== "_" && (identifier[0] === "_" || identifier[len - 1] === "_");
  85. }
  86. /**
  87. * Check if identifier is a special case member expression
  88. * @param {string} identifier name of the node
  89. * @returns {boolean} true if its is a special case
  90. * @private
  91. */
  92. function isSpecialCaseIdentifierForMemberExpression(identifier) {
  93. return identifier === "__proto__";
  94. }
  95. /**
  96. * Check if identifier is a special case variable expression
  97. * @param {string} identifier name of the node
  98. * @returns {boolean} true if its is a special case
  99. * @private
  100. */
  101. function isSpecialCaseIdentifierInVariableExpression(identifier) {
  102. // Checks for the underscore library usage here
  103. return identifier === "_";
  104. }
  105. /**
  106. * Check if a node is a member reference of this.constructor
  107. * @param {ASTNode} node node to evaluate
  108. * @returns {boolean} true if it is a reference on this.constructor
  109. * @private
  110. */
  111. function isThisConstructorReference(node) {
  112. return node.object.type === "MemberExpression" &&
  113. node.object.property.name === "constructor" &&
  114. node.object.object.type === "ThisExpression";
  115. }
  116. /**
  117. * Check if function parameter has a dangling underscore.
  118. * @param {ASTNode} node function node to evaluate
  119. * @returns {void}
  120. * @private
  121. */
  122. function checkForDanglingUnderscoreInFunctionParameters(node) {
  123. if (!allowFunctionParams) {
  124. node.params.forEach(param => {
  125. const { type } = param;
  126. let nodeToCheck;
  127. if (type === "RestElement") {
  128. nodeToCheck = param.argument;
  129. } else if (type === "AssignmentPattern") {
  130. nodeToCheck = param.left;
  131. } else {
  132. nodeToCheck = param;
  133. }
  134. if (nodeToCheck.type === "Identifier") {
  135. const identifier = nodeToCheck.name;
  136. if (hasDanglingUnderscore(identifier) && !isAllowed(identifier)) {
  137. context.report({
  138. node: param,
  139. messageId: "unexpectedUnderscore",
  140. data: {
  141. identifier
  142. }
  143. });
  144. }
  145. }
  146. });
  147. }
  148. }
  149. /**
  150. * Check if function has a dangling underscore
  151. * @param {ASTNode} node node to evaluate
  152. * @returns {void}
  153. * @private
  154. */
  155. function checkForDanglingUnderscoreInFunction(node) {
  156. if (node.type === "FunctionDeclaration" && node.id) {
  157. const identifier = node.id.name;
  158. if (typeof identifier !== "undefined" && hasDanglingUnderscore(identifier) && !isAllowed(identifier)) {
  159. context.report({
  160. node,
  161. messageId: "unexpectedUnderscore",
  162. data: {
  163. identifier
  164. }
  165. });
  166. }
  167. }
  168. checkForDanglingUnderscoreInFunctionParameters(node);
  169. }
  170. /**
  171. * Check if variable expression has a dangling underscore
  172. * @param {ASTNode} node node to evaluate
  173. * @returns {void}
  174. * @private
  175. */
  176. function checkForDanglingUnderscoreInVariableExpression(node) {
  177. const identifier = node.id.name;
  178. if (typeof identifier !== "undefined" && hasDanglingUnderscore(identifier) &&
  179. !isSpecialCaseIdentifierInVariableExpression(identifier) && !isAllowed(identifier)) {
  180. context.report({
  181. node,
  182. messageId: "unexpectedUnderscore",
  183. data: {
  184. identifier
  185. }
  186. });
  187. }
  188. }
  189. /**
  190. * Check if member expression has a dangling underscore
  191. * @param {ASTNode} node node to evaluate
  192. * @returns {void}
  193. * @private
  194. */
  195. function checkForDanglingUnderscoreInMemberExpression(node) {
  196. const identifier = node.property.name,
  197. isMemberOfThis = node.object.type === "ThisExpression",
  198. isMemberOfSuper = node.object.type === "Super",
  199. isMemberOfThisConstructor = isThisConstructorReference(node);
  200. if (typeof identifier !== "undefined" && hasDanglingUnderscore(identifier) &&
  201. !(isMemberOfThis && allowAfterThis) &&
  202. !(isMemberOfSuper && allowAfterSuper) &&
  203. !(isMemberOfThisConstructor && allowAfterThisConstructor) &&
  204. !isSpecialCaseIdentifierForMemberExpression(identifier) && !isAllowed(identifier)) {
  205. context.report({
  206. node,
  207. messageId: "unexpectedUnderscore",
  208. data: {
  209. identifier
  210. }
  211. });
  212. }
  213. }
  214. /**
  215. * Check if method declaration or method property has a dangling underscore
  216. * @param {ASTNode} node node to evaluate
  217. * @returns {void}
  218. * @private
  219. */
  220. function checkForDanglingUnderscoreInMethod(node) {
  221. const identifier = node.key.name;
  222. const isMethod = node.type === "MethodDefinition" || node.type === "Property" && node.method;
  223. if (typeof identifier !== "undefined" && enforceInMethodNames && isMethod && hasDanglingUnderscore(identifier) && !isAllowed(identifier)) {
  224. context.report({
  225. node,
  226. messageId: "unexpectedUnderscore",
  227. data: {
  228. identifier
  229. }
  230. });
  231. }
  232. }
  233. //--------------------------------------------------------------------------
  234. // Public API
  235. //--------------------------------------------------------------------------
  236. return {
  237. FunctionDeclaration: checkForDanglingUnderscoreInFunction,
  238. VariableDeclarator: checkForDanglingUnderscoreInVariableExpression,
  239. MemberExpression: checkForDanglingUnderscoreInMemberExpression,
  240. MethodDefinition: checkForDanglingUnderscoreInMethod,
  241. Property: checkForDanglingUnderscoreInMethod,
  242. FunctionExpression: checkForDanglingUnderscoreInFunction,
  243. ArrowFunctionExpression: checkForDanglingUnderscoreInFunction
  244. };
  245. }
  246. };