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