Skip to content

Commit

Permalink
Merge branch 'support-ts-resolver-v2' of github.com:joaovieira/eslint…
Browse files Browse the repository at this point in the history
…-plugin-import into support-ts-resolver-v2

# Conflicts:
#	tests/config/typescript.js
  • Loading branch information
joaovieira committed Dec 31, 2019
2 parents f46dc45 + 1fd0996 commit d98bf2e
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 10 deletions.
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ This project adheres to [Semantic Versioning](http://semver.org/).
This change log adheres to standards from [Keep a CHANGELOG](http://keepachangelog.com).

## [Unreleased]
### Fixed
- [`no-unused-modules`]: fix usage of `import/extensions` settings ([#1560], thanks [@stekycz])
### 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])
- [`import/extensions`]: ignore non-main modules ([#1563], thanks [@saschanaz])
- TypeScript config: lookup for external modules in @types folder ([#1526], thanks [@joaovieira])

## [2.19.1] - 2019-12-08
### Fixed
Expand Down Expand Up @@ -625,10 +627,12 @@ 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
[#1542]: https://github.com/benmosher/eslint-plugin-import/pull/1542
[#1526]: https://github.com/benmosher/eslint-plugin-import/pull/1526
[#1521]: https://github.com/benmosher/eslint-plugin-import/pull/1521
[#1519]: https://github.com/benmosher/eslint-plugin-import/pull/1519
[#1507]: https://github.com/benmosher/eslint-plugin-import/pull/1507
Expand Down Expand Up @@ -1046,3 +1050,5 @@ for info on changes for earlier releases.
[@randallreedjr]: https://github.com/randallreedjr
[@Pessimistress]: https://github.com/Pessimistress
[@stekycz]: https://github.com/stekycz
[@joaovieira]: https://github.com/joaovieira
[@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';
```

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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"scripts": {
"build": "babel --quiet --out-dir lib src",
"prebuild": "rimraf lib",
"watch": "npm run mocha -- --watch tests/src",
"watch": "npm run mocha -- --watch tests/{src,config}",
"pretest": "linklocal",
"posttest": "eslint .",
"mocha": "cross-env BABEL_ENV=test NODE_PATH=./src nyc -s mocha -R dot --recursive -t 5s",
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
2 changes: 1 addition & 1 deletion tests/config/typescript.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path'
import { expect } from 'chai'

const config = require(path.join(process.cwd(), 'config', 'typescript'))
const config = require(path.join(__dirname, '..', '..', 'config', 'typescript'))

describe('config typescript', () => {
// https://github.com/benmosher/eslint-plugin-import/issues/1525
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),
})

0 comments on commit d98bf2e

Please sign in to comment.