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