camelcase.js 8.7 KB

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