/
method-selector.js
65 lines (54 loc) · 1.37 KB
/
method-selector.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
'use strict';
module.exports = options => {
const {
name,
names,
length,
object,
min,
max,
property = ''
} = {
min: 0,
max: Infinity,
...options
};
const prefix = property ? `${property}.` : '';
const selector = [
`[${prefix}type="CallExpression"]`,
`[${prefix}callee.type="MemberExpression"]`,
`[${prefix}callee.computed=false]`,
`[${prefix}callee.property.type="Identifier"]`
];
if (name) {
selector.push(`[${prefix}callee.property.name="${name}"]`);
}
if (Array.isArray(names) && names.length !== 0) {
selector.push(
':matches(' +
names.map(name => `[${prefix}callee.property.name="${name}"]`).join(', ') +
')'
);
}
if (object) {
selector.push(`[${prefix}callee.object.type="Identifier"]`);
selector.push(`[${prefix}callee.object.name="${object}"]`);
}
if (typeof length === 'number') {
selector.push(`[${prefix}arguments.length=${length}]`);
}
if (min !== 0) {
selector.push(`[${prefix}arguments.length>=${min}]`);
}
if (Number.isFinite(max)) {
selector.push(`[${prefix}arguments.length<=${max}]`);
}
const maxArguments = Number.isFinite(max) ? max : length;
if (typeof maxArguments === 'number') {
// Exclude arguments with `SpreadElement` type
for (let index = 0; index < maxArguments; index += 1) {
selector.push(`[${prefix}arguments.${index}.type!="SpreadElement"]`);
}
}
return selector.join('');
};