-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add avoid-reverse rule (fixes #12)
- Loading branch information
1 parent
f104f4c
commit 15d020b
Showing
4 changed files
with
160 additions
and
2 deletions.
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
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,59 @@ | ||
/** | ||
* @author Martin Giger | ||
* @license MIT | ||
*/ | ||
"use strict"; | ||
|
||
const { | ||
isMethod, | ||
getParent | ||
} = require("../lib/helpers/call-expression"); | ||
|
||
const REPLACEMENTS = { | ||
indexOf: "lastIndexOf", | ||
reduce: "reduceRight", | ||
lastIndexOf: "indexOf", | ||
reduceRight: "reduce" | ||
}; | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: "Prefer methods operating from the right over reversing the array", | ||
recommended: true | ||
}, | ||
schema: [], | ||
fixable: "code" | ||
}, | ||
create(context) { | ||
return { | ||
"CallExpression:exit"(node) { | ||
if(Object.keys(REPLACEMENTS).every((m) => !isMethod(node, m))) { | ||
return; | ||
} | ||
|
||
const parent = getParent(node); | ||
if(!isMethod(parent, 'reverse')) { | ||
return; | ||
} | ||
|
||
const reversed = REPLACEMENTS[node.callee.property.name]; | ||
|
||
context.report({ | ||
node: node.callee.property, | ||
loc: { | ||
start: parent.callee.property.loc.start, | ||
end: node.callee.property.loc.end | ||
}, | ||
message: `Prefer using ${reversed} over reversing the array and ${node.callee.property.name}`, | ||
fix(fixer) { | ||
return fixer.replaceTextRange([ | ||
parent.callee.property.start, | ||
node.callee.property.end | ||
], reversed); | ||
} | ||
}); | ||
} | ||
}; | ||
} | ||
}; |
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,58 @@ | ||
import test from 'ava'; | ||
import AvaRuleTester from 'eslint-ava-rule-tester'; | ||
import rule from '../../rules/avoid-reverse'; | ||
|
||
const ruleTester = new AvaRuleTester(test, { | ||
parserOptions: { | ||
ecmaVersion: 2015 | ||
} | ||
}); | ||
|
||
ruleTester.run('from-map', rule, { | ||
valid: [ | ||
'array.lastIndexOf(1)', | ||
'array.indexOf(1)', | ||
'array.reduce((p, c) => p + c, 0)', | ||
'array.reduceRight((p, c) => p + c, 0)', | ||
'array.reverse()', | ||
'array.reverse().map((r) => r + 1)' | ||
], | ||
invalid: [ | ||
{ | ||
code: 'array.reverse().indexOf(1)', | ||
errors: [ { | ||
message: 'Prefer using lastIndexOf over reversing the array and indexOf', | ||
column: 7, | ||
line: 1 | ||
} ], | ||
output: 'array.lastIndexOf(1)' | ||
}, | ||
{ | ||
code: 'array.reverse().lastIndexOf(1)', | ||
errors: [ { | ||
message: 'Prefer using indexOf over reversing the array and lastIndexOf', | ||
column: 7, | ||
line: 1 | ||
} ], | ||
output: 'array.indexOf(1)' | ||
}, | ||
{ | ||
code: 'array.reverse().reduce((p, c) => p + c, 0)', | ||
errors: [ { | ||
message: 'Prefer using reduceRight over reversing the array and reduce', | ||
column: 7, | ||
line: 1 | ||
} ], | ||
output: 'array.reduceRight((p, c) => p + c, 0)' | ||
}, | ||
{ | ||
code: 'array.reverse().reduceRight((p, c) => p + c, 0)', | ||
errors: [ { | ||
message: 'Prefer using reduce over reversing the array and reduceRight', | ||
column: 7, | ||
line: 1 | ||
} ], | ||
output: 'array.reduce((p, c) => p + c, 0)' | ||
} | ||
] | ||
}); |