Skip to content
ESlint plugin for sorting requires (CommonJS modules) alphabetically
JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci
test
.babelrc Initial commit Jan 24, 2020
.editorconfig
.eslintignore
.eslintrc.js
.gitignore
.prettierrc
LICENCE
README.md
index.js
package-lock.json
package.json

README.md

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)

You can’t perform that action at this time.