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-native-reassign.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * @fileoverview Rule to disallow assignments to native objects or read-only global variables
  3. * @author Ilya Volodin
  4. * @deprecated in ESLint v3.3.0
  5. */
  6. "use strict";
  7. //------------------------------------------------------------------------------
  8. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. module.exports = {
  11. meta: {
  12. type: "suggestion",
  13. docs: {
  14. description: "disallow assignments to native objects or read-only global variables",
  15. category: "Best Practices",
  16. recommended: false,
  17. url: "https://eslint.org/docs/rules/no-native-reassign"
  18. },
  19. deprecated: true,
  20. replacedBy: ["no-global-assign"],
  21. schema: [
  22. {
  23. type: "object",
  24. properties: {
  25. exceptions: {
  26. type: "array",
  27. items: { type: "string" },
  28. uniqueItems: true
  29. }
  30. },
  31. additionalProperties: false
  32. }
  33. ]
  34. },
  35. create(context) {
  36. const config = context.options[0];
  37. const exceptions = (config && config.exceptions) || [];
  38. /**
  39. * Reports write references.
  40. * @param {Reference} reference - A reference to check.
  41. * @param {int} index - The index of the reference in the references.
  42. * @param {Reference[]} references - The array that the reference belongs to.
  43. * @returns {void}
  44. */
  45. function checkReference(reference, index, references) {
  46. const identifier = reference.identifier;
  47. if (reference.init === false &&
  48. reference.isWrite() &&
  49. /*
  50. * Destructuring assignments can have multiple default value,
  51. * so possibly there are multiple writeable references for the same identifier.
  52. */
  53. (index === 0 || references[index - 1].identifier !== identifier)
  54. ) {
  55. context.report({
  56. node: identifier,
  57. message: "Read-only global '{{name}}' should not be modified.",
  58. data: identifier
  59. });
  60. }
  61. }
  62. /**
  63. * Reports write references if a given variable is read-only builtin.
  64. * @param {Variable} variable - A variable to check.
  65. * @returns {void}
  66. */
  67. function checkVariable(variable) {
  68. if (variable.writeable === false && exceptions.indexOf(variable.name) === -1) {
  69. variable.references.forEach(checkReference);
  70. }
  71. }
  72. return {
  73. Program() {
  74. const globalScope = context.getScope();
  75. globalScope.variables.forEach(checkVariable);
  76. }
  77. };
  78. }
  79. };