-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
61 lines (51 loc) · 1.57 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
function stripNode(node, path, removeNode) {
if (node.type === 'CallExpression' &&
node.callee &&
node.callee.type === 'MemberExpression' &&
node.callee.object.name === 'heimdall') {
// strips anything like `heimdall.stop(token)`
// except for within switch statements
if (path.body && path.body.length) {
var index = path.body.indexOf(removeNode);
if (index !== -1) {
path.body.splice(index, 1);
}
// strips `heimdall.stop(token);` within switch statements
} else if (path.type === 'SwitchCase' && path.consequent && path.consequent.length) {
var index = path.consequent.indexOf(removeNode);
if (index !== -1) {
path.consequent.splice(index, 1);
}
} else {
// debugger;
}
// catch things like `token = heimdall.start('<id>')
} else if (node.type === 'AssignmentExpression') {
if (node.right) {
stripNode(node.right, path, removeNode);
}
}
}
function stripHeimdall(babel) {
return new babel.Plugin('strip-heimdall', {
visitor: {
ExpressionStatement: function(node, path) {
// strip stops
stripNode(node.expression, path, node);
},
VariableDeclaration: function(node, path) {
//strip `let token = heimdall.start('<id>');`
if (node.declarations && node.declarations.length === 1) {
var d = node.declarations[0];
if (d.init) {
stripNode(d.init, path, node);
}
}
}
}
});
}
stripHeimdall.baseDir = function() {
return __dirname;
};
module.exports = stripHeimdall;