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 13module.exports = { 14 meta: { 15 type: "suggestion", 16 17 docs: { 18 description: "disallow assignments to native objects or read-only global variables", 19 category: "Best Practices", 20 recommended: false, 21 url: "https://eslint.org/docs/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 51 /** 52 * Reports write references. 53 * @param {Reference} reference A reference to check. 54 * @param {int} index The index of the reference in the references. 55 * @param {Reference[]} references The array that the reference belongs to. 56 * @returns {void} 57 */ 58 function checkReference(reference, index, references) { 59 const identifier = reference.identifier; 60 61 if (reference.init === false && 62 reference.isWrite() && 63 64 /* 65 * Destructuring assignments can have multiple default value, 66 * so possibly there are multiple writeable references for the same identifier. 67 */ 68 (index === 0 || references[index - 1].identifier !== identifier) 69 ) { 70 context.report({ 71 node: identifier, 72 messageId: "nativeReassign", 73 data: identifier 74 }); 75 } 76 } 77 78 /** 79 * Reports write references if a given variable is read-only builtin. 80 * @param {Variable} variable A variable to check. 81 * @returns {void} 82 */ 83 function checkVariable(variable) { 84 if (variable.writeable === false && exceptions.indexOf(variable.name) === -1) { 85 variable.references.forEach(checkReference); 86 } 87 } 88 89 return { 90 Program() { 91 const globalScope = context.getScope(); 92 93 globalScope.variables.forEach(checkVariable); 94 } 95 }; 96 } 97}; 98