Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement alphabetize case insensitive #1586

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
This change log adheres to standards from [Keep a CHANGELOG](http://keepachangelog.com).

## [Unreleased]
### Added
- [`order`]: added `caseInsensitive` as an additional option to `alphabetize` ([#1586], thanks [@dbrewer5])

### Fixed
- [`no-unused-modules`]: fix usage of `import/extensions` settings ([#1560], thanks [@stekycz])

Expand Down Expand Up @@ -625,6 +628,7 @@ for info on changes for earlier releases.

[`memo-parser`]: ./memo-parser/README.md

[#1586]: https://github.com/benmosher/eslint-plugin-import/pull/1586
[#1563]: https://github.com/benmosher/eslint-plugin-import/pull/1563
[#1560]: https://github.com/benmosher/eslint-plugin-import/pull/1560
[#1551]: https://github.com/benmosher/eslint-plugin-import/pull/1551
Expand Down Expand Up @@ -1046,3 +1050,4 @@ for info on changes for earlier releases.
[@randallreedjr]: https://github.com/randallreedjr
[@Pessimistress]: https://github.com/Pessimistress
[@stekycz]: https://github.com/stekycz
[@dbrewer5]: https://github.com/dbrewer5
6 changes: 5 additions & 1 deletion docs/rules/order.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,18 @@ import index from './';
import sibling from './foo';
```

### `alphabetize: {order: asc|desc|ignore}`:
### `alphabetize: {order: asc|desc|ignore, caseInsensitive: true|false}`:

Sort the order within each group in alphabetical manner based on **import path**:

- `order`: use `asc` to sort in ascending order, and `desc` to sort in descending order (default: `ignore`).
- `caseInsensitive`: use `true` to ignore case, and `false` to consider case (default: `false`).

Example setting:
```js
alphabetize: {
order: 'asc', /* sort in ascending order. Options: ['ignore', 'asc', 'desc'] */
caseInsensitive: true /* ignore case. Options: [true, false] */
}
```

Expand All @@ -210,12 +212,14 @@ import React, { PureComponent } from 'react';
import aTypes from 'prop-types';
import { compose, apply } from 'xcompose';
import * as classnames from 'classnames';
import blist from 'BList';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps a relative path would be better here, since npm package names can't be capitalized (for many years now)

```

While this will pass:

```js
/* eslint import/order: ["error", {"alphabetize": true}] */
import blist from 'BList';
import * as classnames from 'classnames';
import aTypes from 'prop-types';
import React, { PureComponent } from 'react';
Expand Down
16 changes: 11 additions & 5 deletions src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ function importsSorterDesc(importA, importB) {
return 0
}

function mutateRanksToAlphabetize(imported, order) {
function mutateRanksToAlphabetize(imported, alphabetizeOptions) {
const groupedByRanks = imported.reduce(function(acc, importedItem) {
if (!Array.isArray(acc[importedItem.rank])) {
acc[importedItem.rank] = []
Expand All @@ -278,10 +278,11 @@ function mutateRanksToAlphabetize(imported, order) {

const groupRanks = Object.keys(groupedByRanks)

const sorterFn = order === 'asc' ? importsSorterAsc : importsSorterDesc
const sorterFn = alphabetizeOptions.order === 'asc' ? importsSorterAsc : importsSorterDesc
const comparator = alphabetizeOptions.caseInsensitive ? (a, b) => sorterFn(String(a).toLowerCase(), String(b).toLowerCase()) : (a, b) => sorterFn(a, b)
// sort imports locally within their group
groupRanks.forEach(function(groupRank) {
groupedByRanks[groupRank].sort(sorterFn)
groupedByRanks[groupRank].sort(comparator)
})

// assign globally unique rank to each import
Expand Down Expand Up @@ -487,8 +488,9 @@ function makeNewlinesBetweenReport (context, imported, newlinesBetweenImports) {
function getAlphabetizeConfig(options) {
const alphabetize = options.alphabetize || {}
const order = alphabetize.order || 'ignore'
const caseInsensitive = alphabetize.caseInsensitive || false

return {order}
return {order, caseInsensitive}
}

module.exports = {
Expand Down Expand Up @@ -540,6 +542,10 @@ module.exports = {
alphabetize: {
type: 'object',
properties: {
caseInsensitive: {
type: 'boolean',
default: false,
},
order: {
enum: ['ignore', 'asc', 'desc'],
default: 'ignore',
Expand Down Expand Up @@ -604,7 +610,7 @@ module.exports = {
}

if (alphabetize.order !== 'ignore') {
mutateRanksToAlphabetize(imported, alphabetize.order)
mutateRanksToAlphabetize(imported, alphabetize)
}

makeOutOfOrderReport(context, imported)
Expand Down
46 changes: 46 additions & 0 deletions tests/src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -1870,5 +1870,51 @@ ruleTester.run('order', rule, {
message: '`bar` import should occur before import of `Bar`',
}],
}),
// Option alphabetize {order: 'asc': caseInsensitive: true}
test({
code: `
import b from 'foo';
import a from 'Bar';

import index from './';
`,
output: `
import a from 'Bar';
import b from 'foo';

import index from './';
`,
options: [{
groups: ['external', 'index'],
alphabetize: {order: 'asc', caseInsensitive: true},
}],
errors: [{
ruleID: 'order',
message: '`Bar` import should occur before import of `foo`',
}],
}),
// Option alphabetize {order: 'desc': caseInsensitive: true}
test({
code: `
import a from 'Bar';
import b from 'foo';

import index from './';
`,
output: `
import b from 'foo';
import a from 'Bar';

import index from './';
`,
options: [{
groups: ['external', 'index'],
alphabetize: {order: 'desc', caseInsensitive: true},
}],
errors: [{
ruleID: 'order',
message: '`foo` import should occur before import of `Bar`',
}],
}),
].filter((t) => !!t),
})