From 86f6b4203610974561482ddabd38dd517c682314 Mon Sep 17 00:00:00 2001 From: dannysindra Date: Sun, 3 Feb 2019 12:00:25 -0800 Subject: [PATCH] update configs for alphabetize --- docs/rules/order.md | 56 +++++++++++++++++++++++++++++++++++++++------ src/rules/order.js | 19 +++++---------- 2 files changed, 55 insertions(+), 20 deletions(-) diff --git a/docs/rules/order.md b/docs/rules/order.md index c353578b78..c4b0343076 100644 --- a/docs/rules/order.md +++ b/docs/rules/order.md @@ -163,24 +163,66 @@ import index from './'; import sibling from './foo'; ``` -### `alphabetize`: boolean +### `alphabetize: object`: -By default, no ordering within groups is enforced. When `alphabetize: true` is used, imports must be alphabetically ordered within their groups. +Sort the order within each group in alphabetical manner: -### Fail +- `order`: use `asc` to sort in ascending order, and `desc` to sort in descending order (default: `ignore`). +- `ignoreCase` [boolean]: when `true`, the rule ignores case-sensitivity of the import name (default: `false`). +Example setting: ```js -/* eslint import/order: ["error", {"alphabetize": true}] */ -import buffer from 'buffer'; -import assert from 'assert'; +alphabetize: { + order: 'asc', /* sort in ascending order. Options: ['ignore', 'asc', 'desc'] */ + ignoreCase: false, /* case-sensitive. This property does not have any effect if 'order' is set to 'ignore' */ +} +``` + +This will fail the rule check: + +```js +import foo from 'foo'; +import bar from 'bar'; +import Baz from 'Baz'; ``` -### Pass +While this will pass: + +```js +import Baz from 'Baz'; +import bar from 'bar'; +import foo from 'foo'; + +Sort the order within each group in alphabetical manner: + +- `order`: use `asc` to sort in ascending order, and `desc` to sort in descending order (default: `ignore`). +- `ignoreCase` [boolean]: when `true`, the rule ignores case-sensitivity of the import name (default: `false`). + +Example setting: +```js +alphabetize: { + order: 'asc', /* sort in ascending order. Options: ['ignore', 'asc', 'desc'] */ + ignoreCase: false, /* case-sensitive. This property does not have any effect if 'order' is set to 'ignore' */ +} +``` + +This will fail the rule check: ```js /* eslint import/order: ["error", {"alphabetize": true}] */ import assert from 'assert'; import buffer from 'buffer'; +import foo from 'foo'; +import bar from 'bar'; +import Baz from 'Baz'; +``` + +While this will pass: + +```js +import Baz from 'Baz'; +import bar from 'bar'; +import foo from 'foo'; ``` ## Related diff --git a/src/rules/order.js b/src/rules/order.js index ec89f23ff3..13aeb8b2bb 100644 --- a/src/rules/order.js +++ b/src/rules/order.js @@ -13,7 +13,6 @@ function reverse(array) { return { name: v.name, rank: -v.rank, - groupRank: -v.rank, node: v.node, } }).reverse() @@ -280,21 +279,15 @@ function mutateRanksToAlphabetize(imported, order, ignoreCase) { // DETECTING -function computeGroupRank(context, ranks, name, type) { +function computeRank(context, ranks, name, type) { return ranks[importType(name, context)] + (type === 'import' ? 0 : 100) } function registerNode(context, node, name, type, ranks, imported) { - const groupRank = computeGroupRank(context, ranks, name, type) - if (groupRank !== -1) { - imported.push({ - name, - groupRank, - // Before, or without alphabetization, individual rank matches group rank - rank: groupRank, - node, - }) + const rank = computeRank(context, ranks, name, type) + if (rank !== -1) { + imported.push({name, rank, node}) } } @@ -378,13 +371,13 @@ function makeNewlinesBetweenReport (context, imported, newlinesBetweenImports) { if (newlinesBetweenImports === 'always' || newlinesBetweenImports === 'always-and-inside-groups') { - if (currentImport.groupRank !== previousImport.groupRank && emptyLinesBetween === 0) { + if (currentImport.rank !== previousImport.rank && emptyLinesBetween === 0) { context.report({ node: previousImport.node, message: 'There should be at least one empty line between import groups', fix: fixNewLineAfterImport(context, previousImport, currentImport), }) - } else if (currentImport.groupRank === previousImport.groupRank + } else if (currentImport.rank === previousImport.rank && emptyLinesBetween > 0 && newlinesBetweenImports !== 'always-and-inside-groups') { context.report({