-
Notifications
You must be signed in to change notification settings - Fork 73
/
no-polymorphic-call.js
53 lines (51 loc) · 1.37 KB
/
no-polymorphic-call.js
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
'use strict';
module.exports = {
meta: {
docs: {
description: "disallow polymorphic function calls e.g.: 'array.slice()'",
category: 'Possible Security Errors',
recommended: true,
url: 'https://github.com/endojs/endo/blob/master/packages/eslint-plugin/lib/rules/no-polymorphic-call.js',
},
type: 'problem',
fixable: null,
schema: [],
supported: true,
},
create(context) {
return {
CallExpression(node) {
if (node.callee.type !== 'MemberExpression') {
return;
}
const reportHint = prepareMemberExpressionHint(node.callee);
context.report(
node,
`Polymorphic call: "${reportHint}". May be vulnerable to corruption or trap`,
);
},
};
},
};
function prepareMemberExpressionHint(node) {
const { object, property, computed } = node;
let objectHint;
let propertyHint;
if (object.type === 'Identifier') {
objectHint = object.name;
} else if (object.type === 'MemberExpression') {
objectHint = prepareMemberExpressionHint(object);
} else {
objectHint = `[[${object.type}]]`;
}
if (property.type === 'Identifier') {
if (computed) {
propertyHint = `[${property.name}]`;
} else {
propertyHint = property.name;
}
} else {
propertyHint = `[[${property.type}]]`;
}
return `${objectHint}.${propertyHint}`;
}