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