Skip to content

Commit 94d2a65

Browse files
author
Robert Jackson
committed
Support custom non-lifecycle methods in options.
1 parent 523efd8 commit 94d2a65

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

ember-qunit-codemod.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ module.exports = function(file, api, options) {
145145
return POSSIBLE_MODULES.some(matcher => j.match(nodePath, matcher));
146146
}
147147

148+
function isMethod(nodePath) {
149+
return j.match(nodePath, { value: { type: 'FunctionExpression' } });
150+
}
151+
148152
const LIFE_CYCLE_METHODS = [
149153
{ key: { name: 'before' }, value: { type: 'FunctionExpression' } },
150154
{ key: { name: 'beforeEach' }, value: { type: 'FunctionExpression' } },
@@ -172,6 +176,8 @@ module.exports = function(file, api, options) {
172176
);
173177

174178
if (options) {
179+
let customMethodBeforeEachBody;
180+
175181
options.properties.forEach(property => {
176182
if (isLifecycleHook(property)) {
177183
let lifecycleStatement = j.expressionStatement(
@@ -184,6 +190,40 @@ module.exports = function(file, api, options) {
184190
lifecycleStatement.comments = property.comments;
185191

186192
callback.body.body.push(lifecycleStatement);
193+
} else if (isMethod(property)) {
194+
if (!customMethodBeforeEachBody) {
195+
customMethodBeforeEachBody = j.blockStatement([]);
196+
197+
let beforeEachInvocation = j.expressionStatement(
198+
j.callExpression(
199+
j.memberExpression(j.identifier('hooks'), j.identifier('beforeEach')),
200+
[
201+
j.functionExpression(
202+
null,
203+
[
204+
/* no arguments */
205+
],
206+
customMethodBeforeEachBody
207+
),
208+
]
209+
)
210+
);
211+
212+
callback.body.body.push(beforeEachInvocation);
213+
}
214+
215+
let methodAssignment = j.expressionStatement(
216+
j.assignmentExpression(
217+
'=',
218+
j.memberExpression(j.thisExpression(), property.key),
219+
property.value
220+
)
221+
);
222+
223+
// preserve any comments that were present
224+
methodAssignment.comments = property.comments;
225+
226+
customMethodBeforeEachBody.body.push(methodAssignment);
187227
}
188228
});
189229
}

0 commit comments

Comments
 (0)