alecxe/eslint-plugin-protractor

View on GitHub
lib/rules/correct-chaining.js

Summary

Maintainability
A
2 hrs
Test Coverage
'use strict'
 
/**
* @fileoverview Prohibit incorrect chaining of `element` and `element.all`
* @author Alexander Afanasyev
*/
 
module.exports = {
meta: {
fixable: 'code',
schema: []
},
 
Function `create` has 32 lines of code (exceeds 25 allowed). Consider refactoring.
Function `create` has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
create: function (context) {
function isElementAllChained (node) {
var property = node.property
if (property && property.name === 'element') {
var parent = node.parent
if (parent && parent.type === 'MemberExpression') {
var parentProperty = parent.property
return parentProperty && parentProperty.name === 'all'
}
}
return false
}
 
function createCorrectChainingAutoFixFunction (node) {
var property = node.property
var startRange = property.range[0] - 1 // -1 to get the "." as well
var endRange = property.range[1]
 
return function (fixer) {
// remove .element from .element.all incorrect chain
if (property && property.name === 'element') { // self-check
return fixer.removeRange([startRange, endRange])
}
}
}
 
return {
MemberExpression: function (node) {
if (isElementAllChained(node)) {
context.report({
node: node,
message: 'Incorrect "element" and "element.all" chaining detected',
fix: createCorrectChainingAutoFixFunction(node)
})
}
}
}
}
}