-
-
Notifications
You must be signed in to change notification settings - Fork 204
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
Changes from 4 commits
690bb11
aa4d3a8
11dbb0a
84909dc
9df52f5
dc4b57f
0a84557
4f455d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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() { | ||
//... | ||
}) | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use strict'; | ||
|
||
const utils = require('../utils/utils'); | ||
|
||
const MESSAGE = 'Dependent keys should not be repeated'; | ||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Disallow repeating dependent keys', | ||
category: 'General', | ||
recommended: true | ||
}, | ||
fixable: null, | ||
message: MESSAGE, | ||
}, | ||
|
||
create(context) { | ||
return { | ||
CallExpression(node) { | ||
if (utils.hasDuplicateDependentKeys(node)) { | ||
context.report(node, MESSAGE); | ||
} | ||
} | ||
}; | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,8 +21,13 @@ module.exports = { | |
parseCallee, | ||
parseArgs, | ||
findUnorderedProperty, | ||
parseDependentKeys, | ||
unwrapBraceExpressions, | ||
hasDuplicateDependentKeys, | ||
}; | ||
|
||
const ember = require('./ember'); | ||
|
||
/** | ||
* Find nodes of given name | ||
* | ||
|
@@ -270,3 +275,62 @@ function findUnorderedProperty(arr) { | |
|
||
return null; | ||
} | ||
|
||
/** | ||
* 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function should be in |
||
if (!ember.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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This too |
||
const isMemberExpCallExp = callExp.arguments.length === 0 && callExp.callee.object; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd add comment above why do we check this. I was confused at first and then I saw a case with You should also check it rather like so:
|
||
const args = isMemberExpCallExp ? callExp.callee.object.arguments : callExp.arguments; | ||
|
||
const dependentKeys = args | ||
.filter(arg => isLiteral(arg)) | ||
.map(literal => literal.value); | ||
return unwrapBraceExpressions(dependentKeys); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add empty line before return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good point, we should probably configure it in the project's eslint I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yup, feel free to add it :) |
||
} | ||
|
||
/** | ||
* Unwraps brace expressions. | ||
* | ||
* @param {Array} dependentKeys array of strings containing unprocessed dependent keys. | ||
* @return {Array} Array of unwrapped dependent keys | ||
*/ | ||
function unwrapBraceExpressions(dependentKeys) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And perhaps this |
||
const braceExpressionRegexp = /\.{.+}/g; | ||
|
||
const unwrappedExpressions = dependentKeys.map((key) => { | ||
if (!braceExpressionRegexp.test(key)) return key; | ||
|
||
const parts = key.split('.'); | ||
const prefix = parts[0]; | ||
const properties = parts[1] | ||
.replace('{', '') | ||
.replace('}', '') | ||
.split(','); | ||
|
||
return properties.map(property => `${prefix}.${property}`); | ||
}); | ||
|
||
return unwrappedExpressions | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if you have dependent key like:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good point! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. on the other hand, this will not work in Ember, as only one level of nesting is supported, WDYT @michalsnik? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This works actually @jbandura, only multi-level braces nesting is not allowed. See: |
||
.reduce((acc, cur) => acc.concat(cur), []); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
'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() {}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should test more cases, those with There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
} | ||
], | ||
|
||
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, | ||
}] | ||
} | ||
] | ||
}); |
There was a problem hiding this comment.
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