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.

prefer-reflect.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * @fileoverview Rule to suggest using "Reflect" api over Function/Object methods
  3. * @author Keith Cirkel <http://keithcirkel.co.uk>
  4. * @deprecated in ESLint v3.9.0
  5. */
  6. "use strict";
  7. //------------------------------------------------------------------------------
  8. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. module.exports = {
  11. meta: {
  12. type: "suggestion",
  13. docs: {
  14. description: "require `Reflect` methods where applicable",
  15. category: "ECMAScript 6",
  16. recommended: false,
  17. url: "https://eslint.org/docs/rules/prefer-reflect"
  18. },
  19. deprecated: true,
  20. replacedBy: [],
  21. schema: [
  22. {
  23. type: "object",
  24. properties: {
  25. exceptions: {
  26. type: "array",
  27. items: {
  28. enum: [
  29. "apply",
  30. "call",
  31. "delete",
  32. "defineProperty",
  33. "getOwnPropertyDescriptor",
  34. "getPrototypeOf",
  35. "setPrototypeOf",
  36. "isExtensible",
  37. "getOwnPropertyNames",
  38. "preventExtensions"
  39. ]
  40. },
  41. uniqueItems: true
  42. }
  43. },
  44. additionalProperties: false
  45. }
  46. ]
  47. },
  48. create(context) {
  49. const existingNames = {
  50. apply: "Function.prototype.apply",
  51. call: "Function.prototype.call",
  52. defineProperty: "Object.defineProperty",
  53. getOwnPropertyDescriptor: "Object.getOwnPropertyDescriptor",
  54. getPrototypeOf: "Object.getPrototypeOf",
  55. setPrototypeOf: "Object.setPrototypeOf",
  56. isExtensible: "Object.isExtensible",
  57. getOwnPropertyNames: "Object.getOwnPropertyNames",
  58. preventExtensions: "Object.preventExtensions"
  59. };
  60. const reflectSubsitutes = {
  61. apply: "Reflect.apply",
  62. call: "Reflect.apply",
  63. defineProperty: "Reflect.defineProperty",
  64. getOwnPropertyDescriptor: "Reflect.getOwnPropertyDescriptor",
  65. getPrototypeOf: "Reflect.getPrototypeOf",
  66. setPrototypeOf: "Reflect.setPrototypeOf",
  67. isExtensible: "Reflect.isExtensible",
  68. getOwnPropertyNames: "Reflect.getOwnPropertyNames",
  69. preventExtensions: "Reflect.preventExtensions"
  70. };
  71. const exceptions = (context.options[0] || {}).exceptions || [];
  72. /**
  73. * Reports the Reflect violation based on the `existing` and `substitute`
  74. * @param {Object} node The node that violates the rule.
  75. * @param {string} existing The existing method name that has been used.
  76. * @param {string} substitute The Reflect substitute that should be used.
  77. * @returns {void}
  78. */
  79. function report(node, existing, substitute) {
  80. context.report({
  81. node,
  82. message: "Avoid using {{existing}}, instead use {{substitute}}.",
  83. data: {
  84. existing,
  85. substitute
  86. }
  87. });
  88. }
  89. return {
  90. CallExpression(node) {
  91. const methodName = (node.callee.property || {}).name;
  92. const isReflectCall = (node.callee.object || {}).name === "Reflect";
  93. const hasReflectSubsitute = Object.prototype.hasOwnProperty.call(reflectSubsitutes, methodName);
  94. const userConfiguredException = exceptions.indexOf(methodName) !== -1;
  95. if (hasReflectSubsitute && !isReflectCall && !userConfiguredException) {
  96. report(node, existingNames[methodName], reflectSubsitutes[methodName]);
  97. }
  98. },
  99. UnaryExpression(node) {
  100. const isDeleteOperator = node.operator === "delete";
  101. const targetsIdentifier = node.argument.type === "Identifier";
  102. const userConfiguredException = exceptions.indexOf("delete") !== -1;
  103. if (isDeleteOperator && !targetsIdentifier && !userConfiguredException) {
  104. report(node, "the delete keyword", "Reflect.deleteProperty");
  105. }
  106. }
  107. };
  108. }
  109. };