2 * @fileoverview Rule to flag updates of imported bindings.
3 * @author Toru Nagashima <https://github.com/mysticatea>
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const { findVariable, getPropertyName } = require("eslint-utils");
14 const MutationMethods = {
16 "assign", "defineProperties", "defineProperty", "freeze",
20 "defineProperty", "deleteProperty", "set", "setPrototypeOf"
25 * Check if a given node is LHS of an assignment node.
26 * @param {ASTNode} node The node to check.
27 * @returns {boolean} `true` if the node is LHS.
29 function isAssignmentLeft(node) {
30 const { parent } = node;
34 parent.type === "AssignmentExpression" &&
38 // Destructuring assignments
39 parent.type === "ArrayPattern" ||
41 parent.type === "Property" &&
42 parent.value === node &&
43 parent.parent.type === "ObjectPattern"
45 parent.type === "RestElement" ||
47 parent.type === "AssignmentPattern" &&
54 * Check if a given node is the operand of mutation unary operator.
55 * @param {ASTNode} node The node to check.
56 * @returns {boolean} `true` if the node is the operand of mutation unary operator.
58 function isOperandOfMutationUnaryOperator(node) {
59 const { parent } = node;
63 parent.type === "UpdateExpression" &&
64 parent.argument === node
67 parent.type === "UnaryExpression" &&
68 parent.operator === "delete" &&
69 parent.argument === node
75 * Check if a given node is the iteration variable of `for-in`/`for-of` syntax.
76 * @param {ASTNode} node The node to check.
77 * @returns {boolean} `true` if the node is the iteration variable.
79 function isIterationVariable(node) {
80 const { parent } = node;
84 parent.type === "ForInStatement" &&
88 parent.type === "ForOfStatement" &&
95 * Check if a given node is the iteration variable of `for-in`/`for-of` syntax.
96 * @param {ASTNode} node The node to check.
97 * @param {Scope} scope A `escope.Scope` object to find variable (whichever).
98 * @returns {boolean} `true` if the node is the iteration variable.
100 function isArgumentOfWellKnownMutationFunction(node, scope) {
101 const { parent } = node;
104 parent.type === "CallExpression" &&
105 parent.arguments[0] === node &&
106 parent.callee.type === "MemberExpression" &&
107 parent.callee.object.type === "Identifier"
109 const { callee } = parent;
110 const { object } = callee;
112 if (Object.keys(MutationMethods).includes(object.name)) {
113 const variable = findVariable(scope, object);
117 variable.scope.type === "global" &&
118 MutationMethods[object.name].has(getPropertyName(callee, scope))
127 * Check if the identifier node is placed at to update members.
128 * @param {ASTNode} id The Identifier node to check.
129 * @param {Scope} scope A `escope.Scope` object to find variable (whichever).
130 * @returns {boolean} `true` if the member of `id` was updated.
132 function isMemberWrite(id, scope) {
133 const { parent } = id;
137 parent.type === "MemberExpression" &&
138 parent.object === id &&
140 isAssignmentLeft(parent) ||
141 isOperandOfMutationUnaryOperator(parent) ||
142 isIterationVariable(parent)
145 isArgumentOfWellKnownMutationFunction(id, scope)
150 * Get the mutation node.
151 * @param {ASTNode} id The Identifier node to get.
152 * @returns {ASTNode} The mutation node.
154 function getWriteNode(id) {
155 let node = id.parent;
159 node.type !== "AssignmentExpression" &&
160 node.type !== "UpdateExpression" &&
161 node.type !== "UnaryExpression" &&
162 node.type !== "CallExpression" &&
163 node.type !== "ForInStatement" &&
164 node.type !== "ForOfStatement"
172 //------------------------------------------------------------------------------
174 //------------------------------------------------------------------------------
181 description: "disallow assigning to imported bindings",
182 category: "Possible Errors",
184 url: "https://eslint.org/docs/rules/no-import-assign"
190 readonly: "'{{name}}' is read-only.",
191 readonlyMember: "The members of '{{name}}' are read-only."
197 ImportDeclaration(node) {
198 const scope = context.getScope();
200 for (const variable of context.getDeclaredVariables(node)) {
201 const shouldCheckMembers = variable.defs.some(
202 d => d.node.type === "ImportNamespaceSpecifier"
204 let prevIdNode = null;
206 for (const reference of variable.references) {
207 const idNode = reference.identifier;
210 * AssignmentPattern (e.g. `[a = 0] = b`) makes two write
211 * references for the same identifier. This should skip
212 * the one of the two in order to prevent redundant reports.
214 if (idNode === prevIdNode) {
219 if (reference.isWrite()) {
221 node: getWriteNode(idNode),
222 messageId: "readonly",
223 data: { name: idNode.name }
225 } else if (shouldCheckMembers && isMemberWrite(idNode, scope)) {
227 node: getWriteNode(idNode),
228 messageId: "readonlyMember",
229 data: { name: idNode.name }