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
39 const config = context.options[0];
40 const exceptions = (config && config.exceptions) || [];
43 * Reports write references.
44 * @param {Reference} reference A reference to check.
45 * @param {int} index The index of the reference in the references.
46 * @param {Reference[]} references The array that the reference belongs to.
49 function checkReference(reference, index, references) {
50 const identifier = reference.identifier;
52 if (reference.init === false &&
53 reference.isWrite() &&
56 * Destructuring assignments can have multiple default value,
57 * so possibly there are multiple writeable references for the same identifier.
59 (index === 0 || references[index - 1].identifier !== identifier)
63 message: "Read-only global '{{name}}' should not be modified.",
70 * Reports write references if a given variable is read-only builtin.
71 * @param {Variable} variable A variable to check.
74 function checkVariable(variable) {
75 if (variable.writeable === false && exceptions.indexOf(variable.name) === -1) {
76 variable.references.forEach(checkReference);
82 const globalScope = context.getScope();
84 globalScope.variables.forEach(checkVariable);