Skip to content

Commit f05be2d

Browse files
author
Robert Jackson
committed
Handle dasherized service names.
1 parent f367b11 commit f05be2d

3 files changed

Lines changed: 16 additions & 1 deletion

File tree

__testfixtures__/ember-qunit-codemod/inject.input.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ test('it works for controllers', function(assert) {
1212
this.inject.controller('foo');
1313
this.inject.controller('foo', { as: 'bar' });
1414
});
15+
16+
test('handles dasherized names', function(assert) {
17+
this.inject.service('foo-bar');
18+
});

__testfixtures__/ember-qunit-codemod/inject.output.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@ module('Unit | Service | FooBar', function(hooks) {
1313
this.foo = this.owner.lookup('controller:foo');
1414
this.bar = this.owner.lookup('controller:foo');
1515
});
16+
17+
test('handles dasherized names', function(assert) {
18+
this['foo-bar'] = this.owner.lookup('service:foo-bar');
19+
});
1620
});

ember-qunit-codemod.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,9 +491,16 @@ module.exports = function(file, api, options) {
491491
localName = as.value.value;
492492
}
493493
}
494+
let property = j.identifier(localName);
495+
// rudimentary attempt to confirm the property name is valid
496+
// as `this.propertyName`
497+
if (!localName.match(/^[a-zA-Z_][a-zA-Z0-9]+$/)) {
498+
// if not, use `this['property-name']`
499+
property = j.literal(localName);
500+
}
494501
let assignment = j.assignmentExpression(
495502
'=',
496-
j.memberExpression(j.thisExpression(), j.identifier(localName)),
503+
j.memberExpression(j.thisExpression(), property),
497504
j.callExpression(
498505
j.memberExpression(
499506
j.memberExpression(j.thisExpression(), j.identifier('owner')),

0 commit comments

Comments
 (0)