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-require-sort

v1.3.0

Published

ESlint plugin for sorting requires (CommonJS modules) alphabetically

Downloads

7,278

Readme

eslint-plugin-require-sort

Introduction

What is require? From latest Node documentation require is a function. As Node follows CommonJS module system, require function is the easiest way to include modules that exist in separate files - more on that here.

Difference between CommonJS and ES2015 modules is very nicely explained in this short talk.

From AST point of view const foo = require('bar') is a VariableDeclaration and require is CallExpression. And in this eslint plugin is treated as such. foo is in this case an Identifier. In case of destructuring const { foo, bar } = require('baz'), foo and bar are properties in ObjectPattern.

This is important for nomenclature in the plugin, because it's not straight forward as in sort-imports rule provided by eslint.

In this plugin all Identifiers are called properties to simplify things. The goal is to follow the AST as closely as possible. What member is to sort-imports rule, properties are to this plugin.

Hopefully this all makes sense.

Installation

Install ESLint:

$ npm install eslint --save-dev

Install eslint-plugin-require-sort:

$ npm install eslint-plugin-require-sort --save-dev

Note: If you installed ESLint globally (using the -g flag) then you must also install eslint-plugin-require-sort globally.

Usage

Add require-sort to the plugins and rules section of your .eslintrc(.js|.json|.yaml) configuration file:

{
  "plugins": ["require-sort"],
  "rules": {
    "require-sort/require-sort": "error"
  }
}

Rule Details

This rule checks all declarations and verifies that all are first sorted by the used property syntax and then alphabetically by the first property or alias name.

The --fix option on the command line automatically fixes some problems reported by this rule: multiple properties on a single line are automatically sorted (e.g. const { b, a } = require('foo') is corrected to const { a, b } = require('foo')), but multiple lines are not reordered.

Rule ignores require functions inside functions, if statements, etc. For example,

function foo() {
  const bar = require('baz');
}

will be ignored. Only top level variable declarations with require are considered.

Options

This rule accepts an object with its properties as

  • ignoreCase (default: false)
  • ignoreDeclarationOrder (default: false)
  • ignorePropertySort (default: false)
  • propertySyntaxSortOrder (default: ["note", "multiple", "single"]); all items must be present in the array, but you can change the order.

Default option settings are:

{
    "require-sort/require-sort": ["error", {
        "ignoreCase": false,
        "ignoreDeclarationSort": false,
        "ignorePropertySort": false,
        "propertySyntaxSortOrder": ["none", "multiple", "single"]
    }]
}

Examples

Default settings

Examples of correct code for this rule when using default options:

/*eslint require-sort: "error"*/
const { alpha, beta } = require('alpha');
const { delta, gamma } = require('delta');
const a = require('baz');
const b = require('qux');

/*eslint require-sort: "error"*/
const a = require('foo');
const b = require('bar');
const c = require('baz');

/*eslint require-sort: "error"*/
const { a, b } = require('baz');
const c = require('qux');

/*eslint require-sort: "error"*/
const { a, b, c } = require('foo)'

Examples of incorrect code for this rule when using default options:

/*eslint require-sort: "error"*/
const b = require('foo');
const a = require('bar');

/*eslint require-sort: "error"*/
const a = require('foo');
const A = require('bar');

/*eslint require-sort: "error"*/
const { b, c } = require('foo');
const { a, b } = require('bar');

/*eslint require-sort: "error"*/
const a = require('foo');
const { b, c } = require('bar');

/*eslint require-sort: "error"*/
const a = require('foo');

/*eslint require-sort: "error"*/
const { b, a, c } = require('foo');

ignoreCase

When true the rule ignores the case-sensitivity of the declaration

Examples of incorrect code for this rule with the { "ignoreCase": true } option:

/*eslint require-sort: ["error", { "ignoreCase": true }]*/

const b = require('foo');
const a = require('bar');

Examples of correct code for this rule with the { "ignoreCase": true } option:

/*eslint require-sort: ["error", { "ignoreCase": true }]*/

const a = require('foo');
const B = require('bar');
const c = require('baz');

Default is false.

ignoreDeclarationSort

Ignores the sorting of variable declarations with require.

Examples of incorrect code for this rule with the default { "ignoreDeclarationSort": false } option:

/*eslint require-sort: ["error", { "ignoreDeclarationSort": false }]*/
const b = require('foo');
const a = require('bar');

Examples of correct code for this rule with the { "ignoreDeclarationSort": true } option:

/*eslint require-sort: ["error", { "ignoreDeclarationSort": true }]*/
const a = require('foo');
const b = require('bar');
/*eslint require-sort: ["error", { "ignoreDeclarationSort": true }]*/
const b = require('foo');
const a = require('bar');

Default is false.

ignorePropertySort

Ignores the property sorting within a multiple property in declaration.

Examples of incorrect code for this rule with the default { "ignorePropertySort": false } option:

/*eslint require-sort: ["error", { "ignorePropertySort": false }]*/
const { b, a, c } = require('foo');

Examples of correct code for this rule with the { "ignorePropertySort": true } option:

/*eslint require-sort: ["error", { "ignorePropertySort": true }]*/
const { b, a, c } = require('foo');

Default is false.

propertySyntaxSortOrder

There are three different styles and the default property syntax sort order is: s.

  • none - just a require expression
  • multiple - require multiple properties.
  • single - require single property.

Both options must be specified in the array, but you can customize their order.

Examples of correct code for this rule with the { "propertySyntaxSortOrder": ["none", "single", "multiple"] } option:

/*eslint require-sort: ["error", { "propertySyntaxSortOrder":  ["none", "single", "multiple"]  }]*/
require('bar');
const z = require('zoo');
const { a, b } = require('foo');

Default is ["none", "multiple", "single"].

Credits

Contributions

  • All contributions, suggestions are welcome.

License

MIT @ Zdravko Ćurić (zcuric)