Skip to content

Commit

Permalink
alias-model-in-controller: allow nested properties (#80)
Browse files Browse the repository at this point in the history
Fixes #79.
  • Loading branch information
buschtoens authored and michalsnik committed Jun 7, 2017
1 parent 390dc0f commit 691aa24
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
14 changes: 14 additions & 0 deletions docs/rules/alias-model-in-controller.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,17 @@ export default Ember.Route.extend({
controller.set('nail', model);
},
});
```

If you're passing
[multiple models](https://guides.emberjs.com/v2.13.0/routing/specifying-a-routes-model/#toc_multiple-models) as an
[`RSVP.hash`](https://emberjs.com/api/classes/RSVP.html#method_hash),
you can also alias nested properties:

```javascript
const { reads } = Ember.computed;
export default Ember.Controller.extend({
people: reads('model.people'),
pets: reads('model.pets')
});
```
5 changes: 4 additions & 1 deletion lib/rules/alias-model-in-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ module.exports = {
if (
parsedCallee.length &&
['alias', 'readOnly', 'reads'].indexOf(parsedCallee.pop()) > -1 &&
parsedArgs[0] === 'model'
(
parsedArgs[0] === 'model' ||
String(parsedArgs[0]).startsWith('model.')
)
) {
aliasPresent = true;
}
Expand Down
55 changes: 55 additions & 0 deletions tests/lib/rules/alias-model-in-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const RuleTester = require('eslint').RuleTester;
const eslintTester = new RuleTester();
eslintTester.run('alias-model-in-controller', rule, {
valid: [

// direct alias

{
code: 'export default Ember.Controller.extend({nail: alias("model")});',
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
Expand Down Expand Up @@ -61,6 +64,58 @@ eslintTester.run('alias-model-in-controller', rule, {
code: 'export default CustomController.extend({nail: alias("model")});',
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
},

// nested alias

{
code: 'export default Ember.Controller.extend({nail: alias("model.nail")});',
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
},
{
code: 'export default Ember.Controller.extend({nail: computed.alias("model.nail")});',
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
},
{
code: 'export default Ember.Controller.extend({nail: Ember.computed.alias("model.nail")});',
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
},
{
code: 'export default Ember.Controller.extend({nail: readOnly("model.nail")});',
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
},
{
code: 'export default Ember.Controller.extend({nail: computed.readOnly("model.nail")});',
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
},
{
code: 'export default Ember.Controller.extend({nail: Ember.computed.readOnly("model.nail")});',
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
},
{
code: 'export default Ember.Controller.extend({nail: reads("model.nail")});',
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
},
{
code: 'export default Ember.Controller.extend({nail: computed.reads("model.nail")});',
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
},
{
code: 'export default Ember.Controller.extend({nail: Ember.computed.reads("model.nail")});',
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
},
{
code: 'export default Ember.Controller.extend(TestMixin, {nail: Ember.computed.alias("model.nail")});',
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
},
{
code: 'export default Ember.Controller.extend(TestMixin, TestMixin2, {nail: Ember.computed.alias("model.nail")});',
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
},
{
filename: 'example-app/controllers/path/to/some-feature.js',
code: 'export default CustomController.extend({nail: alias("model.nail")});',
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
},
],
invalid: [
{
Expand Down

0 comments on commit 691aa24

Please sign in to comment.