eslint-plugin-sort-react-dependency-arrays
Advanced tools
+38
-27
@@ -23,14 +23,3 @@ /** | ||
| module.exports = { | ||
| meta: { | ||
| type: 'suggestion', | ||
| fixable: 'code', | ||
| docs: { | ||
| category: 'Stylistic Issues', | ||
| description: 'Sort React dependency arrays', | ||
| recommended: false, | ||
| url: 'https://github.com/stevensacks/eslint-plugin-sort-react-dependency-arrays' | ||
| }, | ||
| schema: [] // no options | ||
| }, | ||
| create: function(context) { | ||
| create(context) { | ||
| return { | ||
@@ -41,20 +30,31 @@ CallExpression(node) { | ||
| if (dependencies && dependencies.type === 'ArrayExpression' && dependencies.elements.length > 1) { | ||
| if ( | ||
| dependencies && | ||
| dependencies.type === 'ArrayExpression' && | ||
| dependencies.elements.length > 1 | ||
| ) { | ||
| const currentDependencies = [...dependencies.elements]; | ||
| const sortedDependencies = [...dependencies.elements].sort((a, b) => (a.name < b.name ? -1 : 1)); | ||
| const currentNames = currentDependencies.map(item => item.name); | ||
| const sortedNames = sortedDependencies.map(item => item.name); | ||
| const currentNames = currentDependencies.map( | ||
| (item) => item.name | ||
| ); | ||
| const sortedDependencies = [ | ||
| ...dependencies.elements, | ||
| ].sort((a, b) => (a.name < b.name ? -1 : 1)); | ||
| const sortedNames = sortedDependencies.map( | ||
| (item) => item.name | ||
| ); | ||
| if (String(currentNames) !== String(sortedNames)) { | ||
| context.report({ | ||
| fix: (fixer) => | ||
| currentDependencies.map( | ||
| (dependency, index) => | ||
| fixer.replaceText( | ||
| dependency, | ||
| sortedNames[index] | ||
| ) | ||
| ), | ||
| message: 'Sort dependencies', | ||
| node, | ||
| message: 'Sort dependencies', | ||
| fix: function (fixer) { | ||
| const fixes = []; | ||
| currentDependencies.forEach( | ||
| (dep, index) => | ||
| fixes.push(fixer.replaceText(dep, sortedNames[index])) | ||
| ); | ||
| return fixes; | ||
| } | ||
| }); | ||
@@ -64,5 +64,16 @@ } | ||
| } | ||
| } | ||
| }, | ||
| }; | ||
| } | ||
| }, | ||
| meta: { | ||
| docs: { | ||
| category: 'Stylistic Issues', | ||
| description: 'Sort React dependency arrays', | ||
| recommended: false, | ||
| url: 'https://github.com/stevensacks/eslint-plugin-sort-react-dependency-arrays', | ||
| }, | ||
| fixable: 'code', | ||
| schema: [], | ||
| type: 'suggestion', // no options | ||
| }, | ||
| }; |
+1
-1
| { | ||
| "name": "eslint-plugin-sort-react-dependency-arrays", | ||
| "description": "ESLint plugin to alphabetically sort React hook dependency arrays", | ||
| "version": "0.0.3", | ||
| "version": "0.1.0", | ||
| "author": "Steven Sacks <stevensacks@gmail.com>", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
+1
-1
@@ -37,5 +37,5 @@ # eslint-plugin-sort-react-dependency-arrays | ||
| "rules": { | ||
| "sort-react-dependency-arrays/sort": 2 | ||
| "sort-react-dependency-arrays/sort": "error" | ||
| } | ||
| } | ||
| ``` |
7865
3.36%139
7.75%