Skip to content

Commit

Permalink
feat(rules): 'use-first-last' rule to apply fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
alecxe committed Jul 9, 2016
1 parent ac912b3 commit ef246a9
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 30 deletions.
69 changes: 45 additions & 24 deletions lib/rules/use-first-last.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,59 @@
* @author Alexander Afanasyev
*/

module.exports = function (context) {
var valueFunction = {
'0': 'first()',
'-1': 'last()'
}
var valueFunction = {
'0': 'first()',
'-1': 'last()'
}

module.exports = {
meta: {
fixable: 'code'
},

function getFirstArgumentValue (methodArguments) {
var firstArgument = methodArguments[0]
create: function (context) {
function getFirstArgumentValue (methodArguments) {
var firstArgument = methodArguments[0]

if (firstArgument) {
if (firstArgument.value === 0) {
return 0
} else if (firstArgument.operator === '-') {
return -firstArgument.argument.value
if (firstArgument) {
if (firstArgument.value === 0) {
return 0
} else if (firstArgument.operator === '-') {
return -firstArgument.argument.value
}
}
}

function createFirstLastAutoFixFunction (property, argument, argumentValue) {
var functionToUse = valueFunction[argumentValue]
var rangeStart = property.range[0]
var rangeEnd = argument.range[1] + 1 // 1 added for parenthesis

return function (fixer) {
// replace get(0) with first(), get(-1) with last()
return fixer.replaceTextRange([rangeStart, rangeEnd], functionToUse)
}
}
}

return {
CallExpression: function (node) {
var property = node.callee.property
return {
CallExpression: function (node) {
var property = node.callee.property

if (property && property.name === 'get' && node.arguments) {
var argumentValue = getFirstArgumentValue(node.arguments)
if (property && property.name === 'get' && node.arguments) {
var argumentValue = getFirstArgumentValue(node.arguments)

if (argumentValue === 0 || argumentValue === -1) {
var object = node.callee.object
var callee = object.callee
if (argumentValue === 0 || argumentValue === -1) {
var object = node.callee.object
var callee = object.callee

if (callee && ((callee.property && (callee.property.name === 'all' || callee.property.name === '$$')) ||
(callee.name === '$$'))) {
context.report(node, 'Unexpected "get(' + argumentValue + ')" call, use "' + valueFunction[argumentValue] + '" instead')
if (callee && ((callee.property && (callee.property.name === 'all' || callee.property.name === '$$')) ||
(callee.name === '$$'))) {
context.report({
node: callee,
message: 'Unexpected "get(' + argumentValue + ')" call, use "' + valueFunction[argumentValue] + '" instead',
fix: createFirstLastAutoFixFunction(property, node.arguments[0], argumentValue)
})
}
}
}
}
Expand Down
30 changes: 24 additions & 6 deletions test/rules/use-first-last.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,47 +27,65 @@ eslintTester.run('use-first-last', rule, {
{
message: 'Unexpected "get(0)" call, use "first()" instead'
}
]
],
output: 'element.all(by.css(".class")).first();'
},
{
code: 'element(by.id("id")).all(by.css(".class")).get(-1);',
errors: [
{
message: 'Unexpected "get(-1)" call, use "last()" instead'
}
]
],
output: 'element(by.id("id")).all(by.css(".class")).last();'
},
{
code: '$$(".class").get(0);',
errors: [
{
message: 'Unexpected "get(0)" call, use "first()" instead'
}
]
],
output: '$$(".class").first();'
},
{
code: 'element(by.id("id")).$$(".class").get(-1);',
errors: [
{
message: 'Unexpected "get(-1)" call, use "last()" instead'
}
]
],
output: 'element(by.id("id")).$$(".class").last();'
},
{
code: 'element.all(by.css(".class")).get(0).getText();',
errors: [
{
message: 'Unexpected "get(0)" call, use "first()" instead'
}
]
],
output: 'element.all(by.css(".class")).first().getText();'
},
{
code: 'element.all(by.css(".class")).get(-1).getText();',
errors: [
{
message: 'Unexpected "get(-1)" call, use "last()" instead'
}
]
],
output: 'element.all(by.css(".class")).last().getText();'
},
{
code: 'element.all(by.css(".class")).get(-1).all(by.css(".anotherclass")).get(0).getText();',
errors: [
{
message: 'Unexpected "get(0)" call, use "first()" instead'
},
{
message: 'Unexpected "get(-1)" call, use "last()" instead'
}
],
output: 'element.all(by.css(".class")).last().all(by.css(".anotherclass")).first().getText();'
}
]
})

0 comments on commit ef246a9

Please sign in to comment.