id-blacklist.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * @fileoverview Rule that warns when identifier names that are
  3. * blacklisted in the configuration are used.
  4. * @author Keith Cirkel (http://keithcirkel.co.uk)
  5. */
  6. "use strict";
  7. //------------------------------------------------------------------------------
  8. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. module.exports = {
  11. meta: {
  12. type: "suggestion",
  13. docs: {
  14. description: "disallow specified identifiers",
  15. category: "Stylistic Issues",
  16. recommended: false,
  17. url: "https://eslint.org/docs/rules/id-blacklist"
  18. },
  19. schema: {
  20. type: "array",
  21. items: {
  22. type: "string"
  23. },
  24. uniqueItems: true
  25. },
  26. messages: {
  27. blacklisted: "Identifier '{{name}}' is blacklisted."
  28. }
  29. },
  30. create(context) {
  31. //--------------------------------------------------------------------------
  32. // Helpers
  33. //--------------------------------------------------------------------------
  34. const blacklist = context.options;
  35. /**
  36. * Checks if a string matches the provided pattern
  37. * @param {string} name The string to check.
  38. * @returns {boolean} if the string is a match
  39. * @private
  40. */
  41. function isInvalid(name) {
  42. return blacklist.indexOf(name) !== -1;
  43. }
  44. /**
  45. * Verifies if we should report an error or not based on the effective
  46. * parent node and the identifier name.
  47. * @param {ASTNode} effectiveParent The effective parent node of the node to be reported
  48. * @param {string} name The identifier name of the identifier node
  49. * @returns {boolean} whether an error should be reported or not
  50. */
  51. function shouldReport(effectiveParent, name) {
  52. return effectiveParent.type !== "CallExpression" &&
  53. effectiveParent.type !== "NewExpression" &&
  54. isInvalid(name);
  55. }
  56. /**
  57. * Reports an AST node as a rule violation.
  58. * @param {ASTNode} node The node to report.
  59. * @returns {void}
  60. * @private
  61. */
  62. function report(node) {
  63. context.report({
  64. node,
  65. messageId: "blacklisted",
  66. data: {
  67. name: node.name
  68. }
  69. });
  70. }
  71. return {
  72. Identifier(node) {
  73. const name = node.name,
  74. effectiveParent = (node.parent.type === "MemberExpression") ? node.parent.parent : node.parent;
  75. // MemberExpressions get special rules
  76. if (node.parent.type === "MemberExpression") {
  77. // Always check object names
  78. if (node.parent.object.type === "Identifier" &&
  79. node.parent.object.name === node.name) {
  80. if (isInvalid(name)) {
  81. report(node);
  82. }
  83. // Report AssignmentExpressions only if they are the left side of the assignment
  84. } else if (effectiveParent.type === "AssignmentExpression" &&
  85. (effectiveParent.right.type !== "MemberExpression" ||
  86. effectiveParent.left.type === "MemberExpression" &&
  87. effectiveParent.left.property.name === node.name)) {
  88. if (isInvalid(name)) {
  89. report(node);
  90. }
  91. }
  92. // Properties have their own rules
  93. } else if (node.parent.type === "Property") {
  94. if (shouldReport(effectiveParent, name)) {
  95. report(node);
  96. }
  97. // Report anything that is a match and not a CallExpression
  98. } else if (shouldReport(effectiveParent, name)) {
  99. report(node);
  100. }
  101. }
  102. };
  103. }
  104. };