Skip to content

Commit 52a34d4

Browse files
committed
initial commit
1 parent b761471 commit 52a34d4

12 files changed

+3613
-1
lines changed

.editorconfig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# editorconfig.org
2+
3+
root = true
4+
5+
# We recommend to keep these unchanged
6+
[*]
7+
charset = utf-8
8+
end_of_line = lf
9+
indent_size = 4
10+
indent_style = space
11+
insert_final_newline = true
12+
trim_trailing_whitespace = true
13+
14+
[*.md]
15+
trim_trailing_whitespace = false
16+
17+
[*.yml]
18+
indent_size = 2

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
commitlint.config.js
2+
.eslintrc.js

.eslintrc.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
module.exports = {
2+
root: true,
3+
"env": {
4+
"es2021": true,
5+
"node": true
6+
},
7+
extends: [
8+
"eslint:recommended",
9+
"plugin:eslint-plugin/recommended",
10+
"plugin:node/recommended",
11+
],
12+
"parserOptions": {
13+
"ecmaVersion": "latest",
14+
"sourceType": "module"
15+
},
16+
"rules": {
17+
"indent": [
18+
"error",
19+
4
20+
],
21+
"linebreak-style": [
22+
"error",
23+
"unix"
24+
],
25+
"quotes": [
26+
"error",
27+
"single"
28+
],
29+
"semi": [
30+
"error",
31+
"always"
32+
]
33+
},
34+
overrides: [
35+
{
36+
files: ["tests/**/*.js"],
37+
env: { mocha: true }
38+
}
39+
]
40+
}

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.DS_Store
2+
.thumbs.db
3+
node_modules
4+
npm-debug.log*
5+
6+
# Editor directories and files
7+
.vs
8+
.idea
9+
*.suo
10+
*.ntvs*
11+
*.njsproj
12+
*.sln

.prettierignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package-lock.json
2+
.editorconfig
3+
.gitignore
4+
.stylelintignore
5+
.eslintignore
6+
.prettierignore

.prettierrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"bracketSpacing": false,
3+
"singleQuote": true,
4+
"tabWidth": 4
5+
}

README.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,40 @@
11
# eslint-plugin-sort-react-dependency-arrays
2-
ESLint plugin to alphabetically sort React hook dependency arrays
2+
ESLint plugin to enforce alphabetically sorted React hook dependency arrays.
3+
4+
Works with --fix.
5+
6+
## Installation
7+
8+
You'll first need to install [ESLint](https://eslint.org/):
9+
10+
```sh
11+
npm i eslint --save-dev
12+
```
13+
14+
Next, install `eslint-plugin-sort-react-dependency-arrays`:
15+
16+
```sh
17+
npm install eslint-plugin-sort-react-dependency-arrays --save-dev
18+
```
19+
20+
## Usage
21+
22+
Add `sort-react-dependency-arrays` to the plugins section of your `.eslintrc` configuration file. You can omit the `eslint-plugin-` prefix:
23+
24+
```json
25+
{
26+
"plugins": [
27+
"sort-react-dependency-arrays"
28+
]
29+
}
30+
```
31+
32+
Then configure the rule under the rules section.
33+
34+
```json
35+
{
36+
"rules": {
37+
"sort-react-dependency-arrays/sort-react-dependency-arrays": 2
38+
}
39+
}
40+
```

lib/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @fileoverview Sort React dependency arrays with autofix
3+
* @author Steven Sacks
4+
*/
5+
6+
'use strict';
7+
8+
//------------------------------------------------------------------------------
9+
// Requirements
10+
//------------------------------------------------------------------------------
11+
12+
const requireIndex = require('requireindex');
13+
14+
//------------------------------------------------------------------------------
15+
// Plugin Definition
16+
//------------------------------------------------------------------------------
17+
18+
19+
// import all rules in lib/rules
20+
module.exports.rules = requireIndex(__dirname + '/rules');
21+
22+
23+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* This source code is licensed under the MIT license found in the
3+
* LICENSE file in the root directory of this source tree.
4+
*/
5+
6+
'use strict';
7+
8+
const HOOKS = ['useCallback', 'useEffect', 'useLayoutEffect', 'useMemo'];
9+
10+
function isDependencyArrayHook(node) {
11+
if (node.type === 'Identifier') {
12+
return HOOKS.includes(node.name);
13+
}
14+
return false;
15+
}
16+
17+
//------------------------------------------------------------------------------
18+
// Rule Definition
19+
//------------------------------------------------------------------------------
20+
21+
/** @type {import('eslint').Rule.RuleModule} */
22+
module.exports = {
23+
meta: {
24+
type: 'suggestion',
25+
fixable: 'code',
26+
docs: {
27+
category: 'Stylistic Issues',
28+
description: 'require react dependency arrays to be sorted',
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) {
35+
return {
36+
CallExpression(node) {
37+
if (isDependencyArrayHook(node.callee)) {
38+
const dependencies = node.arguments[1];
39+
40+
if (dependencies && dependencies.type === 'ArrayExpression' && dependencies.elements.length > 1) {
41+
const sorted = [...dependencies.elements].sort((a, b) => (a.name < b.name ? -1 : 1));
42+
const sortedNames = sorted.map((dep) => dep.name);
43+
44+
context.report({
45+
node,
46+
message: 'Sorting dependencies',
47+
fix: function (fixer) {
48+
return fixer.replaceText(dependencies, `[${sortedNames}]`);
49+
}
50+
});
51+
}
52+
}
53+
}
54+
};
55+
}
56+
};

0 commit comments

Comments
 (0)