1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | const { findVariable } = require("eslint-utils");
|
13 | const astUtils = require("./utils/ast-utils");
|
14 |
|
15 | const WellKnownMutationFunctions = {
|
16 | Object: /^(?:assign|definePropert(?:y|ies)|freeze|setPrototypeOf)$/u,
|
17 | Reflect: /^(?:(?:define|delete)Property|set(?:PrototypeOf)?)$/u
|
18 | };
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | function isAssignmentLeft(node) {
|
26 | const { parent } = node;
|
27 |
|
28 | return (
|
29 | (
|
30 | parent.type === "AssignmentExpression" &&
|
31 | parent.left === node
|
32 | ) ||
|
33 |
|
34 |
|
35 | parent.type === "ArrayPattern" ||
|
36 | (
|
37 | parent.type === "Property" &&
|
38 | parent.value === node &&
|
39 | parent.parent.type === "ObjectPattern"
|
40 | ) ||
|
41 | parent.type === "RestElement" ||
|
42 | (
|
43 | parent.type === "AssignmentPattern" &&
|
44 | parent.left === node
|
45 | )
|
46 | );
|
47 | }
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | function isOperandOfMutationUnaryOperator(node) {
|
55 | const argumentNode = node.parent.type === "ChainExpression"
|
56 | ? node.parent
|
57 | : node;
|
58 | const { parent } = argumentNode;
|
59 |
|
60 | return (
|
61 | (
|
62 | parent.type === "UpdateExpression" &&
|
63 | parent.argument === argumentNode
|
64 | ) ||
|
65 | (
|
66 | parent.type === "UnaryExpression" &&
|
67 | parent.operator === "delete" &&
|
68 | parent.argument === argumentNode
|
69 | )
|
70 | );
|
71 | }
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | function isIterationVariable(node) {
|
79 | const { parent } = node;
|
80 |
|
81 | return (
|
82 | (
|
83 | parent.type === "ForInStatement" &&
|
84 | parent.left === node
|
85 | ) ||
|
86 | (
|
87 | parent.type === "ForOfStatement" &&
|
88 | parent.left === node
|
89 | )
|
90 | );
|
91 | }
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | function isArgumentOfWellKnownMutationFunction(node, scope) {
|
109 | const { parent } = node;
|
110 |
|
111 | if (parent.type !== "CallExpression" || parent.arguments[0] !== node) {
|
112 | return false;
|
113 | }
|
114 | const callee = astUtils.skipChainExpression(parent.callee);
|
115 |
|
116 | if (
|
117 | !astUtils.isSpecificMemberAccess(callee, "Object", WellKnownMutationFunctions.Object) &&
|
118 | !astUtils.isSpecificMemberAccess(callee, "Reflect", WellKnownMutationFunctions.Reflect)
|
119 | ) {
|
120 | return false;
|
121 | }
|
122 | const variable = findVariable(scope, callee.object);
|
123 |
|
124 | return variable !== null && variable.scope.type === "global";
|
125 | }
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | function isMemberWrite(id, scope) {
|
134 | const { parent } = id;
|
135 |
|
136 | return (
|
137 | (
|
138 | parent.type === "MemberExpression" &&
|
139 | parent.object === id &&
|
140 | (
|
141 | isAssignmentLeft(parent) ||
|
142 | isOperandOfMutationUnaryOperator(parent) ||
|
143 | isIterationVariable(parent)
|
144 | )
|
145 | ) ||
|
146 | isArgumentOfWellKnownMutationFunction(id, scope)
|
147 | );
|
148 | }
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 | function getWriteNode(id) {
|
156 | let node = id.parent;
|
157 |
|
158 | while (
|
159 | node &&
|
160 | node.type !== "AssignmentExpression" &&
|
161 | node.type !== "UpdateExpression" &&
|
162 | node.type !== "UnaryExpression" &&
|
163 | node.type !== "CallExpression" &&
|
164 | node.type !== "ForInStatement" &&
|
165 | node.type !== "ForOfStatement"
|
166 | ) {
|
167 | node = node.parent;
|
168 | }
|
169 |
|
170 | return node || id;
|
171 | }
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 | module.exports = {
|
178 | meta: {
|
179 | type: "problem",
|
180 |
|
181 | docs: {
|
182 | description: "disallow assigning to imported bindings",
|
183 | category: "Possible Errors",
|
184 | recommended: true,
|
185 | url: "https://eslint.org/docs/rules/no-import-assign"
|
186 | },
|
187 |
|
188 | schema: [],
|
189 |
|
190 | messages: {
|
191 | readonly: "'{{name}}' is read-only.",
|
192 | readonlyMember: "The members of '{{name}}' are read-only."
|
193 | }
|
194 | },
|
195 |
|
196 | create(context) {
|
197 | return {
|
198 | ImportDeclaration(node) {
|
199 | const scope = context.getScope();
|
200 |
|
201 | for (const variable of context.getDeclaredVariables(node)) {
|
202 | const shouldCheckMembers = variable.defs.some(
|
203 | d => d.node.type === "ImportNamespaceSpecifier"
|
204 | );
|
205 | let prevIdNode = null;
|
206 |
|
207 | for (const reference of variable.references) {
|
208 | const idNode = reference.identifier;
|
209 |
|
210 | |
211 |
|
212 |
|
213 |
|
214 |
|
215 | if (idNode === prevIdNode) {
|
216 | continue;
|
217 | }
|
218 | prevIdNode = idNode;
|
219 |
|
220 | if (reference.isWrite()) {
|
221 | context.report({
|
222 | node: getWriteNode(idNode),
|
223 | messageId: "readonly",
|
224 | data: { name: idNode.name }
|
225 | });
|
226 | } else if (shouldCheckMembers && isMemberWrite(idNode, scope)) {
|
227 | context.report({
|
228 | node: getWriteNode(idNode),
|
229 | messageId: "readonlyMember",
|
230 | data: { name: idNode.name }
|
231 | });
|
232 | }
|
233 | }
|
234 | }
|
235 | }
|
236 | };
|
237 |
|
238 | }
|
239 | };
|