Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 29ad3bb

Browse files
committedJan 24, 2020
[Fix] order: Fix alphabetize for mixed requires and imports
Fixes #1625
1 parent 99647f1 commit 29ad3bb

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
1010
- [`extensions`]: for invalid code where `name` does not exist, do not crash ([#1613], thanks [@ljharb])
1111
- [`extentions`]: Fix scope regex ([#1611], thanks [@yordis])
1212
- [`no-duplicates`]: allow duplicate imports if one is a namespace and the other not ([#1612], thanks [@sveyret])
13+
- [`order`]: Fix alphabetize for mixed requires and imports ([#1625], thanks [@wschurman])
1314

1415
### Changed
1516
- [`import/external-module-folders` setting] behavior is more strict now: it will only match complete path segments ([#1605], thanks [@skozin])

‎src/rules/order.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ function mutateRanksToAlphabetize(imported, alphabetizeOptions) {
289289
let newRank = 0
290290
const alphabetizedRanks = groupRanks.sort().reduce(function(acc, groupRank) {
291291
groupedByRanks[groupRank].forEach(function(importedItemName) {
292-
acc[importedItemName] = newRank
292+
acc[importedItemName] = parseInt(groupRank, 10) + newRank
293293
newRank += 1
294294
})
295295
return acc

‎tests/src/rules/order.js

+32
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,22 @@ ruleTester.run('order', rule, {
645645
'newlines-between': 'always',
646646
}],
647647
}),
648+
// Alphabetize with require
649+
test({
650+
code: `
651+
import { hello } from './hello';
652+
import { int } from './int';
653+
const blah = require('./blah');
654+
const { cello } = require('./cello');
655+
`,
656+
options: [
657+
{
658+
alphabetize: {
659+
order: 'asc',
660+
},
661+
},
662+
],
663+
}),
648664
],
649665
invalid: [
650666
// builtin before external module (require)
@@ -1986,5 +2002,21 @@ ruleTester.run('order', rule, {
19862002
message: '`foo` import should occur before import of `Bar`',
19872003
}],
19882004
}),
2005+
// Alphabetize with require
2006+
test({
2007+
code: `
2008+
const { cello } = require('./cello');
2009+
import { int } from './int';
2010+
const blah = require('./blah');
2011+
import { hello } from './hello';
2012+
`,
2013+
errors: [{
2014+
ruleId: 'order',
2015+
message: '`./int` import should occur before import of `./cello`',
2016+
}, {
2017+
ruleId: 'order',
2018+
message: '`./hello` import should occur before import of `./cello`',
2019+
}],
2020+
}),
19892021
].filter((t) => !!t),
19902022
})

0 commit comments

Comments
 (0)
Please sign in to comment.