This repository was archived by the owner on Nov 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (40 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
module.exports = function (babel) {
function getBindingName(path) {
const node = path.node;
if (path.type == 'VariableDeclarator') {
if (node.id.type == 'Identifier') {
return node.id.name;
}
}
if (path.type == 'ObjectProperty') {
if (node.key.type == 'Identifier') {
return getBindingName(path.parentPath) + '.' + node.key.name;
}
}
if (path.type == 'ObjectExpression') {
return getBindingName(path.parentPath);
}
return '(line ' + node.loc.start.line +
' column ' + node.loc.start.column + ')';
}
function ImportDeclaration(nodePath) {
const source = nodePath.node.source;
const file = nodePath.hub.file.opts.sourceFileName;
if (source.type == 'StringLiteral' && source.value == 'the-actions') {
const specifier = nodePath.node.specifiers.find(s => s.local.name == 'ActionCreator');
if (specifier) {
const binding = nodePath.scope.getBinding(specifier.local.name);
binding.referencePaths.forEach(ref => {
if (ref.parentPath.type == 'CallExpression') {
const name = getBindingName(ref.parentPath.parentPath);
const arg = babel.template.expression('"' + file + ': ' + name + '"')();
ref.parent.arguments.push(arg);
}
});
}
}
}
return {
visitor: { ImportDeclaration }
}
}