2 * @fileoverview Rule to disallow assignments to native objects or read-only global variables
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
17 description: "disallow assignments to native objects or read-only global variables",
18 category: "Best Practices",
20 url: "https://eslint.org/docs/rules/no-global-assign"
29 items: { type: "string" },
33 additionalProperties: false
38 globalShouldNotBeModified: "Read-only global '{{name}}' should not be modified."
43 const config = context.options[0];
44 const exceptions = (config && config.exceptions) || [];
47 * Reports write references.
48 * @param {Reference} reference A reference to check.
49 * @param {int} index The index of the reference in the references.
50 * @param {Reference[]} references The array that the reference belongs to.
53 function checkReference(reference, index, references) {
54 const identifier = reference.identifier;
56 if (reference.init === false &&
57 reference.isWrite() &&
60 * Destructuring assignments can have multiple default value,
61 * so possibly there are multiple writeable references for the same identifier.
63 (index === 0 || references[index - 1].identifier !== identifier)
67 messageId: "globalShouldNotBeModified",
76 * Reports write references if a given variable is read-only builtin.
77 * @param {Variable} variable A variable to check.
80 function checkVariable(variable) {
81 if (variable.writeable === false && exceptions.indexOf(variable.name) === -1) {
82 variable.references.forEach(checkReference);
88 const globalScope = context.getScope();
90 globalScope.variables.forEach(checkVariable);