Skip to content

Commit f698624

Browse files
authored
Merge pull request #29 from rwjblue/avoid-rewriting-get-owner-deeply
Ensure `getOwner(this)` is only rewritten in test context.
2 parents 062b723 + 3433df9 commit f698624

3 files changed

Lines changed: 75 additions & 14 deletions

File tree

__testfixtures__/ember-qunit-codemod/get-owner-this.input.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Service from '@ember/service';
12
import { moduleFor, test } from 'ember-qunit';
23

34
moduleFor('service:flash', 'Unit | Service | Flash', {
@@ -26,3 +27,28 @@ moduleFor('service:flash', 'Unit | Service | Flash', {
2627
test('can use Ember.getOwner(this) also', function (assert) {
2728
let owner = Ember.getOwner(this);
2829
});
30+
31+
test('objects registered can continue to use `getOwner(this)`', function(assert) {
32+
this.register('service:foo', Service.extend({
33+
someMethod() {
34+
let owner = getOwner(this);
35+
return owner.lookup('other:thing').someMethod();
36+
}
37+
}));
38+
});
39+
40+
moduleFor('service:flash', {
41+
beforeEach() {
42+
this.blah = getOwner(this).lookup('service:blah');
43+
this.register('service:foo', Service.extend({
44+
someMethod() {
45+
let owner = getOwner(this);
46+
return owner.lookup('other:thing').someMethod();
47+
}
48+
}));
49+
}
50+
});
51+
52+
test('can use getOwner(this) in beforeEach for each context', function (assert) {
53+
// stuff
54+
});

__testfixtures__/ember-qunit-codemod/get-owner-this.output.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Service from '@ember/service';
12
import { module, test } from 'qunit';
23
import { setupTest } from 'ember-qunit';
34

@@ -27,4 +28,31 @@ module('Unit | Service | Flash', function(hooks) {
2728
test('can use Ember.getOwner(this) also', function (assert) {
2829
let owner = this.owner;
2930
});
31+
32+
test('objects registered can continue to use `getOwner(this)`', function(assert) {
33+
this.owner.register('service:foo', Service.extend({
34+
someMethod() {
35+
let owner = getOwner(this);
36+
return owner.lookup('other:thing').someMethod();
37+
}
38+
}));
39+
});
40+
});
41+
42+
module('service:flash', function(hooks) {
43+
setupTest(hooks);
44+
45+
hooks.beforeEach(function() {
46+
this.blah = this.owner.lookup('service:blah');
47+
this.owner.register('service:foo', Service.extend({
48+
someMethod() {
49+
let owner = getOwner(this);
50+
return owner.lookup('other:thing').someMethod();
51+
}
52+
}));
53+
});
54+
55+
test('can use getOwner(this) in beforeEach for each context', function (assert) {
56+
// stuff
57+
});
3058
});

ember-qunit-codemod.js

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ module.exports = function(file, api, options) {
179179
let customMethodBeforeEachBody, customMethodBeforeEachExpression;
180180

181181
options.properties.forEach(property => {
182+
updateGetOwnerThisUsage(property.value);
183+
182184
if (isLifecycleHook(property)) {
183185
let lifecycleStatement = j.expressionStatement(
184186
j.callExpression(j.memberExpression(j.identifier('hooks'), property.key), [
@@ -357,10 +359,13 @@ module.exports = function(file, api, options) {
357359
currentModuleCallbackBody.push(expression);
358360

359361
let isTest = j.match(expression, { expression: { callee: { name: 'test' } } });
360-
if (isTest && currentTestType === 'setupRenderingTest') {
361-
processExpressionForRenderingTest(expression);
362-
} else if (currentTestType === 'setupTest' && !currentHasCustomSubject) {
363-
processSubject(expression, currentSubject);
362+
if (isTest) {
363+
updateGetOwnerThisUsage(expression.expression.arguments[1]);
364+
if (currentTestType === 'setupRenderingTest') {
365+
processExpressionForRenderingTest(expression);
366+
} else if (currentTestType === 'setupTest' && !currentHasCustomSubject) {
367+
processSubject(expression, currentSubject);
368+
}
364369
}
365370
} else {
366371
bodyReplacement.push(expression);
@@ -452,20 +457,25 @@ module.exports = function(file, api, options) {
452457
});
453458
}
454459

455-
function updateGetOwnerThisUsage() {
460+
function updateGetOwnerThisUsage(expression) {
461+
let expressionCollection = j(expression);
456462
let thisDotOwner = j.memberExpression(j.thisExpression(), j.identifier('owner'));
457463

458-
root
464+
function replacement(path) {
465+
if (path.scope.node === expression) {
466+
path.replace(thisDotOwner);
467+
}
468+
}
469+
470+
expressionCollection
459471
.find(j.CallExpression, {
460472
callee: {
461473
name: 'getOwner',
462474
},
463475
})
464-
.forEach(path => {
465-
path.replace(thisDotOwner);
466-
});
476+
.forEach(replacement);
467477

468-
root
478+
expressionCollection
469479
.find(j.CallExpression, {
470480
callee: {
471481
type: 'MemberExpression',
@@ -477,9 +487,7 @@ module.exports = function(file, api, options) {
477487
},
478488
},
479489
})
480-
.forEach(path => {
481-
path.replace(thisDotOwner);
482-
});
490+
.forEach(replacement);
483491
}
484492

485493
const printOptions = options.printOptions || { quote: 'single' };
@@ -496,7 +504,6 @@ module.exports = function(file, api, options) {
496504
updateLookupCalls();
497505
updateRegisterCalls();
498506
updateInjectCalls();
499-
updateGetOwnerThisUsage();
500507

501508
return root.toSource(printOptions);
502509
};

0 commit comments

Comments
 (0)