Skip to content

Commit cd5c7b1

Browse files
committed
format code
1 parent 4184873 commit cd5c7b1

File tree

1 file changed

+38
-27
lines changed

1 file changed

+38
-27
lines changed

lib/rules/sort.js

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,46 +20,57 @@ function isDependencyArrayHook(node) {
2020

2121
/** @type {import('eslint').Rule.RuleModule} */
2222
module.exports = {
23-
meta: {
24-
type: 'suggestion',
25-
fixable: 'code',
26-
docs: {
27-
category: 'Stylistic Issues',
28-
description: 'Sort React dependency arrays',
29-
recommended: false,
30-
url: 'https://github.com/stevensacks/eslint-plugin-sort-react-dependency-arrays'
31-
},
32-
schema: [] // no options
33-
},
34-
create: function(context) {
23+
create(context) {
3524
return {
3625
CallExpression(node) {
3726
if (isDependencyArrayHook(node.callee)) {
3827
const dependencies = node.arguments[1];
3928

40-
if (dependencies && dependencies.type === 'ArrayExpression' && dependencies.elements.length > 1) {
29+
if (
30+
dependencies &&
31+
dependencies.type === 'ArrayExpression' &&
32+
dependencies.elements.length > 1
33+
) {
4134
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);
35+
const currentNames = currentDependencies.map(
36+
(item) => item.name
37+
);
38+
39+
const sortedDependencies = [
40+
...dependencies.elements,
41+
].sort((a, b) => (a.name < b.name ? -1 : 1));
42+
const sortedNames = sortedDependencies.map(
43+
(item) => item.name
44+
);
4545

4646
if (String(currentNames) !== String(sortedNames)) {
4747
context.report({
48-
node,
48+
fix: (fixer) =>
49+
currentDependencies.map(
50+
(dependency, index) =>
51+
fixer.replaceText(
52+
dependency,
53+
sortedNames[index]
54+
)
55+
),
4956
message: 'Sort dependencies',
50-
fix: function (fixer) {
51-
const fixes = [];
52-
currentDependencies.forEach(
53-
(dep, index) =>
54-
fixes.push(fixer.replaceText(dep, sortedNames[index]))
55-
);
56-
return fixes;
57-
}
57+
node,
5858
});
5959
}
6060
}
6161
}
62-
}
62+
},
6363
};
64-
}
64+
},
65+
meta: {
66+
docs: {
67+
category: 'Stylistic Issues',
68+
description: 'Sort React dependency arrays',
69+
recommended: false,
70+
url: 'https://github.com/stevensacks/eslint-plugin-sort-react-dependency-arrays',
71+
},
72+
fixable: 'code',
73+
schema: [],
74+
type: 'suggestion', // no options
75+
},
6576
};

0 commit comments

Comments
 (0)