GithubHelp home page GithubHelp logo

uphold / eslint-plugin-sort-imports-requires Goto Github PK

View Code? Open in Web Editor NEW
1.0 41.0 1.0 505 KB

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

License: MIT License

JavaScript 100.00%

eslint-plugin-sort-imports-requires's Introduction

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 i 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 .eslintrc configuration file. You can omit the eslint-plugin- prefix:

{
  "plugins": [
    "sort-imports-requires"
  ]
}

Then configure the rules you want to use under the rules section.

{
  "rules": {
    "sort-imports-requires/sort-imports": "error",
    "sort-imports-requires/sort-requires": "error"
  }
}

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

eslint-plugin-sort-imports-requires's People

Contributors

dependabot[bot] avatar satazor avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

eslint-plugin-sort-imports-requires's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.