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