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 committed Jan 24, 2020
1 parent 99647f1 commit 29ad3bb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
- [`extensions`]: for invalid code where `name` does not exist, do not crash ([#1613], thanks [@ljharb])
- [`extentions`]: Fix scope regex ([#1611], thanks [@yordis])
- [`no-duplicates`]: allow duplicate imports if one is a namespace and the other not ([#1612], thanks [@sveyret])
- [`order`]: Fix alphabetize for mixed requires and imports ([#1625], 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
2 changes: 1 addition & 1 deletion src/rules/order.js
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
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 29ad3bb

Please sign in to comment.