Skip to content

Commit 32d13a3

Browse files
committed
replace text in nodes
1 parent b0bcf7e commit 32d13a3

File tree

4 files changed

+15
-9
lines changed

4 files changed

+15
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Then configure the rule under the rules section.
3434
```json
3535
{
3636
"rules": {
37-
"sort-react-dependency-arrays/sort-react-dependency-arrays": 2
37+
"sort-react-dependency-arrays/sort": 2
3838
}
3939
}
4040
```

lib/rules/sort-react-dependency-arrays.js renamed to lib/rules/sort.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module.exports = {
2525
fixable: 'code',
2626
docs: {
2727
category: 'Stylistic Issues',
28-
description: 'require react dependency arrays to be sorted',
28+
description: 'Sort React dependency arrays',
2929
recommended: false,
3030
url: 'https://github.com/stevensacks/eslint-plugin-sort-react-dependency-arrays'
3131
},
@@ -38,16 +38,22 @@ module.exports = {
3838
const dependencies = node.arguments[1];
3939

4040
if (dependencies && dependencies.type === 'ArrayExpression' && dependencies.elements.length > 1) {
41-
const currentNames = dependencies.elements.map(item => item.name);
42-
const sorted = [...dependencies.elements].sort((a, b) => (a.name < b.name ? -1 : 1));
43-
const sortedNames = sorted.map((dep) => dep.name);
41+
const currentDependencies = [...dependencies.elements];
42+
const sortedDependencies = [...dependencies.elements].sort((a, b) => (a.name < b.name ? -1 : 1));
43+
const currentNames = currentDependencies.map(item => item.name);
44+
const sortedNames = sortedDependencies.map(item => item.name);
4445

4546
if (String(currentNames) !== String(sortedNames)) {
4647
context.report({
4748
node,
48-
message: 'Sorting dependencies',
49+
message: 'Sort dependencies',
4950
fix: function (fixer) {
50-
return fixer.replaceText(dependencies, `[${sortedNames}]`);
51+
const fixes = [];
52+
currentDependencies.forEach(
53+
(dep, index) =>
54+
fixes.push(fixer.replaceText(dep, sortedNames[index]))
55+
);
56+
return fixes;
5157
}
5258
});
5359
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "eslint-plugin-sort-react-dependency-arrays",
33
"description": "ESLint plugin to alphabetically sort React hook dependency arrays",
4-
"version": "0.0.2",
4+
"version": "0.0.3",
55
"author": "Steven Sacks <[email protected]>",
66
"keywords": [
77
"eslint",

tests/lib/rules/sort-react-dependency-arrays.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
'use strict';
77

88
const RuleTester = require('eslint').RuleTester;
9-
const rule = require('../../../lib/rules/sort-react-dependency-arrays');
9+
const rule = require('../../../lib/rules/sort');
1010

1111
const ruleTester = new RuleTester({
1212
parserOptions: {

0 commit comments

Comments
 (0)