@jipis/eslint-plugin-import-sort
v0.3.1
Published
A highly-opinionated ESLint plugin to enforce a strict custom sorting order of `import` statements across four clearly defined sections.
Downloads
20
Maintainers
Readme
@jipis/eslint-plugin-import-sort
A highly-opinionated ESLint plugin to enforce a strict custom sorting order of import statements across four clearly defined sections.
🔧 Rule: custom-import-sort
✨ Features
Groups imports into four distinct blocks:
- External packages ( from
node_modules, e.g.'react','lodash') - Internal modules (from subdirectories of your project’s
srcdirectory, excludingsrc/types) - Internal types (imports starting with
types/) - Stylesheets (
.css,.scss,.sass)
- External packages ( from
Enforces blank lines between groups
Within each group:
- Named imports (e.g.,
import { foo } from 'bar';) - Namespace imports (e.g.,
import * as Foo from 'bar';) - Default imports (e.g.,
import Foo from 'bar';)
- Named imports (e.g.,
Fixes incorrect import order automatically with
--fixSupports both semicolon and no-semicolon code styles (though fix code will always be with semicolons -- TODO)
📦 Installation
npm install --save-dev @jipis/eslint-plugin-import-sort🛠 Usage (ESLint Flat Config)
In your eslint.config.js:
import importSort from '@jipis/eslint-plugin-import-sort';
export default [
{
files: ['**/*.js', '**/*.ts'],
plugins: {
importSort,
},
rules: {
'importSort/custom-import-sort': ['error', { srcDir: 'src' }],
},
},
];🔧 Replace
"src"with the root path of your source tree if different. The plugin scans that directory for subdirectories to treat as internal paths. Ifsrcis the root of the source tree, the option can be omitted.
✅ Example
// Correct
import fs from 'fs';
import path from 'path';
import { Button } from 'components';
import utils from 'utils/helpers';
import { MyType } from 'types/models';
import './index.css';// Incorrect
import { MyType } from 'types/models';
import './index.css';
import utils from 'utils/helpers';
import path from 'path';
import fs from 'fs';
import { Button } from 'components';🧪 Behavior Summary
- All imports from external packages go first
- Internal code imports (excluding
types/) go next - Then internal
types/imports - Then stylesheets
- Within each section:
- Specifier groups (named → namespace → default)
- Strict character sort (e.g.
A < B < Z < a < b)
- Blank line between each section
- Fix output always includes semicolons
📁 Project Structure Assumptions
Your internal imports are assumed to be anything that:
- Starts with a folder name under
src/(or your configuredsrcDir) - Starts with
'./'or'../'
Types are grouped separately if they start with types/.
📝 TODO
- Add an option to enable or disable semicolons in the fixed code output
- Handle mixed imports (e.g., named and default) from a single module more robustly
- Ensure renamed imports (e.g.,
import { foo as bar }) are sorted and grouped correctly
💡 Suggestions or Issues?
Open a GitHub issue or pull request at jipis/eslint-plugin-import-sort.
📝 License
MIT
