package
An ESLint plugin to sort both import and require declarations in a unified manner.
npm install eslint-plugin-sort-imports-requiresGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Tagged topics.
An ESLint plugin to sort both import and require declarations in a unified manner.
ESLint's sort-imports only works for import statements. However, require statements are still being widely used. We needed to validate import and require statements in a similar way throughout our codebase and we couldn't find an OSS package that would address this need with the features we require.
This plugin is a drop-in replacement to sort-imports with a few extra features:
unsafeAutofix).useAliases).multiple type corresponds to all named imports, regardless of how many are imported (see useOldSingleMemberSyntax).You'll first need to install ESLint:
npm install eslint --save-devNext, install eslint-plugin-sort-imports-requires:
npm install eslint-plugin-sort-imports-requires --save-devAdd sort-imports-requires to the plugins section of your ESLint configuration file and configure its rules. Here's an example:
// eslint.config.js
import sortImportRequires from 'eslint-plugin-sort-imports-requires';
export default [
{
plugins: {
'sort-imports-requires': sortImportRequires
},
rules: {
'sort-imports-requires/sort-imports': ['error', { unsafeAutofix: true }],
'sort-imports-requires/sort-requires': ['error', { unsafeAutofix: true }]
}
}
];sort-imports and sort-requiresThese are the only supported rules and can be configured independently. Both have exactly the same options as ESLint's sort-imports rule, with a few more options:
unsafeAutofix (default: false)useAliases (default: true)useOldSingleMemberSyntax (default: false)unsafeAutofixWhether to autofix potentially unsafe scenarios automatically when the --fix flag is used when calling eslint.
The current scenarios considered unsafe are:
sort-imports rule does not autofix.const { [foo()]: bar } = require('bar'). In this scenario, the order in which keys are declared might matter.Enable this option at your own discretion.
useAliasesWhether to use aliases when sorting.
Consider the following import:
import { foo as bar } from 'some-module';If useAliases is enabled, bar is used when sorting. If it was disabled, foo would have been used instead.
useOldSingleMemberSyntaxWhether to restore the old ESLint behavior where multiple type corresponds to all named imports (regardless of how many are imported), while the single type corresponds only to default imports.
Install dependencies:
npm iRun tests:
npm run testThe release process is automated via the release GitHub workflow. Run it by clicking the "Run workflow" button.
ruimarinho
ruipmarinho@gmail.com
Estimated unpacked size across 3 packages.
552.8 kB
Includes dependencies.