source: imaps-frontend/node_modules/eslint/lib/rules/no-global-assign.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 3.0 KB
Line 
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/** @type {import('../shared/types').Rule} */
13module.exports = {
14 meta: {
15 type: "suggestion",
16
17 docs: {
18 description: "Disallow assignments to native objects or read-only global variables",
19 recommended: true,
20 url: "https://eslint.org/docs/latest/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 messages: {
38 globalShouldNotBeModified: "Read-only global '{{name}}' should not be modified."
39 }
40 },
41
42 create(context) {
43 const config = context.options[0];
44 const sourceCode = context.sourceCode;
45 const exceptions = (config && config.exceptions) || [];
46
47 /**
48 * Reports write references.
49 * @param {Reference} reference A reference to check.
50 * @param {int} index The index of the reference in the references.
51 * @param {Reference[]} references The array that the reference belongs to.
52 * @returns {void}
53 */
54 function checkReference(reference, index, references) {
55 const identifier = reference.identifier;
56
57 if (reference.init === false &&
58 reference.isWrite() &&
59
60 /*
61 * Destructuring assignments can have multiple default value,
62 * so possibly there are multiple writeable references for the same identifier.
63 */
64 (index === 0 || references[index - 1].identifier !== identifier)
65 ) {
66 context.report({
67 node: identifier,
68 messageId: "globalShouldNotBeModified",
69 data: {
70 name: identifier.name
71 }
72 });
73 }
74 }
75
76 /**
77 * Reports write references if a given variable is read-only builtin.
78 * @param {Variable} variable A variable to check.
79 * @returns {void}
80 */
81 function checkVariable(variable) {
82 if (variable.writeable === false && !exceptions.includes(variable.name)) {
83 variable.references.forEach(checkReference);
84 }
85 }
86
87 return {
88 Program(node) {
89 const globalScope = sourceCode.getScope(node);
90
91 globalScope.variables.forEach(checkVariable);
92 }
93 };
94 }
95};
Note: See TracBrowser for help on using the repository browser.