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

[Fix] order: Fix alphabetize for mixed requires and imports #1626

Merged
merged 1 commit into from
Feb 2, 2020
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
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),
})