Skip to content

Commit 79ced38

Browse files
committed
Transform container lookup into owner lookup
1 parent 3cc2831 commit 79ced38

3 files changed

Lines changed: 57 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { moduleFor, test } from 'ember-qunit';
2+
3+
moduleFor('service:foo', 'Unit | Service | Foo', {
4+
beforeEach() {
5+
let service = this.container.lookup('service:thingy');
6+
}
7+
});
8+
9+
test('it happens', function() {
10+
this.container.lookup('service:thingy').doSomething();
11+
});
12+
13+
moduleFor('service:bar', 'Unit | Service | Bar');
14+
15+
test('it happens again?', function() {
16+
this.container.lookup('service:thingy').doSomething();
17+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { module, test } from 'qunit';
2+
import { setupTest } from 'ember-qunit';
3+
4+
module('Unit | Service | Foo', function(hooks) {
5+
setupTest(hooks);
6+
7+
hooks.beforeEach(function() {
8+
let service = this.owner.lookup('service:thingy');
9+
});
10+
11+
test('it happens', function() {
12+
this.owner.lookup('service:thingy').doSomething();
13+
});
14+
});
15+
16+
module('Unit | Service | Bar', function(hooks) {
17+
setupTest(hooks);
18+
19+
test('it happens again?', function() {
20+
this.owner.lookup('service:thingy').doSomething();
21+
});
22+
});

ember-qunit-codemod.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,23 @@ function updateModuleForToNestedModule(j, root) {
222222
bodyPath.replace(bodyReplacement);
223223
}
224224

225+
function updateLookupCalls(j, root) {
226+
return root
227+
.find(j.MemberExpression)
228+
.filter(path => {
229+
return (
230+
path.value.property.name === 'lookup' &&
231+
j.MemberExpression.check(path.value.object) &&
232+
path.value.object.property.name === 'container' &&
233+
j.ThisExpression.check(path.value.object.object)
234+
);
235+
})
236+
.forEach(path => {
237+
let thisDotOwner = j.memberExpression(j.thisExpression(), j.identifier('owner'));
238+
path.replace(j.memberExpression(thisDotOwner, path.value.property));
239+
});
240+
}
241+
225242
module.exports = function(file, api, options) {
226243
const j = api.jscodeshift;
227244

@@ -237,6 +254,7 @@ module.exports = function(file, api, options) {
237254
moveQUnitImportsFromEmberQUnit(j, root);
238255
updateToNewEmberQUnitImports(j, root);
239256
updateModuleForToNestedModule(j, root);
257+
updateLookupCalls(j, root);
240258

241259
return root.toSource(printOptions);
242260
};

0 commit comments

Comments
 (0)