-
Notifications
You must be signed in to change notification settings - Fork 109
/
outline-require.js
222 lines (200 loc) · 6 KB
/
outline-require.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
module.exports = function(file, api, options) {
if (file.path.indexOf('/__tests__/') == -1) {
return null;
}
const printOptions = options.printOptions || {quote: 'single'};
const REQUIRE_CALL = {
type: 'CallExpression',
callee: {
name: 'require',
},
};
const j = api.jscodeshift;
const root = j(file.source);
const {program} = root.get().value;
const {body} = program;
const firstComment = body[0].comments;
const isJestCall = node => (
node.type == 'CallExpression' &&
node.callee.type == 'MemberExpression' &&
node.callee.object.type == 'Identifier' &&
node.callee.object.name == 'jest'
);
const isMockModule = node => (
node && (
(
node.type == 'CallExpression' &&
node.callee.type == 'Identifier' &&
node.callee.name == 'require' &&
node.arguments.length &&
node.arguments[0].type == 'Literal' &&
['mocks', 'mock-modules'].indexOf(node.arguments[0].value) != -1
) || (
node.type == 'AssignmentExpression' &&
isMockModule(node.right)
) || (
node.type == 'VariableDeclaration' &&
node.declarations.length &&
node.declarations.some(
declaration => isMockModule(declaration.init)
)
)
)
);
const isDescribeCall = node => (
node.type == 'CallExpression' &&
node.callee.type == 'Identifier' &&
node.callee.name == 'describe'
);
const isMockLike = node => (
node.type == 'CallExpression' &&
node.callee.type == 'MemberExpression' &&
(
(
node.callee.object.type == 'Identifier' &&
(
node.callee.object.name == 'mocks' ||
node.callee.object.name == 'mockModules'
)
) ||
(
node.callee.object.type == 'CallExpression' &&
isMockLike(node.callee.object)
)
)
);
const isClassDeclaration = node => node.type == 'ClassDeclaration';
const isFunctionDeclaration = node => node.type == 'FunctionDeclaration';
const isFunctionAssignment = node => (
node.type == 'VariableDeclaration' &&
node.declarations.some(declaration => (
declaration.init &&
declaration.init.type == 'FunctionExpression'
))
);
const findInsertionPoint = body => {
var index = 0;
for (let i = 0; i < body.length; i++) {
const item = body[i];
if (
isJestCall(item) ||
isMockModule(item) ||
(item.expression && isMockLike(item.expression))
) {
index = i;
}
// Stop looking at the first describe block, a class or a function
if (
(item.expression && isDescribeCall(item.expression)) ||
isClassDeclaration(item) ||
isFunctionDeclaration(item) ||
isFunctionAssignment(item)
) {
// If we don't have an insertion point, put everything
// right before the first describe block.
if (index == 0) {
index = i - 1;
}
break;
}
}
return index + 1;
};
const createRequire = (id, requireName) =>
j.variableDeclarator(
id,
j.callExpression(
j.identifier('require'),
[j.literal(requireName)]
)
);
const isSideEffectComment = node => (
node.comments &&
node.comments.length == 1 &&
/\@side-effect/.test(node.comments[0].value)
);
const isFluxStore = (node, path) => (
/Store/.test(node.arguments[0].value)
);
root
.find(j.CallExpression, REQUIRE_CALL)
.filter(p => (
p.value.arguments.length == 1 &&
p.value.arguments[0].type == 'Literal' &&
p.parent.value.type == 'AssignmentExpression' &&
p.parent.value.left.type == 'Identifier' &&
p.parent.parent.value.type == 'ExpressionStatement' &&
p.parent.parent.parent.value != program &&
!isSideEffectComment(p.parent.parent.value) &&
!isFluxStore(p.value, p.parent.parent)
))
.forEach(p => {
const {parent} = p;
const name = parent.value.left.name;
const require = p.value.arguments[0].value;
const hasVariableDeclarator = root.find(j.VariableDeclarator, {id: {name}})
.filter(p => (
!p.value.init ||
(
p.value.init.type == 'Literal' ||
p.value.init.vlaue == 'null'
)
))
.replaceWith(p => createRequire(j.identifier(name), require))
.size();
if (hasVariableDeclarator) {
j(parent).remove();
} else {
j(parent).replaceWith(createRequire(j.identifier(name), require));
}
});
const requires = [];
root
.find(j.VariableDeclarator, {init: REQUIRE_CALL})
.filter(p => p.parent.parent && p.parent.parent.value != program)
.forEach(p => requires.unshift(
j.variableDeclaration(
'var',
[createRequire(p.value.id, p.value.init.arguments[0].value)]
)
))
.remove();
body.splice(findInsertionPoint(body), 0, ...requires.reverse());
// Cleanup empty beforeEach calls
root
.find(j.CallExpression, {
callee: {
name: 'beforeEach',
},
arguments: [{body: []}],
})
.filter(p => !p.value.arguments[0].body.body.length)
.remove();
// Cleanup duplicate requires
const requireNames = new Set();
root
.find(j.VariableDeclarator, {init: REQUIRE_CALL})
.filter(p => p.parent.parent && p.parent.parent.value == program)
.filter(p => {
const {name} = p.value.id;
const requireName = p.value.init.arguments[0].value;
const combined = name + '|' + requireName;
const has = requireNames.has(combined);
if (!has) {
requireNames.add(combined);
}
return has;
})
.remove();
// Cleanup unused requires
root
.find(j.VariableDeclarator, {init: REQUIRE_CALL})
.filter(p => !!p.value.id && p.value.id.name)
.filter(p => root.find(j.Identifier, {name: p.value.id.name}).size() == 1)
.filter(p => (
p.value.id.name != 'React' ||
root.find(j.JSXElement).size() == 0
)).remove();
body[0].comments = firstComment;
return root.toSource(printOptions);
};