Skip to content

Commit d86f86f

Browse files
authored
Merge pull request #55 from rwjblue/run-wrap-😭
Wrap `.createRecord` calls in `Ember.run.`
2 parents 2f8928c + 887f021 commit d86f86f

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

‎__testfixtures__/ember-qunit-codemod/subject.output.js‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { module, test } from 'qunit';
22
import { setupTest } from 'ember-qunit';
33

4+
import { run } from '@ember/runloop';
5+
46
module('Unit | Service | Flash', function(hooks) {
57
setupTest(hooks);
68

@@ -21,27 +23,27 @@ module('Unit | Model | Foo', function(hooks) {
2123
setupTest(hooks);
2224

2325
test('has some thing', function (assert) {
24-
let subject = this.owner.lookup('service:store').createRecord('foo');
26+
let subject = run(() => this.owner.lookup('service:store').createRecord('foo'));
2527
});
2628

2729
test('has another thing', function (assert) {
28-
let subject = this.owner.lookup('service:store').createRecord('foo', { size: 'big' });
30+
let subject = run(() => this.owner.lookup('service:store').createRecord('foo', { size: 'big' }));
2931
});
3032
});
3133

3234
module('Integration | Model | Foo', function(hooks) {
3335
setupTest(hooks);
3436

3537
test('has some thing', function (assert) {
36-
let subject = this.owner.lookup('service:store').createRecord('foo');
38+
let subject = run(() => this.owner.lookup('service:store').createRecord('foo'));
3739
});
3840
});
3941

4042
module('Unit | Model | Foo', function(hooks) {
4143
setupTest(hooks);
4244

4345
test('has some thing', function (assert) {
44-
let subject = this.owner.lookup('service:store').createRecord('foo');
46+
let subject = run(() => this.owner.lookup('service:store').createRecord('foo'));
4547
});
4648
});
4749

‎ember-qunit-codemod.js‎

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = function(file, api, options) {
1+
module.exports = function(file, api) {
22
const j = api.jscodeshift;
33
const root = j(file.source);
44

@@ -28,10 +28,10 @@ module.exports = function(file, api, options) {
2828
let anchorImport = root.find(j.ImportDeclaration, { source: { value: anchor } });
2929
let imports = root.find(j.ImportDeclaration);
3030
if (anchorImport.size() > 0) {
31-
anchorImport[method](newImport);
31+
anchorImport.at(anchorImport.size() - 1)[method](newImport);
3232
} else if (imports.size() > 0) {
3333
// if anchor is not present, always add at the end
34-
imports.insertAfter(newImport);
34+
imports.at(imports.size() - 1).insertAfter(newImport);
3535
} else {
3636
// if no imports are present, add as first statement
3737
root.get().node.program.body.unshift(newImport);
@@ -415,20 +415,31 @@ module.exports = function(file, api, options) {
415415
)
416416
);
417417
} else if (subjectType === 'model') {
418+
ensureImportWithSpecifiers({
419+
source: '@ember/runloop',
420+
specifiers: ['run'],
421+
});
422+
418423
p.replace(
419-
j.callExpression(
420-
j.memberExpression(
424+
j.callExpression(j.identifier('run'), [
425+
j.arrowFunctionExpression(
426+
[],
421427
j.callExpression(
422428
j.memberExpression(
423-
j.memberExpression(j.thisExpression(), j.identifier('owner')),
424-
j.identifier('lookup')
429+
j.callExpression(
430+
j.memberExpression(
431+
j.memberExpression(j.thisExpression(), j.identifier('owner')),
432+
j.identifier('lookup')
433+
),
434+
[j.literal('service:store')]
435+
),
436+
j.identifier('createRecord')
425437
),
426-
[j.literal('service:store')]
438+
[j.literal(subjectName), options].filter(Boolean)
427439
),
428-
j.identifier('createRecord')
440+
true
429441
),
430-
[j.literal(subjectName), options].filter(Boolean)
431-
)
442+
])
432443
);
433444
} else {
434445
p.replace(
@@ -636,7 +647,7 @@ module.exports = function(file, api, options) {
636647
}
637648
}
638649

639-
const printOptions = options.printOptions || { quote: 'single' };
650+
const printOptions = { quote: 'single', wrapColumn: 100 };
640651

641652
moveQUnitImportsFromEmberQUnit();
642653
updateToNewEmberQUnitImports();

0 commit comments

Comments
 (0)