-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (56 loc) · 2.07 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
const pluginName = JSON.parse(process.argv[5].replace(/index=/g,'')).pluginName;
const nameMissingIn = (properties) => {
return !properties.some(property => property.node.key.name === "name");
};
export default function({ types: t }) {
const nameKey = t.identifier("name");
const nameValue = t.stringLiteral(pluginName);
const pluginNameObject = t.objectProperty(nameKey, nameValue);
return {
name: "babel-plugin-add-name-to-plugin",
visitor: {
ExportDefaultDeclaration(path) {
// let pluginReturn;
let pluginProperties;
try {
pluginProperties = path.get([
"declaration", // FunctionDeclaration
"body", // BlockStatement
"body", // Statements[]
"0", // ReturnStatement
"argument", // ObjectExpression
"properties" // Properties[]
].join("."));
} catch(e) {
// console.log(`ExportDefaultDeclaration-${pluginName}`);
// console.log(e);
} finally {
const nameMissing = pluginProperties ? nameMissingIn(pluginProperties) : null;
if (nameMissing) {
const objProp = pluginProperties[0];
objProp ? objProp.insertBefore(pluginNameObject) : null;
}
}
},
ObjectExpression(path) {
let pluginProperties;
try {
pluginProperties = path.get('properties');
} catch(e) {
// console.log(`objexpression-${pluginName}`);
// console.log(e);
} finally {
const nameMissing = pluginProperties ? nameMissingIn(pluginProperties) : null;
if (nameMissing) {
for(let i=0; i < pluginProperties.length; i++) {
if (pluginProperties[i].node.key.name === "visitor") {
let objProp = pluginProperties[i];
objProp ? objProp.insertBefore(pluginNameObject) : null;
}
}
}
}
}
}
};
}