hiukky / eslint-plugin-hooks Goto Github PK
View Code? Open in Web Editor NEW⚗️ A simple organizer for ordering hooks
License: MIT License
⚗️ A simple organizer for ordering hooks
License: MIT License
'hooks/sort': [
'error',
{
'groups': [
'useReducer',
'useState',
'useContext',
'useRef',
'useDispatch',
'useEffect',
'useCallback'
]
}
],
Oops! Something went wrong! :(
ESLint: 8.5.0
TypeError: Cannot read properties of null (reading 'callee')
Occurred while linting /Users/michael/Documents/app-frontend/src/modules/decisions/components/OpportunitiesContainer/tables/CommissionGrowthTable.tsx:1
Rule: "hooks/sort"
at /Users/michael/Documents/app-frontend/node_modules/eslint-plugin-hooks/dist/rules/sort/index.js:109:59
at Array.map (<anonymous>)
at /Users/michael/Documents/app-frontend/node_modules/eslint-plugin-hooks/dist/rules/sort/index.js:97:72
at Array.forEach (<anonymous>)
at Program (/Users/michael/Documents/app-frontend/node_modules/eslint-plugin-hooks/dist/rules/sort/index.js:79:22)
at ruleErrorHandler (/Users/michael/Documents/app-frontend/node_modules/eslint/lib/linter/linter.js:1076:28)
at /Users/michael/Documents/app-frontend/node_modules/eslint/lib/linter/safe-emitter.js:45:58
at Array.forEach (<anonymous>)
at Object.emit (/Users/michael/Documents/app-frontend/node_modules/eslint/lib/linter/safe-emitter.js:45:38)
at NodeEventGenerator.applySelector (/Users/michael/Documents/app-frontend/node_modules/eslint/lib/linter/node-event-generator.js:297:26)
error Command failed with exit code 2.
I got the last version 0.3.0, but, I have a new several problem... ESLint does not work
TypeError: Cannot read property 'declarations' of null
Occurred while linting {FILES}
at {PATH}/node_modules/eslint-plugin-hooks/dist/rules/sort/index.js:70:55
at Array.map (<anonymous>)
at Program ({PATH}/node_modules/eslint-plugin-hooks/dist/rules/sort/index.js:62:22)
Do you have a idea ?
Thanks
For my program I have made the distinction between hooks that provide state (like useState
), that consume state (like useEffect
) and ones that do both (useCallback
or useMemo
).
For the first and second group the order can be static, but for the third one I could need to call useCallback
before useMemo
or vice versa. Because they can use each other as dependencies:
const memoized = useMemo(() => (/* ... */), []);
const callback = useCallback(() => (/* ... */), [memoized]);
// vs
const callback = useCallback(() => (/* ... */), []);
const memoized = useMemo(() => (/* ... */), [callback]);
For these two hooks it may seem a little constructed, but for other (custom) hooks this will be important.
I would wish I could specify an array inside the groups
config so I can order these specific functions as I like, but not at any place in the file. The config could look like this:
'hooks/sort': [2, {
groups: [
'useState',
['useMemo', 'useCallback'],
'useEffect'
]
];
With the following config:
hooks/sort: ["warn", {
groups: ["useState", "useNetinfo"]
}]
A file given multiple components:
const Icon = () => {
// offending line because in the file this hook is used before useState
const { internetReachable } = useNetInfo();
return internetReachable ? <Image /> : <Spinner />;
}
const Container = () => {
const [loading, setLoading] = useState(true);
const { internetReachable } = useNetInfo();
useEffect(() => {
setLoading(internetReachable);
}, [internetReachable]);
}
Seems the rules are not scoped to the component itself, but rather the whole file.
There is no link from npm package page to this repo
We need support of siblings hooks, that can be in any order to each other
'hooks-sort/order-hooks': [
2,
{
groups: [
['useNavigation', 'useLanguage', 'useTranslation'],
'useRef',
['useIsKeyboardShown', 'useWindowDimensions'],
'useForm',
'useState',
['useQuery', 'useLazyQuery', 'useMutation'],
'useToggle',
['useCallback', 'useMemo'],
'useEffect',
],
},
],
Like it did in import/order
'import/order': [
'error',
{
groups: ['external', 'internal', 'builtin', ['sibling', 'parent'], 'index'],
'newlines-between': 'always',
},
],
Hello,
I would like to know if you are working on the possibility to take into account the hooks called by React.{hook}?
Because when we import React like this :
import * as React from 'react';
the useState()
doesn't sort correctly, because it is called like this :
React.useState()
I have the feeling that this is not taken into account in the sorting.
Or should it be declared in the configuration like this:
'hooks/sort': [
2,
{
groups: [
useState', ==> 'React.useState
'useRequest',
useCallback',
useEffect',
],
},
],
Thanks,
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.