npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

eslint-plugin-sort-imports-requires

v2.0.0

Published

An ESLint plugin to sort both import and require declarations in a unified manner.

Downloads

29,406

Readme

eslint-plugin-sort-imports-requires

An ESLint plugin to sort both import and require declarations in a unified manner.

Status

npm version build status

Motivation

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:

  • Provides autofix for potentially unsafe situations (see unsafeAutofix).
  • Allows sorting by aliases. (see useAliases).
  • Allows restoring the old ESLint behavior where multiple type corresponds to all named imports, regardless of how many are imported (see useOldSingleMemberSyntax).

Installation

You'll first need to install ESLint:

npm install eslint --save-dev

Next, install eslint-plugin-sort-imports-requires:

npm install eslint-plugin-sort-imports-requires --save-dev

Usage

Add 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 }]
    }
  }
];

Supported Rules

sort-imports and sort-requires

These 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)

unsafeAutofix

Whether to autofix potentially unsafe scenarios automatically when the --fix flag is used when calling eslint.

The current scenarios considered unsafe are:

  • Sorting import / require declarations because they can have side-effects, therefore the order in which they are executed might matter. That's the reason why the built-in ESLint sort-imports rule does not autofix.
  • Sorting dynamic keys with potential side-effects, e.g.: const { [foo()]: bar } = require('bar'). In this scenario, the order in which keys are declared might matter.

Enable this option at your own discretion.

useAliases

Whether 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.

useOldSingleMemberSyntax

Whether 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.

License

MIT

Contributing

Development

Install dependencies:

npm i

Run tests:

npm run test

Cutting a release

The release process is automated via the release GitHub workflow. Run it by clicking the "Run workflow" button.