Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

id-blacklist.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. },
  27. create(context) {
  28. //--------------------------------------------------------------------------
  29. // Helpers
  30. //--------------------------------------------------------------------------
  31. const blacklist = context.options;
  32. /**
  33. * Checks if a string matches the provided pattern
  34. * @param {string} name The string to check.
  35. * @returns {boolean} if the string is a match
  36. * @private
  37. */
  38. function isInvalid(name) {
  39. return blacklist.indexOf(name) !== -1;
  40. }
  41. /**
  42. * Verifies if we should report an error or not based on the effective
  43. * parent node and the identifier name.
  44. * @param {ASTNode} effectiveParent The effective parent node of the node to be reported
  45. * @param {string} name The identifier name of the identifier node
  46. * @returns {boolean} whether an error should be reported or not
  47. */
  48. function shouldReport(effectiveParent, name) {
  49. return effectiveParent.type !== "CallExpression" &&
  50. effectiveParent.type !== "NewExpression" &&
  51. isInvalid(name);
  52. }
  53. /**
  54. * Reports an AST node as a rule violation.
  55. * @param {ASTNode} node The node to report.
  56. * @returns {void}
  57. * @private
  58. */
  59. function report(node) {
  60. context.report({
  61. node,
  62. message: "Identifier '{{name}}' is blacklisted.",
  63. data: {
  64. name: node.name
  65. }
  66. });
  67. }
  68. return {
  69. Identifier(node) {
  70. const name = node.name,
  71. effectiveParent = (node.parent.type === "MemberExpression") ? node.parent.parent : node.parent;
  72. // MemberExpressions get special rules
  73. if (node.parent.type === "MemberExpression") {
  74. // Always check object names
  75. if (node.parent.object.type === "Identifier" &&
  76. node.parent.object.name === node.name) {
  77. if (isInvalid(name)) {
  78. report(node);
  79. }
  80. // Report AssignmentExpressions only if they are the left side of the assignment
  81. } else if (effectiveParent.type === "AssignmentExpression" &&
  82. (effectiveParent.right.type !== "MemberExpression" ||
  83. effectiveParent.left.type === "MemberExpression" &&
  84. effectiveParent.left.property.name === node.name)) {
  85. if (isInvalid(name)) {
  86. report(node);
  87. }
  88. }
  89. // Properties have their own rules
  90. } else if (node.parent.type === "Property") {
  91. if (shouldReport(effectiveParent, name)) {
  92. report(node);
  93. }
  94. // Report anything that is a match and not a CallExpression
  95. } else if (shouldReport(effectiveParent, name)) {
  96. report(node);
  97. }
  98. }
  99. };
  100. }
  101. };