-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: useSelector-prefer-selectors (#54)
* feat: useSelector-prefer-selectors Add rule that enforces the use of selector function on redux `useSelector` hook. * docs: add docs to useSelector-prefer-selectors
- Loading branch information
Showing
5 changed files
with
176 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Enforces that all useSelector hooks use named selector functions. (react-redux/useSelector-prefer-selectors) | ||
|
||
Using selectors in `useSelector` to pull data from the store or [compute derived data](https://redux.js.org/recipes/computing-derived-data#composing-selectors) allows you to decouple your containers from the state architecture and more easily enable memoization. This rule will ensure that every hook utilizes a named selector. | ||
|
||
## Rule details | ||
|
||
The following pattern is considered incorrect: | ||
|
||
```js | ||
const property = useSelector((state) => state.property) | ||
const property = useSelector(function (state) { return state.property }) | ||
``` | ||
|
||
The following patterns are considered correct: | ||
|
||
```js | ||
const selector = (state) => state.property | ||
|
||
function Component() { | ||
const property = useSelector(selector) | ||
// ... | ||
} | ||
``` | ||
|
||
## Rule Options | ||
|
||
```js | ||
... | ||
"react-redux/useSelector-prefer-selectors": [<enabled>, { | ||
"matching": <string> | ||
"validateParams": <boolean> | ||
}] | ||
... | ||
``` | ||
|
||
### `matching` | ||
If provided, validates the name of the selector functions against the RegExp pattern provided. | ||
|
||
```js | ||
// .eslintrc | ||
{ | ||
"react-redux/useSelector-prefer-selectors": ["error", { matching: "^.*Selector$"}] | ||
} | ||
|
||
// container.js | ||
const propertyA = useSelector(aSelector) // success | ||
const propertyB = useSelector(selectB) // failure | ||
``` | ||
|
||
```js | ||
// .eslintrc | ||
{ | ||
"react-redux/mapStateToProps-prefer-selectors": ["error", { matching: "^get.*FromState$"}] | ||
} | ||
|
||
// container.js | ||
const propertyA = useSelector(getAFromState) // success | ||
const propertyB = useSelector(getB) // failure | ||
``` |
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,39 @@ | ||
function isUseSelector(node) { | ||
return node.callee.name === 'useSelector'; | ||
} | ||
|
||
function reportWrongName(context, node, functionName, matching) { | ||
context.report({ | ||
message: `useSelector selector "${functionName}" does not match "${matching}".`, | ||
node, | ||
}); | ||
} | ||
|
||
function reportNoSelector(context, node) { | ||
context.report({ | ||
message: 'useSelector should use a named selector function.', | ||
node, | ||
}); | ||
} | ||
|
||
module.exports = function (context) { | ||
const config = context.options[0] || {}; | ||
return { | ||
CallExpression(node) { | ||
if (!isUseSelector(node)) return; | ||
const selector = node.arguments && node.arguments[0]; | ||
if (selector && ( | ||
selector.type === 'ArrowFunctionExpression' || | ||
selector.type === 'FunctionExpression') | ||
) { | ||
reportNoSelector(context, node); | ||
} else if ( | ||
selector.type === 'Identifier' && | ||
config.matching && | ||
!selector.name.match(new RegExp(config.matching)) | ||
) { | ||
reportWrongName(context, node, selector.name, config.matching); | ||
} | ||
}, | ||
}; | ||
}; |
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,75 @@ | ||
require('babel-eslint'); | ||
|
||
const rule = require('../../../lib/rules/useSelector-prefer-selectors'); | ||
const RuleTester = require('eslint').RuleTester; | ||
|
||
const parserOptions = { | ||
ecmaVersion: 6, | ||
sourceType: 'module', | ||
ecmaFeatures: { | ||
experimentalObjectRestSpread: true, | ||
}, | ||
}; | ||
|
||
const ruleTester = new RuleTester({ parserOptions }); | ||
|
||
ruleTester.run('useSelector-prefer-selectors', rule, { | ||
valid: [ | ||
'const property = useSelector(xSelector)', | ||
{ | ||
code: 'const property = useSelector(xSelector)', | ||
options: [{ | ||
matching: '^.*Selector$', | ||
}], | ||
}, | ||
{ | ||
code: 'const property = useSelector(getX)', | ||
options: [{ | ||
matching: '^get.*$', | ||
}], | ||
}, | ||
{ | ||
code: 'const property = useSelector(selector)', | ||
options: [{ | ||
matching: '^selector$', | ||
}], | ||
}, | ||
], | ||
invalid: [{ | ||
code: 'const property = useSelector((state) => state.x)', | ||
errors: [ | ||
{ | ||
message: 'useSelector should use a named selector function.', | ||
}, | ||
], | ||
}, { | ||
code: 'const property = useSelector(function(state) { return state.x })', | ||
errors: [{ | ||
message: 'useSelector should use a named selector function.', | ||
}], | ||
}, { | ||
code: 'const property = useSelector(xSelector)', | ||
options: [{ | ||
matching: '^get.*$', | ||
}], | ||
errors: [{ | ||
message: 'useSelector selector "xSelector" does not match "^get.*$".', | ||
}], | ||
}, { | ||
code: 'const property = useSelector(getX)', | ||
options: [{ | ||
matching: '^.*Selector$', | ||
}], | ||
errors: [{ | ||
message: 'useSelector selector "getX" does not match "^.*Selector$".', | ||
}], | ||
}, { | ||
code: 'const property = useSelector(selectorr)', | ||
options: [{ | ||
matching: '^selector$', | ||
}], | ||
errors: [{ | ||
message: 'useSelector selector "selectorr" does not match "^selector$".', | ||
}], | ||
}], | ||
}); |