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.

no-prototype-builtins.js 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * @fileoverview Rule to disallow use of Object.prototype builtins on objects
  3. * @author Andrew Levine
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. type: "problem",
  12. docs: {
  13. description: "disallow calling some `Object.prototype` methods directly on objects",
  14. category: "Possible Errors",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/no-prototype-builtins"
  17. },
  18. schema: []
  19. },
  20. create(context) {
  21. const DISALLOWED_PROPS = [
  22. "hasOwnProperty",
  23. "isPrototypeOf",
  24. "propertyIsEnumerable"
  25. ];
  26. /**
  27. * Reports if a disallowed property is used in a CallExpression
  28. * @param {ASTNode} node The CallExpression node.
  29. * @returns {void}
  30. */
  31. function disallowBuiltIns(node) {
  32. if (node.callee.type !== "MemberExpression" || node.callee.computed) {
  33. return;
  34. }
  35. const propName = node.callee.property.name;
  36. if (DISALLOWED_PROPS.indexOf(propName) > -1) {
  37. context.report({
  38. message: "Do not access Object.prototype method '{{prop}}' from target object.",
  39. loc: node.callee.property.loc.start,
  40. data: { prop: propName },
  41. node
  42. });
  43. }
  44. }
  45. return {
  46. CallExpression: disallowBuiltIns
  47. };
  48. }
  49. };