-
-
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 7 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 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 | ||
}, | ||
fixable: null, | ||
message: MESSAGE, | ||
}, | ||
|
||
create(context) { | ||
return { | ||
CallExpression(node) { | ||
if (emberUtils.hasDuplicateDependentKeys(node)) { | ||
context.report(node, MESSAGE); | ||
} | ||
} | ||
}; | ||
} | ||
}; |
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() {}) | ||
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, | ||
}, | ||
{ | ||
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, | ||
}] | ||
} | ||
] | ||
}); |
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