-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathaddImport.js
139 lines (118 loc) · 3.47 KB
/
addImport.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
'use strict';
const j = require('jscodeshift');
const statements = j.template.statements;
const statement = j.template.statement;
function findViaConfigType(type, nodePath) {
return j(nodePath)
.find(type.searchTerms[0])
.filter(p => type.filters.every(filter => filter(p)));
}
function findInsertTypeIndex(config, requireStatement) {
const wrappedRequireStatement = j.program([requireStatement]);
for (let i = 0; i < config.length; i++) {
if (findViaConfigType(config[i], wrappedRequireStatement).size()) {
return i;
}
}
throw new Error('No valid config found!');
}
function findNextInsertIndex(config, root) {
for (let i = config.length - 1; i >= 0; i--) {
if (findViaConfigType(config[i], root).size()) {
return i;
}
}
return -1;
}
function reprintComment(node) {
if (node.type === 'Block') {
return j.block(node.value);
} else if (node.type === 'Line') {
return j.line(node.value);
}
return node;
}
function applyCommentsToStatements(nodes, comments) {
if (comments) {
nodes[0].comments = comments.map(comment => reprintComment(comment));
}
return nodes;
}
function reprintNode(node) {
if (j.ExpressionStatement.check(node)) {
return statement`${node.expression}`;
}
if (j.VariableDeclaration.check(node)) {
const declaration = node.declarations[0];
return j.variableDeclaration(node.kind, [
j.variableDeclarator(declaration.id, declaration.init),
]);
}
if (j.ImportDeclaration.check(node) && node.importKind === 'type') {
// TODO: Properly remove new lines from the node.
return node;
}
return node;
}
function addImport(config, root, requireStatement) {
const insertTypeIndex = findInsertTypeIndex(config, requireStatement);
const currentType = config[insertTypeIndex];
const nodesForType = findViaConfigType(currentType, root).paths();
if (nodesForType.length) {
let insertAt = nodesForType.length;
for (let i = 0; i < nodesForType.length; i++) {
const pos = currentType.comparator(
nodesForType[i].value,
requireStatement
);
if (pos >= 0) {
insertAt = i;
break;
}
}
if (insertAt === 0) {
const nodePath = nodesForType[0];
j(nodePath)
.replaceWith(applyCommentsToStatements(statements`
${requireStatement};
${reprintNode(nodePath.value)};
`, nodePath.value.comments));
} else {
const nodePath = nodesForType[insertAt - 1];
j(nodePath)
.replaceWith(applyCommentsToStatements(statements`
${reprintNode(nodePath.value)};
${requireStatement};
`, nodePath.value.comments));
}
} else {
const nextFoundConfigTypeIndex = findNextInsertIndex(
config,
root
);
if (nextFoundConfigTypeIndex > -1) {
const requiresForType = findViaConfigType(
config[nextFoundConfigTypeIndex],
root
).paths();
if (insertTypeIndex < nextFoundConfigTypeIndex) {
j(requiresForType[0])
.insertBefore(requireStatement);
} else {
j(requiresForType[requiresForType.length - 1])
.insertAfter(requireStatement);
}
} else {
const firstPath = j(root)
.find(j.Program)
.get('body')
.get(0);
j(firstPath)
.replaceWith(applyCommentsToStatements(statements`
${requireStatement};
${reprintNode(firstPath.value)};
`, firstPath.value.comments));
}
}
}
module.exports = addImport;