项目原始demo,不改动
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

no-global-assign.js 2.7 KiB

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