From 5d00854f3c59a497f0a850c6ccab01a012d6b8db Mon Sep 17 00:00:00 2001 From: William Schurman Date: Thu, 23 Jan 2020 17:45:24 -0700 Subject: [PATCH] [Fix] `order`: Fix alphabetize for mixed requires and imports Fixes #1625 --- CHANGELOG.md | 3 +++ src/rules/order.js | 2 +- tests/src/rules/order.js | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 165df6e54..4701322ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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]) @@ -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 @@ -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 diff --git a/src/rules/order.js b/src/rules/order.js index fcdf12bda..948c5f427 100644 --- a/src/rules/order.js +++ b/src/rules/order.js @@ -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 diff --git a/tests/src/rules/order.js b/tests/src/rules/order.js index 29ecc28e2..d5a0dd98b 100644 --- a/tests/src/rules/order.js +++ b/tests/src/rules/order.js @@ -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) @@ -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), })