Skip to content

Commit 1054a7c

Browse files
authored
Merge pull request #7 from rwjblue/migrate-inject
Migrate `this.inject.service` usage to `this.owner.lookup`.
2 parents 9838ebc + 20b9e61 commit 1054a7c

3 files changed

Lines changed: 73 additions & 0 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { moduleFor, test } from 'ember-qunit';
2+
3+
moduleFor('service:foo-bar', 'Unit | Service | FooBar', {
4+
});
5+
6+
test('it exists', function(assert) {
7+
this.inject.service('foo');
8+
this.inject.service('foo', { as: 'bar' });
9+
});
10+
11+
test('it works for controllers', function(assert) {
12+
this.inject.controller('foo');
13+
this.inject.controller('foo', { as: 'bar' });
14+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { module, test } from 'qunit';
2+
import { setupTest } from 'ember-qunit';
3+
4+
module('Unit | Service | FooBar', function(hooks) {
5+
setupTest(hooks);
6+
7+
test('it exists', function(assert) {
8+
this.foo = this.owner.lookup('service:foo');
9+
this.bar = this.owner.lookup('service:foo');
10+
});
11+
12+
test('it works for controllers', function(assert) {
13+
this.foo = this.owner.lookup('controller:foo');
14+
this.bar = this.owner.lookup('controller:foo');
15+
});
16+
});

ember-qunit-codemod.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,48 @@ function updateRegisterCalls(j, root) {
281281
});
282282
}
283283

284+
function updateInjectCalls(j, root) {
285+
root
286+
.find(j.CallExpression, {
287+
callee: {
288+
type: 'MemberExpression',
289+
object: {
290+
object: {
291+
type: 'ThisExpression',
292+
},
293+
property: {
294+
name: 'inject',
295+
},
296+
},
297+
},
298+
})
299+
.forEach(p => {
300+
let injectType = p.node.callee.property.name;
301+
let injectedName = p.node.arguments[0].value;
302+
let localName = injectedName;
303+
if (p.node.arguments[1]) {
304+
let options = p.node.arguments[1];
305+
let as = options.properties.find(property => property.key.name === 'as');
306+
if (as) {
307+
localName = as.value.value;
308+
}
309+
}
310+
let assignment = j.assignmentExpression(
311+
'=',
312+
j.memberExpression(j.thisExpression(), j.identifier(localName)),
313+
j.callExpression(
314+
j.memberExpression(
315+
j.memberExpression(j.thisExpression(), j.identifier('owner')),
316+
j.identifier('lookup')
317+
),
318+
[j.literal(`${injectType}:${injectedName}`)]
319+
)
320+
);
321+
322+
p.replace(assignment);
323+
});
324+
}
325+
284326
module.exports = function(file, api, options) {
285327
const j = api.jscodeshift;
286328

@@ -298,6 +340,7 @@ module.exports = function(file, api, options) {
298340
updateModuleForToNestedModule(j, root);
299341
updateLookupCalls(j, root);
300342
updateRegisterCalls(j, root);
343+
updateInjectCalls(j, root);
301344

302345
return root.toSource(printOptions);
303346
};

0 commit comments

Comments
 (0)