Skip to content

Commit 062b723

Browse files
authored
Merge pull request #27 from rwjblue/get-owner-this
Ensure `getOwner(this)` usage is rewritten.
2 parents 6f2f981 + cc42719 commit 062b723

3 files changed

Lines changed: 89 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { moduleFor, test } from 'ember-qunit';
2+
3+
moduleFor('service:flash', 'Unit | Service | Flash', {
4+
unit: true
5+
});
6+
7+
test('can fix getOwner(this) usage in a test', function (assert) {
8+
let owner = getOwner(this);
9+
});
10+
11+
moduleFor('service:flash', 'Unit | Service | Flash', {
12+
unit: true,
13+
beforeEach() {
14+
let owner = getOwner(this);
15+
}
16+
});
17+
18+
test('can use getOwner(this) in beforeEach', function (assert) {
19+
// stuff
20+
});
21+
22+
moduleFor('service:flash', 'Unit | Service | Flash', {
23+
unit: true
24+
});
25+
26+
test('can use Ember.getOwner(this) also', function (assert) {
27+
let owner = Ember.getOwner(this);
28+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { module, test } from 'qunit';
2+
import { setupTest } from 'ember-qunit';
3+
4+
module('Unit | Service | Flash', function(hooks) {
5+
setupTest(hooks);
6+
7+
test('can fix getOwner(this) usage in a test', function (assert) {
8+
let owner = this.owner;
9+
});
10+
});
11+
12+
module('Unit | Service | Flash', function(hooks) {
13+
setupTest(hooks);
14+
15+
hooks.beforeEach(function() {
16+
let owner = this.owner;
17+
});
18+
19+
test('can use getOwner(this) in beforeEach', function (assert) {
20+
// stuff
21+
});
22+
});
23+
24+
module('Unit | Service | Flash', function(hooks) {
25+
setupTest(hooks);
26+
27+
test('can use Ember.getOwner(this) also', function (assert) {
28+
let owner = this.owner;
29+
});
30+
});

ember-qunit-codemod.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,36 @@ module.exports = function(file, api, options) {
452452
});
453453
}
454454

455+
function updateGetOwnerThisUsage() {
456+
let thisDotOwner = j.memberExpression(j.thisExpression(), j.identifier('owner'));
457+
458+
root
459+
.find(j.CallExpression, {
460+
callee: {
461+
name: 'getOwner',
462+
},
463+
})
464+
.forEach(path => {
465+
path.replace(thisDotOwner);
466+
});
467+
468+
root
469+
.find(j.CallExpression, {
470+
callee: {
471+
type: 'MemberExpression',
472+
object: {
473+
name: 'Ember',
474+
},
475+
property: {
476+
name: 'getOwner',
477+
},
478+
},
479+
})
480+
.forEach(path => {
481+
path.replace(thisDotOwner);
482+
});
483+
}
484+
455485
const printOptions = options.printOptions || { quote: 'single' };
456486

457487
// Find `ember-qunit` imports
@@ -466,6 +496,7 @@ module.exports = function(file, api, options) {
466496
updateLookupCalls();
467497
updateRegisterCalls();
468498
updateInjectCalls();
499+
updateGetOwnerThisUsage();
469500

470501
return root.toSource(printOptions);
471502
};

0 commit comments

Comments
 (0)