forked from ember-cli/babel-plugin-filter-imports
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (42 loc) · 1.31 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
module.exports = function(babel) {
return {
visitor: {
Program: function(path, state) {
// A stack of booleans that determine whether an expression statement
// should be removed as it is exited. Expression statements are removed
// when they contain a reference to a filtered imported.
state.shouldRemove = [];
},
ExpressionStatement: {
enter: function(path, state) {
state.shouldRemove.push(false);
},
exit: function(path, state) {
if (state.shouldRemove.pop()) {
path.remove();
}
}
},
Identifier: function(path, state) {
// Ensure that we're inside of an expression statement.
if (state.shouldRemove.length > 0) {
if (referencesFilteredImport(path, state.opts)) {
state.shouldRemove[state.shouldRemove.length - 1] = true;
}
}
}
}
};
};
function referencesFilteredImport(identifier, filteredImports) {
for (var moduleName in filteredImports) {
var imports = filteredImports[moduleName];
for (var i = 0; i < imports.length; i++) {
if (identifier.referencesImport(moduleName, imports[i])) {
return true;
}
}
}
return false;
}
module.exports.baseDir = function() { return __dirname; };