catapult-camelcase.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright 2016 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. /* eslint-disable */
  5. /**
  6. * @fileoverview Rule to flag non-camelcased identifiers
  7. * @author Nicholas C. Zakas
  8. */
  9. 'use strict';
  10. //------------------------------------------------------------------------------
  11. // Rule Definition
  12. //------------------------------------------------------------------------------
  13. module.exports = {
  14. meta: {
  15. docs: {
  16. description: "enforce Catapult camelcase naming convention",
  17. category: "Stylistic Issues",
  18. recommended: false
  19. },
  20. schema: [
  21. {
  22. type: "object",
  23. properties: {
  24. properties: {
  25. enum: ["always", "never"]
  26. }
  27. },
  28. additionalProperties: false
  29. }
  30. ]
  31. },
  32. create(context) {
  33. //--------------------------------------------------------------------------
  34. // Helpers
  35. //--------------------------------------------------------------------------
  36. // contains reported nodes to avoid reporting twice on destructuring with shorthand notation
  37. var reported = [];
  38. /**
  39. * Checks if a string contains an underscore and isn't all upper-case
  40. * @param {string} name The string to check.
  41. * @returns {boolean} if the string is underscored
  42. * @private
  43. */
  44. function isUnderscored(name) {
  45. // if there's an underscore, it might be A_VARANT, which is okay
  46. return name.indexOf("_") > -1 && name !== name.toUpperCase();
  47. }
  48. /**
  49. * Reports an AST node as a rule violation.
  50. * @param {ASTNode} node The node to report.
  51. * @returns {void}
  52. * @private
  53. */
  54. function report(node) {
  55. if (reported.indexOf(node) < 0) {
  56. reported.push(node);
  57. context.report(node, "Identifier '{{name}}' is not in camel case.", { name: node.name });
  58. }
  59. }
  60. var options = context.options[0] || {};
  61. let properties = options.properties || "";
  62. if (properties !== "always" && properties !== "never") {
  63. properties = "always";
  64. }
  65. return {
  66. Identifier(node) {
  67. /*
  68. * Leading and trailing underscores are commonly used to flag
  69. * private/protected identifiers, strip them.
  70. *
  71. * NOTE: This has four Catapult-specific style exceptions:
  72. *
  73. * - The prefix opt_
  74. * - The prefix g_
  75. * - The suffix _smallerIsBetter
  76. * - The suffix _biggerIsBetter
  77. */
  78. var name = node.name.replace(/(?:^opt_)|^(?:^g_)|^_+|_+$|(?:_smallerIsBetter)$|(?:_biggerIsBetter)$/g, ""),
  79. effectiveParent = (node.parent.type === "MemberExpression") ? node.parent.parent : node.parent;
  80. // MemberExpressions get special rules
  81. if (node.parent.type === "MemberExpression") {
  82. // "never" check properties
  83. if (properties === "never") {
  84. return;
  85. }
  86. // Always report underscored object names
  87. if (node.parent.object.type === "Identifier" &&
  88. node.parent.object.name === node.name &&
  89. isUnderscored(name)) {
  90. report(node);
  91. // Report AssignmentExpressions only if they are the left side of the assignment
  92. } else if (effectiveParent.type === "AssignmentExpression" &&
  93. isUnderscored(name) &&
  94. (effectiveParent.right.type !== "MemberExpression" ||
  95. effectiveParent.left.type === "MemberExpression" &&
  96. effectiveParent.left.property.name === node.name)) {
  97. report(node);
  98. }
  99. // Properties have their own rules
  100. } else if (node.parent.type === "Property") {
  101. // "never" check properties
  102. if (properties === "never") {
  103. return;
  104. }
  105. if (node.parent.parent && node.parent.parent.type === "ObjectPattern" &&
  106. node.parent.key === node && node.parent.value !== node) {
  107. return;
  108. }
  109. if (isUnderscored(name) && effectiveParent.type !== "CallExpression") {
  110. report(node);
  111. }
  112. // Check if it's an import specifier
  113. } else if (["ImportSpecifier", "ImportNamespaceSpecifier", "ImportDefaultSpecifier"].indexOf(node.parent.type) >= 0) {
  114. // Report only if the local imported identifier is underscored
  115. if (node.parent.local && node.parent.local.name === node.name && isUnderscored(name)) {
  116. report(node);
  117. }
  118. // Report anything that is underscored that isn't a CallExpression
  119. } else if (isUnderscored(name) && effectiveParent.type !== "CallExpression") {
  120. report(node);
  121. }
  122. }
  123. };
  124. }
  125. };