Skip to content

Commit 44469f9

Browse files
author
Robert Jackson
committed
Refactor to be able to easily add statements to beforeEach callback.
1 parent b2cd635 commit 44469f9

File tree

1 file changed

+39
-30
lines changed

1 file changed

+39
-30
lines changed

ember-qunit-codemod.js

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,37 @@ module.exports = function(file, api) {
212212
return LIFE_CYCLE_METHODS.some(matcher => j.match(nodePath, matcher));
213213
}
214214

215+
function addHooksParam(moduleInfo) {
216+
moduleInfo.moduleCallback.params = [j.identifier('hooks')];
217+
}
218+
219+
function addToBeforeEach(moduleInfo, expression) {
220+
let beforeEachBody = moduleInfo.moduleBeforeEachBody;
221+
if (!beforeEachBody) {
222+
if (moduleInfo.moduleCallback) {
223+
addHooksParam(moduleInfo);
224+
}
225+
226+
let beforeEachBlockStatement = j.blockStatement([]);
227+
let beforeEachExpression = j.expressionStatement(
228+
j.callExpression(j.memberExpression(j.identifier('hooks'), j.identifier('beforeEach')), [
229+
j.functionExpression(
230+
null,
231+
[
232+
/* no arguments */
233+
],
234+
beforeEachBlockStatement
235+
),
236+
])
237+
);
238+
239+
beforeEachBody = moduleInfo.moduleBeforeEachBody = beforeEachBlockStatement.body;
240+
moduleInfo.moduleCallbackBody.push(beforeEachExpression);
241+
}
242+
243+
beforeEachBody.push(expression);
244+
}
245+
215246
function createModule(p) {
216247
let moduleInfo = new ModuleInfo(p);
217248

@@ -231,9 +262,11 @@ module.exports = function(file, api) {
231262
// Create the new `module(moduleName, function(hooks) {});` invocation
232263
let callback = j.functionExpression(
233264
null /* no function name */,
234-
[j.identifier('hooks')],
265+
[],
235266
j.blockStatement([moduleSetupExpression].filter(Boolean))
236267
);
268+
moduleInfo.moduleCallbackBody = callback.body.body;
269+
moduleInfo.moduleCallback = callback;
237270

238271
function buildModule(moduleName, callback) {
239272
// using j.callExpression() builds this:
@@ -295,27 +328,6 @@ module.exports = function(file, api) {
295328
return;
296329
}
297330

298-
if (!customMethodBeforeEachBody) {
299-
needsHooks = true;
300-
customMethodBeforeEachBody = j.blockStatement([]);
301-
customMethodBeforeEachExpression = j.expressionStatement(
302-
j.callExpression(
303-
j.memberExpression(j.identifier('hooks'), j.identifier('beforeEach')),
304-
[
305-
j.functionExpression(
306-
null,
307-
[
308-
/* no arguments */
309-
],
310-
customMethodBeforeEachBody
311-
),
312-
]
313-
)
314-
);
315-
316-
callback.body.body.push(customMethodBeforeEachExpression);
317-
}
318-
319331
let methodAssignment = j.expressionStatement(
320332
j.assignmentExpression(
321333
'=',
@@ -327,7 +339,7 @@ module.exports = function(file, api) {
327339
// preserve any comments that were present
328340
methodAssignment.comments = property.comments;
329341

330-
customMethodBeforeEachBody.body.push(methodAssignment);
342+
addToBeforeEach(moduleInfo, methodAssignment);
331343
}
332344
});
333345

@@ -338,14 +350,11 @@ module.exports = function(file, api) {
338350
}
339351
}
340352

341-
if (needsHooks === false) {
342-
// nothing used the `hooks` argument, remove it
343-
callback.params = [];
344-
}
345-
346-
// [moduleInvocation, callback.body.body, setupType, subject, hasCustomSubject];
347353
moduleInfo.moduleInvocation = moduleInvocation;
348-
moduleInfo.moduleCallbackBody = callback.body.body;
354+
355+
if (needsHooks === true) {
356+
addHooksParam(moduleInfo);
357+
}
349358

350359
return moduleInfo;
351360
}

0 commit comments

Comments
 (0)