Skip to content

Commit 2e9e63f

Browse files
author
Robert Jackson
committed
Migrate this.inject.service usage to this.owner.lookup.
1 parent 9838ebc commit 2e9e63f

3 files changed

Lines changed: 65 additions & 0 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
});

ember-qunit-codemod.js

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

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

@@ -298,6 +342,7 @@ module.exports = function(file, api, options) {
298342
updateModuleForToNestedModule(j, root);
299343
updateLookupCalls(j, root);
300344
updateRegisterCalls(j, root);
345+
updateInjectCalls(j, root);
301346

302347
return root.toSource(printOptions);
303348
};

0 commit comments

Comments
 (0)