Skip to content

Commit

Permalink
update configs for alphabetize
Browse files Browse the repository at this point in the history
  • Loading branch information
dannysindra authored and Radim Svoboda committed May 14, 2019
1 parent e5e72f7 commit 86f6b42
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 20 deletions.
56 changes: 49 additions & 7 deletions docs/rules/order.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 6 additions & 13 deletions src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ function reverse(array) {
return {
name: v.name,
rank: -v.rank,
groupRank: -v.rank,
node: v.node,
}
}).reverse()
Expand Down Expand Up @@ -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})
}
}

Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit 86f6b42

Please sign in to comment.