1.0.0 • Published 5 years ago

@zcuric/eslint-config-sort-requires v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

eslint-plugin-sort-requires

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 explain 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 in sort-imports rule, properties are in this plugin.

Hopefully this all makes sense.

Installation

Install ESLint:

$ npm install eslint --save-dev

Install eslint-plugin-sort-requires:

$ npm install @zcuric/eslint-plugin-sort-requires --save-dev

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

Usage

Add sort-requires to the plugins section of your .eslintrc(.js|json) configuration file:

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

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.js') is corrected to const { a, b } = require('foo.js')), 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)
  • ignoreDeclarationSort (default: false)
  • ignorePropertySort (default: false)
  • propertySyntaxSortOrder (default: ["multiple", "single"]); both items must be present in the array, but you can change the order.

Default option settings are:

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

Examples

Default settings

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

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

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

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

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

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

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

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

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

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

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

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

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 sort-requires: ["error", { "ignoreCase": true }]*/

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

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

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

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

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 sort-requires: ["error", { "ignoreDeclarationSort": false }]*/
const b = require('foo.js')
const a = require('bar.js')

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

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

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 sort-requires: ["error", { "ignorePropertySort": false }]*/
const { b, a, c } = require('foo.js)'

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

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

Default is false.

propertySyntaxSortOrder

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

  • 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": ['single', 'multiple'] } option:

/*eslint sort-requires: ["error", { "propertySyntaxSortOrder": ['single', 'multiple'] }]*/

const z = require('zoo.js');
const { a, b } = require('foo.js');

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

Credits

Contributions

  • All contributions, suggestions are welcome.

License

MIT @ Zdravko Ćurić (zcuric)