-
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 'no-array-finder-methods' rule
- Loading branch information
Showing
6 changed files
with
218 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,41 @@ | ||
# Disallow using `ElementArrayFinder` methods on `ElementFinder` | ||
|
||
This rule is inspired by [this problem](http://stackoverflow.com/questions/39710881/protractor-locator-issues) when `ElementArrayFinder` methods were unintentionally used on `ElementFinder`. | ||
|
||
This rule is especially useful if ESLint is configured in your IDE to scan on the fly - you would catch these kinds of problems early. | ||
|
||
## Rule details | ||
|
||
Here is the current list of methods the rule is looking for: | ||
|
||
'first', 'last', 'get', 'filter', 'map', 'each', 'reduce', 'count' | ||
|
||
Any use of the following patterns are considered warnings: | ||
|
||
```js | ||
element(by.css(".class")).get(0); | ||
$(".class").first(); | ||
element(by.css(".class")).last(); | ||
element(by.css(".class")).map(function (elm) {}); | ||
$(".class").filter(function (elm) {}); | ||
element(by.css(".class")).each(function (elm) {}); | ||
element(by.css(".class1")).element(by.css(".class2")).first(); | ||
element(by.css(".class1")).$(".class2").first(); | ||
$(".class1").element(by.css(".class2")).first(); | ||
$(".class1").$(".class2").first(); | ||
``` | ||
|
||
The following patterns are not warnings: | ||
|
||
```js | ||
element.all(by.css(".class")).get(0); | ||
$$(".class").first(); | ||
element.all(by.css(".class")).last(); | ||
element.all(by.css(".class")).map(function (elm) {}); | ||
$$(".class").filter(function (elm) {}); | ||
element.all(by.css(".class")).each(function (elm) {}); | ||
element(by.css(".class1")).element.all(by.css(".class2")).first(); | ||
element(by.css(".class1")).$$(".class2").first(); | ||
$(".class1").element.all(by.css(".class2")).first(); | ||
$(".class1").$$(".class2").first(); | ||
``` |
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,26 @@ | ||
'use strict' | ||
|
||
/** | ||
* Checks if a given node is an ElementFinder instance. | ||
* | ||
* @fileoverview Utility function to determine if a node is an ElementFinder | ||
* @author Alexander Afanasyev | ||
*/ | ||
module.exports = function (node) { | ||
// handling $ shortcut | ||
var callee = node.callee | ||
if (callee) { | ||
if (callee.name === '$' || callee.name === 'element') { | ||
return true | ||
} | ||
|
||
// handling $ and element chaining | ||
if (callee.type === 'MemberExpression' && callee.property) { | ||
if (callee.property.name === '$' || callee.property.name === 'element') { | ||
return true | ||
} | ||
} | ||
} | ||
|
||
return false | ||
} |
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,41 @@ | ||
'use strict' | ||
|
||
/** | ||
* @fileoverview Disallow using `ElementArrayFinder` methods on `ElementFinder` | ||
* @author Alexander Afanasyev | ||
*/ | ||
|
||
var isElementFinder = require('../is-element-finder') | ||
var elementArrayFinderMethods = [ | ||
'first', | ||
'last', | ||
'get', | ||
'filter', | ||
'map', | ||
'each', | ||
'reduce', | ||
'count' | ||
] | ||
|
||
module.exports = { | ||
meta: { | ||
schema: [] | ||
}, | ||
|
||
create: function (context) { | ||
return { | ||
MemberExpression: function (node) { | ||
var property = node.property | ||
|
||
if (property && elementArrayFinderMethods.indexOf(property.name) > -1) { | ||
if (isElementFinder(node.object)) { | ||
context.report({ | ||
node: node, | ||
message: 'Unexpected "' + property.name + '()" call on ElementFinder' | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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,104 @@ | ||
'use strict' | ||
|
||
var rule = require('../../lib/rules/no-array-finder-methods') | ||
var RuleTester = require('eslint').RuleTester | ||
|
||
var eslintTester = new RuleTester() | ||
|
||
eslintTester.run('no-array-finder-methods', rule, { | ||
valid: [ | ||
'element.all(by.css(".class")).get(0);', | ||
'$$(".class").first();', | ||
'element.all(by.css(".class")).last();', | ||
'element.all(by.css(".class")).map(function (elm) {});', | ||
'$$(".class").filter(function (elm) {});', | ||
'element.all(by.css(".class")).each(function (elm) {});', | ||
'element(by.css(".class1")).element.all(by.css(".class2")).first();', | ||
'element(by.css(".class1")).$$(".class2").first();', | ||
'$(".class1").element.all(by.css(".class2")).first();', | ||
'$(".class1").$$(".class2").first();' | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: 'element(by.css(".class")).get(0);', | ||
errors: [ | ||
{ | ||
message: 'Unexpected "get()" call on ElementFinder' | ||
} | ||
] | ||
}, | ||
{ | ||
code: '$(".class").first();', | ||
errors: [ | ||
{ | ||
message: 'Unexpected "first()" call on ElementFinder' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'element(by.css(".class")).last();', | ||
errors: [ | ||
{ | ||
message: 'Unexpected "last()" call on ElementFinder' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'element(by.css(".class")).map(function (elm) {});', | ||
errors: [ | ||
{ | ||
message: 'Unexpected "map()" call on ElementFinder' | ||
} | ||
] | ||
}, | ||
{ | ||
code: '$(".class").filter(function (elm) {});', | ||
errors: [ | ||
{ | ||
message: 'Unexpected "filter()" call on ElementFinder' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'element(by.css(".class")).each(function (elm) {});', | ||
errors: [ | ||
{ | ||
message: 'Unexpected "each()" call on ElementFinder' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'element(by.css(".class1")).element(by.css(".class2")).first();', | ||
errors: [ | ||
{ | ||
message: 'Unexpected "first()" call on ElementFinder' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'element(by.css(".class1")).$(".class2").first();', | ||
errors: [ | ||
{ | ||
message: 'Unexpected "first()" call on ElementFinder' | ||
} | ||
] | ||
}, | ||
{ | ||
code: '$(".class1").element(by.css(".class2")).first();', | ||
errors: [ | ||
{ | ||
message: 'Unexpected "first()" call on ElementFinder' | ||
} | ||
] | ||
}, | ||
{ | ||
code: '$(".class1").$(".class2").first();', | ||
errors: [ | ||
{ | ||
message: 'Unexpected "first()" call on ElementFinder' | ||
} | ||
] | ||
} | ||
] | ||
}) |