Skip to content

Commit

Permalink
[Fix] order: Fix alphabetize for mixed requires and imports
Browse files Browse the repository at this point in the history
Fixes #1625
  • Loading branch information
wschurman authored and ljharb committed Jan 24, 2020
1 parent bbd166b commit 5d00854
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
- [`no-duplicates`]: allow duplicate imports if one is a namespace and the other not ([#1612], thanks [@sveyret])
- Add some missing rule meta schemas and types ([#1620], thanks [@bmish])
- [`named`]: for importing from a module which re-exports named exports from a `node_modules` module ([#1569], [#1447], thanks [@redbugz], [@kentcdodds])
- [`order`]: Fix alphabetize for mixed requires and imports ([#5625], thanks [@wschurman])

### Changed
- [`import/external-module-folders` setting] behavior is more strict now: it will only match complete path segments ([#1605], thanks [@skozin])
Expand Down Expand Up @@ -650,6 +651,7 @@ for info on changes for earlier releases.
[`memo-parser`]: ./memo-parser/README.md

[#1635]: https://github.com/benmosher/eslint-plugin-import/issues/1635
[#1625]: https://github.com/benmosher/eslint-plugin-import/pull/1625
[#1620]: https://github.com/benmosher/eslint-plugin-import/pull/1620
[#1619]: https://github.com/benmosher/eslint-plugin-import/pull/1619
[#1616]: https://github.com/benmosher/eslint-plugin-import/issues/1616
Expand Down Expand Up @@ -1099,3 +1101,4 @@ for info on changes for earlier releases.
[@redbugz]: https://github.com/redbugz
[@kentcdodds]: https://github.com/kentcdodds
[@IvanGoncharov]: https://github.com/IvanGoncharov
[@wschurman]: https://github.com/wschurman
2 changes: 1 addition & 1 deletion src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ function mutateRanksToAlphabetize(imported, alphabetizeOptions) {
let newRank = 0
const alphabetizedRanks = groupRanks.sort().reduce(function(acc, groupRank) {
groupedByRanks[groupRank].forEach(function(importedItemName) {
acc[importedItemName] = newRank
acc[importedItemName] = parseInt(groupRank, 10) + newRank
newRank += 1
})
return acc
Expand Down
32 changes: 32 additions & 0 deletions tests/src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,22 @@ ruleTester.run('order', rule, {
'newlines-between': 'always',
}],
}),
// Alphabetize with require
test({
code: `
import { hello } from './hello';
import { int } from './int';
const blah = require('./blah');
const { cello } = require('./cello');
`,
options: [
{
alphabetize: {
order: 'asc',
},
},
],
}),
],
invalid: [
// builtin before external module (require)
Expand Down Expand Up @@ -1986,5 +2002,21 @@ ruleTester.run('order', rule, {
message: '`foo` import should occur before import of `Bar`',
}],
}),
// Alphabetize with require
test({
code: `
const { cello } = require('./cello');
import { int } from './int';
const blah = require('./blah');
import { hello } from './hello';
`,
errors: [{
ruleId: 'order',
message: '`./int` import should occur before import of `./cello`',
}, {
ruleId: 'order',
message: '`./hello` import should occur before import of `./cello`',
}],
}),
].filter((t) => !!t),
})

0 comments on commit 5d00854

Please sign in to comment.