no-underscore-dangle.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. 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. },
  45. additionalProperties: false
  46. }
  47. ]
  48. },
  49. create(context) {
  50. const options = context.options[0] || {};
  51. const ALLOWED_VARIABLES = options.allow ? options.allow : [];
  52. const allowAfterThis = typeof options.allowAfterThis !== "undefined" ? options.allowAfterThis : false;
  53. const allowAfterSuper = typeof options.allowAfterSuper !== "undefined" ? options.allowAfterSuper : false;
  54. const allowAfterThisConstructor = typeof options.allowAfterThisConstructor !== "undefined" ? options.allowAfterThisConstructor : false;
  55. const enforceInMethodNames = typeof options.enforceInMethodNames !== "undefined" ? options.enforceInMethodNames : false;
  56. //-------------------------------------------------------------------------
  57. // Helpers
  58. //-------------------------------------------------------------------------
  59. /**
  60. * Check if identifier is present inside the allowed option
  61. * @param {string} identifier name of the node
  62. * @returns {boolean} true if its is present
  63. * @private
  64. */
  65. function isAllowed(identifier) {
  66. return ALLOWED_VARIABLES.some(ident => ident === identifier);
  67. }
  68. /**
  69. * Check if identifier has a underscore at the end
  70. * @param {string} identifier name of the node
  71. * @returns {boolean} true if its is present
  72. * @private
  73. */
  74. function hasTrailingUnderscore(identifier) {
  75. const len = identifier.length;
  76. return identifier !== "_" && (identifier[0] === "_" || identifier[len - 1] === "_");
  77. }
  78. /**
  79. * Check if identifier is a special case member expression
  80. * @param {string} identifier name of the node
  81. * @returns {boolean} true if its is a special case
  82. * @private
  83. */
  84. function isSpecialCaseIdentifierForMemberExpression(identifier) {
  85. return identifier === "__proto__";
  86. }
  87. /**
  88. * Check if identifier is a special case variable expression
  89. * @param {string} identifier name of the node
  90. * @returns {boolean} true if its is a special case
  91. * @private
  92. */
  93. function isSpecialCaseIdentifierInVariableExpression(identifier) {
  94. // Checks for the underscore library usage here
  95. return identifier === "_";
  96. }
  97. /**
  98. * Check if a node is a member reference of this.constructor
  99. * @param {ASTNode} node node to evaluate
  100. * @returns {boolean} true if it is a reference on this.constructor
  101. * @private
  102. */
  103. function isThisConstructorReference(node) {
  104. return node.object.type === "MemberExpression" &&
  105. node.object.property.name === "constructor" &&
  106. node.object.object.type === "ThisExpression";
  107. }
  108. /**
  109. * Check if function has a underscore at the end
  110. * @param {ASTNode} node node to evaluate
  111. * @returns {void}
  112. * @private
  113. */
  114. function checkForTrailingUnderscoreInFunctionDeclaration(node) {
  115. if (node.id) {
  116. const identifier = node.id.name;
  117. if (typeof identifier !== "undefined" && hasTrailingUnderscore(identifier) && !isAllowed(identifier)) {
  118. context.report({
  119. node,
  120. message: "Unexpected dangling '_' in '{{identifier}}'.",
  121. data: {
  122. identifier
  123. }
  124. });
  125. }
  126. }
  127. }
  128. /**
  129. * Check if variable expression has a underscore at the end
  130. * @param {ASTNode} node node to evaluate
  131. * @returns {void}
  132. * @private
  133. */
  134. function checkForTrailingUnderscoreInVariableExpression(node) {
  135. const identifier = node.id.name;
  136. if (typeof identifier !== "undefined" && hasTrailingUnderscore(identifier) &&
  137. !isSpecialCaseIdentifierInVariableExpression(identifier) && !isAllowed(identifier)) {
  138. context.report({
  139. node,
  140. message: "Unexpected dangling '_' in '{{identifier}}'.",
  141. data: {
  142. identifier
  143. }
  144. });
  145. }
  146. }
  147. /**
  148. * Check if member expression has a underscore at the end
  149. * @param {ASTNode} node node to evaluate
  150. * @returns {void}
  151. * @private
  152. */
  153. function checkForTrailingUnderscoreInMemberExpression(node) {
  154. const identifier = node.property.name,
  155. isMemberOfThis = node.object.type === "ThisExpression",
  156. isMemberOfSuper = node.object.type === "Super",
  157. isMemberOfThisConstructor = isThisConstructorReference(node);
  158. if (typeof identifier !== "undefined" && hasTrailingUnderscore(identifier) &&
  159. !(isMemberOfThis && allowAfterThis) &&
  160. !(isMemberOfSuper && allowAfterSuper) &&
  161. !(isMemberOfThisConstructor && allowAfterThisConstructor) &&
  162. !isSpecialCaseIdentifierForMemberExpression(identifier) && !isAllowed(identifier)) {
  163. context.report({
  164. node,
  165. message: "Unexpected dangling '_' in '{{identifier}}'.",
  166. data: {
  167. identifier
  168. }
  169. });
  170. }
  171. }
  172. /**
  173. * Check if method declaration or method property has a underscore at the end
  174. * @param {ASTNode} node node to evaluate
  175. * @returns {void}
  176. * @private
  177. */
  178. function checkForTrailingUnderscoreInMethod(node) {
  179. const identifier = node.key.name;
  180. const isMethod = node.type === "MethodDefinition" || node.type === "Property" && node.method;
  181. if (typeof identifier !== "undefined" && enforceInMethodNames && isMethod && hasTrailingUnderscore(identifier)) {
  182. context.report({
  183. node,
  184. message: "Unexpected dangling '_' in '{{identifier}}'.",
  185. data: {
  186. identifier
  187. }
  188. });
  189. }
  190. }
  191. //--------------------------------------------------------------------------
  192. // Public API
  193. //--------------------------------------------------------------------------
  194. return {
  195. FunctionDeclaration: checkForTrailingUnderscoreInFunctionDeclaration,
  196. VariableDeclarator: checkForTrailingUnderscoreInVariableExpression,
  197. MemberExpression: checkForTrailingUnderscoreInMemberExpression,
  198. MethodDefinition: checkForTrailingUnderscoreInMethod,
  199. Property: checkForTrailingUnderscoreInMethod
  200. };
  201. }
  202. };