An ESLint plugin to sort exported function declarations alphabetically while respecting their dependencies.
You'll first need to install ESLint:
npm install eslint --save-dev
# or
yarn add -D eslint
# or
pnpm i -D eslint
npm install -D eslint-plugin-orderly-functions
# or
yarn add -D eslint-plugin-orderly-functions
# or
pnpm i -D eslint-plugin-orderly-functions
In your ESLint configuration file (.eslintrc.js
or eslint.config.js
for ESLint v9+), add the plugin and configure the rule.
// eslint.config.js for ESLint v9+ (flat config)
import orderlyFunctions from 'eslint-plugin-orderly-functions';
export default [
{
plugins: {
'orderly-functions': orderlyFunctions,
},
rules: {
'orderly-functions/sort-functions': 'error',
},
},
];
// eslint.config.js
import orderlyFunctions from 'eslint-plugin-orderly-functions';
export default [
{
plugins: {
'orderly-functions': orderlyFunctions,
},
rules: {
'orderly-functions/sort-functions': [
'error',
{
enableFixer: true,
},
],
},
},
];
Note: Enabling the autofixer will reorder your exported functions according to the rule. Use it with caution and ensure you have proper version control and testing in place.
This rule enforces that exported functions are sorted alphabetically while respecting their dependencies.
-
enableFixer
(boolean, default:false
): Enables the autofixer to automatically reorder functions.
Examples of incorrect code:
export const c = () => {
return a() + b();
};
export const a = () => 'a';
export const b = () => 'b';
Examples of correct code:
export const a = () => 'a';
export const b = () => 'b';
export const c = () => {
return a() + b();
};