This repository has been archived by the owner on Nov 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
eslint-local-rules.js
78 lines (75 loc) · 2.29 KB
/
eslint-local-rules.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
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
module.exports = {
'no-quadratic-complexity': {
meta: {
fixable: 'code',
docs: {
description: 'ESLint local rule that will detect quadratic complexity',
recommended: false,
},
schema: [],
},
create(context) {
function process(node) {
function searchTree(root, find) {
const result = [];
const targetCount = find.length;
const pullType = () => {
const nextType = find.shift();
if (typeof nextType === 'string') return [nextType];
else return nextType;
};
let nextType = pullType();
let current = root;
while (current && nextType) {
if (nextType.indexOf(current.type) !== -1) {
result.push(current);
nextType = pullType();
}
current = current.parent;
}
return result.length === targetCount ? result : null;
}
const nodes = searchTree(node, [
['ObjectExpression', 'ArrayExpression', 'ReturnStatement'],
['FunctionExpression', 'ArrowFunctionExpression'],
'CallExpression',
]);
if (!nodes) return false;
const [return_stmt, func_stmt, call_stmt] = nodes;
if (func_stmt.params.length == 0) return false;
if (func_stmt.params[0].name !== node.argument.name) return false;
if (
call_stmt.callee.type !== 'MemberExpression' ||
call_stmt.callee.property.name !== 'reduce'
)
return false;
if (
call_stmt.arguments.length == 0 ||
call_stmt.arguments[0] !== func_stmt
)
return false;
return true;
}
return {
SpreadElement(node) {
const expand_into_stmt = node.parent;
if (
!expand_into_stmt ||
!(
expand_into_stmt.type === 'ArrayExpression' ||
expand_into_stmt.type === 'ObjectExpression'
)
)
return;
if (process(node)) {
const arg_name = node.argument.name;
context.report({
node,
message: `Quadratic reduce algorithm detected for variable "${arg_name}"`,
});
}
},
};
},
},
};