-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (58 loc) · 1.68 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
72
73
const get = require('lodash.get');
const flatten = require('lodash.flattendeep');
const t = require('babel-types');
// Counts the number of arguments to console.log that are variables.
const countIdentifiersVisitor = {
Identifier: function (path, args) {
const name = path.node.name;
if (name !== 'console' && name !== 'log') {
args.state.identifierCount++;
}
}
};
const aggregateMemberNames = node => {
if (t.isThisExpression(node)) return 'this';
if (t.isIdentifier(node)) return node.name;
if (t.isMemberExpression(node)) {
const propertyName = node.property.name;
return [ aggregateMemberNames(node.object), propertyName ];
}
}
const transformer = function (babel) {
return {
visitor: {
CallExpression: (path) => {
const obj = get(path, 'node.callee.object.name');
const prop = get(path, 'node.callee.property.name')
if (obj === 'console' && prop === 'log') {
const args = path.node.arguments;
const argCount = args.length;
let state = {
identifierCount: 0
};
path.traverse(
countIdentifiersVisitor,
{ state }
);
const validArgs = args.filter(node => {
return t.isMemberExpression(node) || t.isIdentifier(node);
});
if (validArgs.length === argCount) {
const names = args.map(node => {
if (t.isIdentifier(node)) {
return node.name;
}
if (node.type === 'MemberExpression') {
const namesArray = flatten(aggregateMemberNames(node));
return namesArray.join('.');
}
});
const concated = `${names.join(', ')}: `;
path.node.arguments.unshift(t.stringLiteral(concated));
}
}
}
}
}
}
module.exports = transformer;