-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathno-param-reassign.js
More file actions
92 lines (76 loc) · 2.18 KB
/
Copy pathno-param-reassign.js
File metadata and controls
92 lines (76 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const originalRule = require('eslint/lib/rules/no-param-reassign');
const {isTypecast} = require('../../ast-utils');
const {deepShallowCopy} = require('../../utils');
module.exports = {
meta: originalRule.meta,
create: (context) => {
const sourceCode = context.getSourceCode();
const contextCopy = deepShallowCopy(context);
/**
* @param {ASTNode} nodeA
* @param {ASTNode} nodeB
* @param {string} type
* @return {boolean}
*/
function bothHaveType(nodeA, nodeB, type) {
return nodeA.type === type && nodeB.type === type;
}
/**
* @param {ASTNode} nodeA
* @param {ASTNode} nodeB
* @return {boolean}
*/
function bothHaveSameName(nodeA, nodeB) {
return nodeA.name === nodeB.name;
}
contextCopy.report = (error) => {
const {node} = error;
let assignment;
if (node.parent.type === 'AssignmentExpression') {
assignment = node.parent;
} else if (node.parent.type === 'MemberExpression') {
let ancestor = node.parent;
while (ancestor.type === 'MemberExpression') {
ancestor = ancestor.parent;
}
if (ancestor.type === 'AssignmentExpression') {
assignment = ancestor;
}
}
if (assignment && assignment.operator === '=') {
const lhs = assignment.left;
const rhs = assignment.right;
if (isTypecast(rhs, sourceCode)) {
if (bothHaveType(lhs, rhs, 'Identifier') && bothHaveSameName(lhs, rhs)) {
return;
}
if (bothHaveType(lhs, rhs, 'MemberExpression')) {
let lhsObject = lhs;
let rhsObject = rhs;
let areIdentical = true;
while (areIdentical) {
if (bothHaveType(lhsObject, rhsObject, 'Identifier')) {
areIdentical = bothHaveSameName(lhsObject, rhsObject);
} else {
areIdentical = (
bothHaveType(lhsObject.property, rhsObject.property, 'Identifier') &&
bothHaveSameName(lhsObject.property, rhsObject.property)
);
}
lhsObject = lhsObject.object;
rhsObject = rhsObject.object;
if (!lhsObject || !rhsObject) {
break;
}
}
if (areIdentical) {
return;
}
}
}
}
context.report(error);
};
return originalRule.create(contextCopy);
}
};