-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rules): add 'use-simple-repeaters' rule
- Loading branch information
Showing
5 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Discourage using extended ng-repeat syntax in by.repeater() locators | ||
|
||
Warn about using [filters, ordering or tracking](https://docs.angularjs.org/api/ng/directive/ngRepeat) inside `by.repeater()` locators. | ||
Typically filters, ordering or tracking don't contribute to the usefulness and reliability of a locator. | ||
|
||
Imagine you have the following elements you need to locate: | ||
|
||
<div ng-repeat="customer in customers | orderBy: 'id' track by $index">...</div> | ||
|
||
Now the `| orderBy: 'id' track by $index"` part is not data-oriented and does not give any more useful or unique piece of information about the elements. | ||
|
||
This rule would recommend against having this extra part inside `by.repeater()` values. This would be bad: | ||
|
||
element.all(by.repeater("customer in customers | orderBy: 'id' track by $index")); | ||
|
||
And, this what would be better: | ||
|
||
element.all(by.repeater("customer in customers")); | ||
element.all(by.exactRepeater("customer in customers")); | ||
|
||
## Rule details | ||
|
||
The rule would warn if it detects a pipe - `|` inside a repeater or a `track by` substring. | ||
|
||
Any use of the following patterns are considered warnings: | ||
|
||
```js | ||
element.all(by.repeater("item in items | filter : x | orderBy : order | limitTo : limit as results")); | ||
element.all(by.repeater("item in items | filter:searchTerm")); | ||
element.all(by.repeater("item in items track by $index")); | ||
``` | ||
|
||
The following patterns are not warnings: | ||
|
||
```js | ||
element.all(by.repeater("item in items")); | ||
element.all(by.exactRepeater("item in items")); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use strict' | ||
|
||
/** | ||
* @fileoverview Discourage using extended ng-repeat syntax in by.repeater() locators | ||
* @author Alexander Afanasyev | ||
*/ | ||
|
||
module.exports = function (context) { | ||
return { | ||
'CallExpression': function (node) { | ||
if (node.arguments) { | ||
var object = node.callee.object | ||
var property = node.callee.property | ||
|
||
if (object && property && object.name === 'by' && property.name === 'repeater') { | ||
var repeaterValue = node.arguments[0].value | ||
|
||
if (repeaterValue.indexOf('|') > 0) { | ||
context.report(node, 'Unexpected filter inside a by.repeater() locator.') | ||
} | ||
|
||
if (repeaterValue.indexOf('track by') > 0) { | ||
context.report(node, 'Unexpected "track by" inside a by.repeater() locator.') | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use strict' | ||
|
||
var rule = require('../../lib/rules/use-simple-repeaters') | ||
var RuleTester = require('eslint').RuleTester | ||
|
||
var eslintTester = new RuleTester() | ||
|
||
eslintTester.run('use-simple-repeaters', rule, { | ||
valid: [ | ||
'element.all(by.repeater("item in items"));', | ||
'element.all(by.exactRepeater("item in items"));' | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: 'element.all(by.repeater("item in items | filter : x | orderBy : order | limitTo : limit as results"));', | ||
errors: [ | ||
{ | ||
message: 'Unexpected filter inside a by.repeater() locator.' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'element.all(by.repeater("item in items | filter:searchTerm"));', | ||
errors: [ | ||
{ | ||
message: 'Unexpected filter inside a by.repeater() locator.' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'element.all(by.repeater("item in items track by $index"));', | ||
errors: [ | ||
{ | ||
message: 'Unexpected "track by" inside a by.repeater() locator.' | ||
} | ||
] | ||
} | ||
] | ||
}) |