-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmacros.js
More file actions
90 lines (77 loc) · 2.61 KB
/
Copy pathmacros.js
File metadata and controls
90 lines (77 loc) · 2.61 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
'use strict';
const Builder = require('./builder');
const SUPPORTED_MACROS = ['assert', 'deprecate', 'warn', 'log'];
module.exports = class Macros {
constructor(babel, options) {
this.babel = babel;
this.localDebugBindings = [];
this.debugHelpers = options.externalizeHelpers || {};
this.builder = new Builder(babel.types, {
module: this.debugHelpers.module,
global: this.debugHelpers.global,
assertPredicateIndex: options.debugTools && options.debugTools.assertPredicateIndex,
isDebug: options.debugTools.isDebug,
});
}
/**
* Injects the either the env-flags module with the debug binding or
* adds the debug binding if missing from the env-flags module.
*/
expand(path) {
this.builder.expandMacros();
this._cleanImports(path);
}
/**
* Collects the import bindings for the debug tools.
*/
collectDebugToolsSpecifiers(specifiers) {
specifiers.forEach(specifier => {
if (specifier.node.imported && SUPPORTED_MACROS.indexOf(specifier.node.imported.name) > -1) {
this.localDebugBindings.push(specifier);
}
});
}
/**
* Builds the expressions that the CallExpression will expand into.
*/
build(path) {
let expression = path.node.expression;
if (
this.builder.t.isCallExpression(expression) &&
this.localDebugBindings.some(b => b.get('local').node.name === expression.callee.name)
) {
let specifier = path.scope.getBinding(expression.callee.name).path.node;
let imported = specifier.imported.name;
// The builder needs to be made aware of the the local name of the ImportSpecifier
this.builder[`${imported}`](path, {
localName: specifier.local.name,
importedName: imported,
});
}
}
_cleanImports() {
if (!this.debugHelpers.module) {
if (this.localDebugBindings.length > 0) {
let importPath = this.localDebugBindings[0].findParent(p => p.isImportDeclaration());
if (importPath === null) {
// import declaration in question seems to have already been removed
return;
}
let specifiers = importPath.get('specifiers');
if (specifiers.length === this.localDebugBindings.length) {
importPath.remove();
} else {
this.localDebugBindings.forEach(binding => {
let specifier = binding.get('local').parentPath;
let importPath = specifier.parentPath;
if (importPath.get('specifiers').length === 1) {
importPath.remove();
} else {
specifier.remove();
}
});
}
}
}
}
};