Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New rule: no-duplicate-dependent-keys #104

Merged
merged 8 commits into from
Aug 7, 2017
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions docs/rules/no-duplicate-dependent-keys.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Disallow repeating dependent keys (`no-duplicate-dependent-keys`)

## Rule Details

This rule makes it easy to spot repeating dependent keys in computed properties.

Examples of **incorrect** code for this rule:

```js
computed('foo.bar', 'foo.baz', 'foo.qux', 'foo.bar', function() {
//...
})
// or using brace expansions
computed('foo.{bar,baz,qux}', 'foo.bar', function() {
//...
})
```

Examples of **correct** code for this rule:

```js
computed('foo.bar', 'foo.baz', 'foo.qux', function() {
//...
})
// or using brace expansions
computed('foo.{bar,baz,qux}', 'bar.foo', function() {
//...
})
```
30 changes: 30 additions & 0 deletions lib/rules/no-duplicate-dependent-keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

const emberUtils = require('../utils/ember');

const MESSAGE = 'Dependent keys should not be repeated';
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = {
meta: {
docs: {
description: 'Disallow repeating dependent keys',
category: 'General',
recommended: true
Copy link
Member

@michalsnik michalsnik Aug 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make it not recommended, so we can do a minor release first. I didn't catch this earlier

},
fixable: null,
message: MESSAGE,
},

create(context) {
return {
CallExpression(node) {
if (emberUtils.hasDuplicateDependentKeys(node)) {
context.report(node, MESSAGE);
}
}
};
}
};
68 changes: 68 additions & 0 deletions lib/utils/ember.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ module.exports = {

isControllerProperty,
isControllerDefaultProp,
parseDependentKeys,
unwrapBraceExpressions,
hasDuplicateDependentKeys,
};

// Private
Expand Down Expand Up @@ -364,3 +367,68 @@ function isRelation(property) {

return result;
}

/**
* Checks whether a computed property has duplicate dependent keys.
*
* @param {CallExpression} callExp Given call expression
* @return {Boolean} Flag whether dependent keys present.
*/
function hasDuplicateDependentKeys(callExp) {
if (!isComputedProp(callExp)) return false;

const dependentKeys = parseDependentKeys(callExp);
const uniqueKeys = dependentKeys
.filter((val, index, self) => self.indexOf(val) === index);

return uniqueKeys.length !== dependentKeys.length;
}

/**
* Parses dependent keys from call expression and returns them in an array.
*
* It also unwraps the expressions, so that `model.{foo,bar}` becomes `model.foo, model.bar`.
*
* @param {CallExpression} callExp CallExpression to examine
* @return {Array} Array of unwrapped dependent keys
*/
function parseDependentKeys(callExp) {
// Check whether we have a MemberExpression, eg. computed(...).volatile()
const isMemberExpCallExp = !callExp.arguments.length &&
utils.isMemberExpression(callExp.callee) &&
utils.isCallExpression(callExp.callee.object);

const args = isMemberExpCallExp ? callExp.callee.object.arguments : callExp.arguments;

const dependentKeys = args
.filter(arg => utils.isLiteral(arg))
.map(literal => literal.value);

return unwrapBraceExpressions(dependentKeys);
}

/**
* Unwraps brace expressions.
*
* @param {Array} dependentKeys array of strings containing unprocessed dependent keys.
* @return {Array} Array of unwrapped dependent keys
*/
function unwrapBraceExpressions(dependentKeys) {
const braceExpressionRegexp = /{.+}/g;

const unwrappedExpressions = dependentKeys.map((key) => {
if (!braceExpressionRegexp.test(key)) return key;

const braceExpansionPart = key.match(braceExpressionRegexp)[0];
const prefix = key.replace(braceExpansionPart, '');
const properties = braceExpansionPart
.replace('{', '')
.replace('}', '')
.split(',');

return properties.map(property => `${prefix}${property}`);
});

return unwrappedExpressions
.reduce((acc, cur) => acc.concat(cur), []);
}
167 changes: 167 additions & 0 deletions tests/lib/rules/no-duplicate-dependent-keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
'use strict';

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const rule = require('../../../lib/rules/no-duplicate-dependent-keys');
const RuleTester = require('eslint').RuleTester;


//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

const ruleTester = new RuleTester();
const parserOptions = { ecmaVersion: 6, sourceType: 'module' };
ruleTester.run('no-duplicate-dependent-keys', rule, {
valid: [
{
code: `
{
foo: computed('model.foo', 'model.bar', 'model.baz', function() {})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should test more cases, those with @each and [] too. People tend to create really crazy dependent keys and we need to make sure we support all of those weird cases, so I recommend to enlarge the test suite for this rule significantly :) WDYT?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense, will do 👍

}
`,
parserOptions,
},
{
code: `
{
foo: computed('model.{foo,bar}', 'model.qux', function() {})
}
`,
parserOptions,
},
{
code: `
{
foo: Ember.computed('model.{foo,bar}', 'model.qux', function() {
}).volatile()
}
`,
parserOptions,
},
{
code: `
{
foo: Ember.computed('model.{foo,bar}', 'model.qux', 'collection.@each.fooProp', function() {
}).volatile()
}
`,
parserOptions,
},
{
code: `
{
foo: Ember.computed('model.{foo,bar}', 'model.qux', 'collection.[]', function() {
}).volatile()
}
`,
parserOptions,
},
{
code: `
{
foo: Ember.computed('model.{foo,bar}', 'model.qux', 'collection.@each.{foo,bar}', function() {
}).volatile()
}
`,
parserOptions,
},
{
code: `
{
foo: Ember.computed('collection.@each.{foo,bar}', 'collection.@each.qux', function() {
}).volatile()
}
`,
parserOptions,
},
{
code: `
{
foo: Ember.computed('collection.@each.foo', 'collection.@each.qux', function() {
}).volatile()
}
`,
parserOptions,
},
{
code: `
{
foo: Ember.computed('collection.{foo.@each.prop, bar}', 'collection.foo.@each.qux', function() {
}).volatile()
}
`,
parserOptions,
}
],
invalid: [
{
code: `
{
foo: computed('model.foo', 'model.bar', 'model.baz', 'model.foo', function() {})
}
`,
parserOptions,
errors: [{
message: rule.meta.message,
}]
},
{
code: `
{
foo: computed('model.{foo,bar}', 'model.bar', function() {})
}
`,
parserOptions,
errors: [{
message: rule.meta.message,
}]
},
{
code: `
{
foo: computed('collection.@each.{foo,bar}', 'model.bar', 'collection.@each.bar', function() {})
}
`,
parserOptions,
errors: [{
message: rule.meta.message,
}]
},
{
code: `
{
foo: computed('collection.@each.foo', 'model.bar', 'collection.@each.foo', function() {})
}
`,
parserOptions,
errors: [{
message: rule.meta.message,
}]
},
{
code: `
{
foo: computed('collection.{foo.@each.qux,bar}', 'collection.foo.@each.qux', function() {})
}
`,
parserOptions,
errors: [{
message: rule.meta.message,
}]
},
{
code: `
{
foo: computed('a.b.c.{foo.@each.qux,bar}', 'a.b.c.baz.[]', 'a.b.c.foo.@each.qux', function() {})
}
`,
parserOptions,
errors: [{
message: rule.meta.message,
}]
}
]
});
Loading