-
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 'user-count-method' rule
- Loading branch information
Showing
7 changed files
with
347 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,42 @@ | ||
# Recommend using `count()` instead of `then()` and `length` | ||
|
||
When you need to assert how many elements were found, it is more readable and easier when you do it using [`count()`](http://www.protractortest.org/#/api?view=ElementArrayFinder.prototype.count): | ||
|
||
```js | ||
expect(element.all(by.repeater('product in products')).count()).toBeGreaterThan(1); | ||
``` | ||
|
||
instead of resolving the `ElementArrayFinder` value and getting the `.length` property: | ||
|
||
```js | ||
element.all(by.repeater('product in products')).then(function (products) { | ||
expect(products.length >= 1).toBeTruthy(); | ||
}); | ||
``` | ||
|
||
## Rule details | ||
|
||
Any use of the following patterns are considered warnings: | ||
|
||
```js | ||
element.all(by.repeater('product in products')).then(function (products) { | ||
expect(products.length >= 1).toBeTruthy(); | ||
}); | ||
|
||
element.all(by.repeater('product in products')).then(function (products) { | ||
expect(10).toEqual(products.length); | ||
}); | ||
``` | ||
|
||
The following patterns are not warnings: | ||
|
||
```js | ||
expect(element.all(by.repeater('product in products')).count()).toBeGreaterThan(1); | ||
|
||
var products = []; | ||
console.log(products.length); | ||
|
||
element.all(by.repeater('product in products')).then(function (products) { | ||
console.log('test'); | ||
}); | ||
``` |
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,24 @@ | ||
'use strict' | ||
|
||
/** | ||
* Checks if a given node is an "expect" statement - works for both left and right parts | ||
* | ||
* @param {ASTNode} node - A node to check. | ||
* @returns {boolean} | ||
*/ | ||
module.exports = function (node) { | ||
var callee = node.callee | ||
|
||
// left part of "expect()" | ||
if (callee && callee.name === 'expect') { | ||
return true | ||
} | ||
|
||
// right part of "expect()" | ||
if (callee.object && callee.object.type === 'CallExpression' && callee.object.callee) { | ||
callee = callee.object.callee | ||
if (callee.name && callee.name === 'expect') { | ||
return true | ||
} | ||
} | ||
} |
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,24 @@ | ||
'use strict' | ||
|
||
/** | ||
* Checks if a given node is a "then" callback function | ||
* | ||
* @param {ASTNode} node - A node to check. | ||
* @returns {ASTNode} node - The first function argument node. | ||
*/ | ||
module.exports = function (node) { | ||
var property = node.callee.property | ||
var isThen = property && property.name === 'then' && node.arguments | ||
if (isThen) { | ||
var argument = node.arguments[0] | ||
// only function type allowed | ||
var isFunction = argument && (argument.type === 'FunctionExpression' || argument.type === 'ArrowFunctionExpression') | ||
if (isFunction) { | ||
// it has to have at least one argument | ||
if (argument.params && argument.params.length > 0) { | ||
return argument.params[0] | ||
} | ||
} | ||
} | ||
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,60 @@ | ||
'use strict' | ||
|
||
/** | ||
* @fileoverview Recommend using `count()` instead of `then()` and `length` | ||
* @author Alexander Afanasyev | ||
*/ | ||
|
||
var isElementArrayFinder = require('../is-element-array-finder') | ||
var isThenCallBack = require('../is-then-callback') | ||
var isExpect = require('../is-expect') | ||
|
||
module.exports = { | ||
meta: { | ||
schema: [] | ||
}, | ||
|
||
create: function (context) { | ||
return { | ||
MemberExpression: function (node) { | ||
if (node.property && node.object && node.property.name === 'length') { | ||
// remember the variable name the ".length" was used on | ||
var variableName = node.object.name | ||
|
||
// find out if we are in an expect() | ||
var expectAncestor | ||
var thenAncestor | ||
var ancestors = context.getAncestors(node) | ||
|
||
for (var i = 0; i < ancestors.length; i++) { | ||
expectAncestor = ancestors[i] | ||
if (expectAncestor && expectAncestor.type === 'CallExpression' && isExpect(expectAncestor)) { | ||
// find out if we are inside a then callback | ||
ancestors = context.getAncestors(expectAncestor) | ||
for (var j = 0; j < ancestors.length; j++) { | ||
thenAncestor = ancestors[j] | ||
if (thenAncestor && thenAncestor.type === 'CallExpression') { | ||
var thenCallbackArgument = isThenCallBack(thenAncestor) | ||
|
||
// the same variable is a "then" callback function argument | ||
if (thenCallbackArgument && thenCallbackArgument.name === variableName) { | ||
// check that it was an ElementArrayFinder resolution | ||
if (thenAncestor.callee && thenAncestor.callee.object) { | ||
if (isElementArrayFinder(thenAncestor.callee.object)) { | ||
context.report({ | ||
node: node, | ||
message: 'Array.length inside promise resolution function detected. Use count() instead.' | ||
}) | ||
return | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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,191 @@ | ||
'use strict' | ||
|
||
var rule = require('../../lib/rules/use-count-method') | ||
var RuleTester = require('eslint').RuleTester | ||
var toCode = require('../lib/join-multiline-code') | ||
|
||
var eslintTester = new RuleTester() | ||
|
||
eslintTester.run('use-count-method', rule, { | ||
valid: [ | ||
toCode([ | ||
'expect(element.all(by.repeater("product in products")).count()).toBeGreaterThan(1);' | ||
]), | ||
toCode([ | ||
'var products = [];', | ||
'console.log(products.length);' | ||
]), | ||
toCode([ | ||
'element.all(by.repeater("product in products")).then(function (products) {', | ||
' console.log("test");', | ||
'});' | ||
]), | ||
toCode([ | ||
'element.all(by.repeater("product in products")).then(function (products) {', | ||
' var testArray = [1, 2, 3];', | ||
' expect(testArray.length).toEqual(3)', | ||
'});' | ||
]), | ||
toCode([ | ||
'element.all(by.repeater("product in products")).then(function (products) {', | ||
' var testArray = [1, 2, 3];', | ||
' expect(3).toEqual(testArray.length)', | ||
'});' | ||
]), | ||
toCode([ | ||
'element.all(by.repeater("product in products")).then(function () {', | ||
'});' | ||
]), | ||
toCode([ | ||
'element.all(by.repeater("product in products")).then(function () {', | ||
' var testArray = [1, 2, 3];', | ||
' expect(3).toEqual(testArray.length)', | ||
'});' | ||
]), | ||
toCode([ | ||
'element.all(by.repeater("product in products")).filter(function (elm) {', | ||
' return true;', | ||
'});' | ||
]), | ||
toCode([ | ||
'element.all(by.repeater("product in products")).then(console.log);' | ||
]), | ||
toCode([ | ||
'somePromise.then(function (products) {', | ||
' expect(products.length).toEqual(1);', | ||
'});' | ||
]), | ||
toCode([ | ||
'then(function (products) {', | ||
' expect(products.length).toEqual(1);', | ||
'});' | ||
]), | ||
toCode([ | ||
'var products = [1, 2, 3];', | ||
'element.all(by.repeater("product in products")).then(function () {', | ||
' expect(products.length).toEqual(1);', | ||
'});' | ||
]), | ||
toCode([ | ||
'promise.method().then(function (products) {', | ||
' expect(products.length).toEqual(1);', | ||
'});' | ||
]) | ||
// TODO: promise.all().then() - recognized as an ElementArrayFinder! | ||
// TODO: run against ap-ui-html | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: toCode([ | ||
'element.all(by.repeater("product in products")).then(function (products) {', | ||
' expect(products.length >= 1).toBeTruthy();', | ||
'});' | ||
]), | ||
errors: [ | ||
{ | ||
message: 'Array.length inside promise resolution function detected. Use count() instead.' | ||
} | ||
] | ||
}, | ||
{ | ||
code: toCode([ | ||
'element.all(by.repeater("product in products")).then((products) => {', | ||
' expect(products.length >= 1).toBeTruthy();', | ||
'});' | ||
]), | ||
parserOptions: { | ||
ecmaVersion: 6 | ||
}, | ||
errors: [ | ||
{ | ||
message: 'Array.length inside promise resolution function detected. Use count() instead.' | ||
} | ||
] | ||
}, | ||
{ | ||
code: toCode([ | ||
'element.all(by.repeater("product in products")).then(function (products) {', | ||
' expect(1).toEqual(products.length);', | ||
'});' | ||
]), | ||
errors: [ | ||
{ | ||
message: 'Array.length inside promise resolution function detected. Use count() instead.' | ||
} | ||
] | ||
}, | ||
{ | ||
code: toCode([ | ||
'element.all(by.repeater("product in products")).then((products) => {', | ||
' expect(1).toEqual(products.length);', | ||
'});' | ||
]), | ||
parserOptions: { | ||
ecmaVersion: 6 | ||
}, | ||
errors: [ | ||
{ | ||
message: 'Array.length inside promise resolution function detected. Use count() instead.' | ||
} | ||
] | ||
}, | ||
{ | ||
code: toCode([ | ||
'element.all(by.repeater("product in products")).then(function (products) {', | ||
' expect(products.length).toEqual(products.length);', | ||
'});' | ||
]), | ||
errors: [ | ||
{ | ||
message: 'Array.length inside promise resolution function detected. Use count() instead.' | ||
}, | ||
{ | ||
message: 'Array.length inside promise resolution function detected. Use count() instead.' | ||
} | ||
] | ||
}, | ||
{ | ||
code: toCode([ | ||
'element.all(by.repeater("product in products")).then((products) => {', | ||
' expect(products.length).toEqual(products.length);', | ||
'});' | ||
]), | ||
parserOptions: { | ||
ecmaVersion: 6 | ||
}, | ||
errors: [ | ||
{ | ||
message: 'Array.length inside promise resolution function detected. Use count() instead.' | ||
}, | ||
{ | ||
message: 'Array.length inside promise resolution function detected. Use count() instead.' | ||
} | ||
] | ||
}, | ||
{ | ||
code: toCode([ | ||
'$$(".product").then(function (products) {', | ||
' expect(products.length >= 1).toBeTruthy();', | ||
'});' | ||
]), | ||
errors: [ | ||
{ | ||
message: 'Array.length inside promise resolution function detected. Use count() instead.' | ||
} | ||
] | ||
}, | ||
{ | ||
code: toCode([ | ||
'$$(".product").then(function (products) {', | ||
' expect(1).toEqual(products.length);', | ||
'});' | ||
]), | ||
errors: [ | ||
{ | ||
message: 'Array.length inside promise resolution function detected. Use count() instead.' | ||
} | ||
] | ||
} | ||
] | ||
}) |