PageRenderTime 1775ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/node_modules/eslint/lib/rules/no-underscore-dangle.js

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