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