camelcase.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /**
  2. * @fileoverview Rule to flag non-camelcased identifiers
  3. * @author Nicholas C. Zakas
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. type: "suggestion",
  12. docs: {
  13. description: "enforce camelcase naming convention",
  14. category: "Stylistic Issues",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/camelcase"
  17. },
  18. schema: [
  19. {
  20. type: "object",
  21. properties: {
  22. ignoreDestructuring: {
  23. type: "boolean",
  24. default: false
  25. },
  26. properties: {
  27. enum: ["always", "never"]
  28. },
  29. allow: {
  30. type: "array",
  31. items: [
  32. {
  33. type: "string"
  34. }
  35. ],
  36. minItems: 0,
  37. uniqueItems: true
  38. }
  39. },
  40. additionalProperties: false
  41. }
  42. ],
  43. messages: {
  44. notCamelCase: "Identifier '{{name}}' is not in camel case."
  45. }
  46. },
  47. create(context) {
  48. const options = context.options[0] || {};
  49. let properties = options.properties || "";
  50. const ignoreDestructuring = options.ignoreDestructuring;
  51. const allow = options.allow || [];
  52. if (properties !== "always" && properties !== "never") {
  53. properties = "always";
  54. }
  55. //--------------------------------------------------------------------------
  56. // Helpers
  57. //--------------------------------------------------------------------------
  58. // contains reported nodes to avoid reporting twice on destructuring with shorthand notation
  59. const reported = [];
  60. const ALLOWED_PARENT_TYPES = new Set(["CallExpression", "NewExpression"]);
  61. /**
  62. * Checks if a string contains an underscore and isn't all upper-case
  63. * @param {string} name The string to check.
  64. * @returns {boolean} if the string is underscored
  65. * @private
  66. */
  67. function isUnderscored(name) {
  68. // if there's an underscore, it might be A_CONSTANT, which is okay
  69. return name.indexOf("_") > -1 && name !== name.toUpperCase();
  70. }
  71. /**
  72. * Checks if a string match the ignore list
  73. * @param {string} name The string to check.
  74. * @returns {boolean} if the string is ignored
  75. * @private
  76. */
  77. function isAllowed(name) {
  78. return allow.findIndex(
  79. entry => name === entry || name.match(new RegExp(entry)) // eslint-disable-line require-unicode-regexp
  80. ) !== -1;
  81. }
  82. /**
  83. * Checks if a parent of a node is an ObjectPattern.
  84. * @param {ASTNode} node The node to check.
  85. * @returns {boolean} if the node is inside an ObjectPattern
  86. * @private
  87. */
  88. function isInsideObjectPattern(node) {
  89. let current = node;
  90. while (current) {
  91. const parent = current.parent;
  92. if (parent && parent.type === "Property" && parent.computed && parent.key === current) {
  93. return false;
  94. }
  95. if (current.type === "ObjectPattern") {
  96. return true;
  97. }
  98. current = parent;
  99. }
  100. return false;
  101. }
  102. /**
  103. * Reports an AST node as a rule violation.
  104. * @param {ASTNode} node The node to report.
  105. * @returns {void}
  106. * @private
  107. */
  108. function report(node) {
  109. if (reported.indexOf(node) < 0) {
  110. reported.push(node);
  111. context.report({ node, messageId: "notCamelCase", data: { name: node.name } });
  112. }
  113. }
  114. return {
  115. Identifier(node) {
  116. /*
  117. * Leading and trailing underscores are commonly used to flag
  118. * private/protected identifiers, strip them before checking if underscored
  119. */
  120. const name = node.name,
  121. nameIsUnderscored = isUnderscored(name.replace(/^_+|_+$/gu, "")),
  122. effectiveParent = (node.parent.type === "MemberExpression") ? node.parent.parent : node.parent;
  123. // First, we ignore the node if it match the ignore list
  124. if (isAllowed(name)) {
  125. return;
  126. }
  127. // MemberExpressions get special rules
  128. if (node.parent.type === "MemberExpression") {
  129. // "never" check properties
  130. if (properties === "never") {
  131. return;
  132. }
  133. // Always report underscored object names
  134. if (node.parent.object.type === "Identifier" && node.parent.object.name === node.name && nameIsUnderscored) {
  135. report(node);
  136. // Report AssignmentExpressions only if they are the left side of the assignment
  137. } else if (effectiveParent.type === "AssignmentExpression" && nameIsUnderscored && (effectiveParent.right.type !== "MemberExpression" || effectiveParent.left.type === "MemberExpression" && effectiveParent.left.property.name === node.name)) {
  138. report(node);
  139. }
  140. /*
  141. * Properties have their own rules, and
  142. * AssignmentPattern nodes can be treated like Properties:
  143. * e.g.: const { no_camelcased = false } = bar;
  144. */
  145. } else if (node.parent.type === "Property" || node.parent.type === "AssignmentPattern") {
  146. if (node.parent.parent && node.parent.parent.type === "ObjectPattern") {
  147. if (node.parent.shorthand && node.parent.value.left && nameIsUnderscored) {
  148. report(node);
  149. }
  150. const assignmentKeyEqualsValue = node.parent.key.name === node.parent.value.name;
  151. if (isUnderscored(name) && node.parent.computed) {
  152. report(node);
  153. }
  154. // prevent checking righthand side of destructured object
  155. if (node.parent.key === node && node.parent.value !== node) {
  156. return;
  157. }
  158. const valueIsUnderscored = node.parent.value.name && nameIsUnderscored;
  159. // ignore destructuring if the option is set, unless a new identifier is created
  160. if (valueIsUnderscored && !(assignmentKeyEqualsValue && ignoreDestructuring)) {
  161. report(node);
  162. }
  163. }
  164. // "never" check properties or always ignore destructuring
  165. if (properties === "never" || (ignoreDestructuring && isInsideObjectPattern(node))) {
  166. return;
  167. }
  168. // don't check right hand side of AssignmentExpression to prevent duplicate warnings
  169. if (nameIsUnderscored && !ALLOWED_PARENT_TYPES.has(effectiveParent.type) && !(node.parent.right === node)) {
  170. report(node);
  171. }
  172. // Check if it's an import specifier
  173. } else if (["ImportSpecifier", "ImportNamespaceSpecifier", "ImportDefaultSpecifier"].indexOf(node.parent.type) >= 0) {
  174. // Report only if the local imported identifier is underscored
  175. if (node.parent.local && node.parent.local.name === node.name && nameIsUnderscored) {
  176. report(node);
  177. }
  178. // Report anything that is underscored that isn't a CallExpression
  179. } else if (nameIsUnderscored && !ALLOWED_PARENT_TYPES.has(effectiveParent.type)) {
  180. report(node);
  181. }
  182. }
  183. };
  184. }
  185. };