Skip to content

Commit bb67fd8

Browse files
authored
Merge pull request #4 from cibernox/transform-container-lookup
Transform container lookup into owner lookup
2 parents 8c7d3f3 + 099bbde commit bb67fd8

3 files changed

Lines changed: 55 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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,21 @@ function updateModuleForToNestedModule(j, root) {
241241
bodyPath.replace(bodyReplacement);
242242
}
243243

244+
function updateLookupCalls(j, root) {
245+
return root
246+
.find(j.MemberExpression, {
247+
object: {
248+
object: { type: 'ThisExpression' },
249+
property: { name: 'container' },
250+
},
251+
property: { name: 'lookup' },
252+
})
253+
.forEach(path => {
254+
let thisDotOwner = j.memberExpression(j.thisExpression(), j.identifier('owner'));
255+
path.replace(j.memberExpression(thisDotOwner, path.value.property));
256+
});
257+
}
258+
244259
module.exports = function(file, api, options) {
245260
const j = api.jscodeshift;
246261

@@ -256,6 +271,7 @@ module.exports = function(file, api, options) {
256271
moveQUnitImportsFromEmberQUnit(j, root);
257272
updateToNewEmberQUnitImports(j, root);
258273
updateModuleForToNestedModule(j, root);
274+
updateLookupCalls(j, root);
259275

260276
return root.toSource(printOptions);
261277
};

0 commit comments

Comments
 (0)