-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
71 lines (68 loc) · 1.91 KB
/
index.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
const { getParser } = require('codemod-cli').jscodeshift;
const { getOptions } = require('codemod-cli');
module.exports = function transformer(file, api) {
const j = getParser(api);
const options = getOptions();
const root = j(file.source);
root
.find(j.CallExpression, {
callee: {
object: {
callee: {
object: {
type: "ThisExpression"
},
property: {
name: "$"
}
}
}
}
})
.forEach(path => {
//console.log(path);
})
.replaceWith(path => {
//callee.object.arguments
//console.log(path.value.callee.object.arguments);
const isQuerySelector = path.value.callee.object.arguments.length > 0;
//console.log(isQuerySelector);
let newCallExp;
if (isQuerySelector) {
newCallExp = j.callExpression(
j.memberExpression(
j.callExpression(
j.memberExpression(
j.memberExpression(j.thisExpression(), j.identifier("element"), false),
j.identifier("querySelectorAll")
),
path.value.callee.object.arguments
),
j.identifier("forEach"),
false
),
[
j.arrowFunctionExpression(
[j.identifier("el")],
j.callExpression(
j.memberExpression(j.identifier("el"), j.identifier("addEventListener"), false),
path.value.arguments
),
false
)
]
);
} else {
newCallExp = j.callExpression(
j.memberExpression(
j.memberExpression(j.thisExpression(), j.identifier("element"), false),
j.identifier("addEventListener"),
false
),
path.value.arguments
);
}
return newCallExp;
});
return root.toSource();
}