This repository has been archived by the owner on Nov 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
74 lines (63 loc) · 1.73 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
74
'use strict';
function shouldStripNode(node) {
if (node.id &&
node.id.type === 'Identifier' &&
node.id.name === 'heimdall') {
return true;
} else if (node.type === 'CallExpression' &&
node.callee &&
node.callee.type === 'MemberExpression' &&
node.callee.object.name === 'heimdall') {
return true;
// catch things like `token = heimdall.start('<id>')
} else if (node.type === 'AssignmentExpression') {
if (node.right) {
return shouldStripNode(node.right);
}
} else if (node.type === 'VariableDeclarator') {
if (node.init) {
return shouldStripNode(node.init);
}
}
return false;
}
function stripHeimdall(babel) {
let t = babel.types;
return {
name: "strip-heimdall", // not required
visitor: {
ExpressionStatement: function(path) {
let node = path.node;
// strip stops
if (shouldStripNode(node.expression)) {
path.remove();
}
},
VariableDeclaration: function(path) {
let node = path.node;
//strip `let token = heimdall.start('<id>');`
if (node.declarations) {
if (node.declarations.length === 1) {
let d = node.declarations[0];
if (shouldStripNode(d)) {
path.remove();
}
} else {
for (let i = 0; i < node.declarations.length; i++) {
let d = node.declarations[i];
if (d.init && d.init.type === 'CallExpression') {
if (shouldStripNode(d)) {
node.declarations.splice(i, 1);
}
}
}
}
}
}
}
};
}
stripHeimdall.baseDir = function() {
return __dirname;
};
module.exports = stripHeimdall;