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-global-assign.js 2.8KB

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