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",
19 url
: "https://eslint.org/docs/rules/no-global-assign"
28 items
: { type
: "string" },
32 additionalProperties
: false
37 globalShouldNotBeModified
: "Read-only global '{{name}}' should not be modified."
42 const config
= context
.options
[0];
43 const exceptions
= (config
&& config
.exceptions
) || [];
46 * Reports write references.
47 * @param {Reference} reference A reference to check.
48 * @param {int} index The index of the reference in the references.
49 * @param {Reference[]} references The array that the reference belongs to.
52 function checkReference(reference
, index
, references
) {
53 const identifier
= reference
.identifier
;
55 if (reference
.init
=== false &&
56 reference
.isWrite() &&
59 * Destructuring assignments can have multiple default value,
60 * so possibly there are multiple writeable references for the same identifier.
62 (index
=== 0 || references
[index
- 1].identifier
!== identifier
)
66 messageId
: "globalShouldNotBeModified",
75 * Reports write references if a given variable is read-only builtin.
76 * @param {Variable} variable A variable to check.
79 function checkVariable(variable
) {
80 if (variable
.writeable
=== false && exceptions
.indexOf(variable
.name
) === -1) {
81 variable
.references
.forEach(checkReference
);
87 const globalScope
= context
.getScope();
89 globalScope
.variables
.forEach(checkVariable
);