Skip to content

Commit

Permalink
[patch] order/TypeScript: ignore ordering of object imports
Browse files Browse the repository at this point in the history
  • Loading branch information
manuth authored and ljharb committed Jun 19, 2020
1 parent bfc50b7 commit c38b169
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 47 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
- [`default`]: avoid crash with `export =` ([#1822], thanks [@AndrewLeedham])
- [`order`]/[`newline-after-import`]: ignore TypeScript's "export import object" ([#1830], thanks [@be5invis])
- [`dynamic-import-chunkname`]/TypeScript: supports `@typescript-eslint/parser` ([#1833], thanks [@noelebrun])
- [`order`]/TypeScript: ignore ordering of object imports ([#1831], thanks [@manuth])

### Changed
- [`no-extraneous-dependencies`]: add tests for importing types ([#1824], thanks [@taye])
Expand Down Expand Up @@ -716,6 +717,7 @@ for info on changes for earlier releases.
[`memo-parser`]: ./memo-parser/README.md

[#1833]: https://github.com/benmosher/eslint-plugin-import/pull/1833
[#1831]: https://github.com/benmosher/eslint-plugin-import/pull/1831
[#1830]: https://github.com/benmosher/eslint-plugin-import/pull/1830
[#1824]: https://github.com/benmosher/eslint-plugin-import/pull/1824
[#1823]: https://github.com/benmosher/eslint-plugin-import/pull/1823
Expand Down
67 changes: 37 additions & 30 deletions src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ const defaultGroups = ['builtin', 'external', 'parent', 'sibling', 'index']

function reverse(array) {
return array.map(function (v) {
return {
name: v.name,
rank: -v.rank,
node: v.node,
}
return Object.assign({}, v, { rank: -v.rank })
}).reverse()
}

Expand Down Expand Up @@ -197,8 +193,7 @@ function fixOutOfOrder(context, firstNode, secondNode, order) {
newCode = newCode + '\n'
}

const message = '`' + secondNode.name + '` import should occur ' + order +
' import of `' + firstNode.name + '`'
const message = `\`${secondNode.displayName}\` import should occur ${order} import of \`${firstNode.displayName}\``

if (order === 'before') {
context.report({
Expand Down Expand Up @@ -270,7 +265,7 @@ function mutateRanksToAlphabetize(imported, alphabetizeOptions) {
if (!Array.isArray(acc[importedItem.rank])) {
acc[importedItem.rank] = []
}
acc[importedItem.rank].push(importedItem.name)
acc[importedItem.rank].push(importedItem.value)
return acc
}, {})

Expand All @@ -295,7 +290,7 @@ function mutateRanksToAlphabetize(imported, alphabetizeOptions) {

// mutate the original group-rank with alphabetized-rank
imported.forEach(function(importedItem) {
importedItem.rank = alphabetizedRanks[importedItem.name]
importedItem.rank = alphabetizedRanks[importedItem.value]
})
}

Expand All @@ -310,31 +305,31 @@ function computePathRank(ranks, pathGroups, path, maxPosition) {
}
}

function computeRank(context, node, ranks, name, type, excludedImportTypes) {
function computeRank(context, ranks, importEntry, excludedImportTypes) {
let impType
if (type === 'import:object') {
let rank
if (importEntry.type === 'import:object') {
impType = 'object'
} else {
impType = importType(name, context)
impType = importType(importEntry.value, context)
}
let rank
if (!excludedImportTypes.has(impType)) {
rank = computePathRank(ranks.groups, ranks.pathGroups, name, ranks.maxPosition)
rank = computePathRank(ranks.groups, ranks.pathGroups, importEntry.value, ranks.maxPosition)
}
if (typeof rank === 'undefined') {
rank = ranks.groups[impType]
}
if (type !== 'import' && !type.startsWith('import:')) {
if (importEntry.type !== 'import' && !importEntry.type.startsWith('import:')) {
rank += 100
}

return rank
}

function registerNode(context, node, name, type, ranks, imported, excludedImportTypes) {
const rank = computeRank(context, node, ranks, name, type, excludedImportTypes)
function registerNode(context, importEntry, ranks, imported, excludedImportTypes) {
const rank = computeRank(context, ranks, importEntry, excludedImportTypes)
if (rank !== -1) {
imported.push({name, rank, node})
imported.push(Object.assign({}, importEntry, { rank }))
}
}

Expand Down Expand Up @@ -603,34 +598,43 @@ module.exports = {
const name = node.source.value
registerNode(
context,
node,
name,
'import',
{
node,
value: name,
displayName: name,
type: 'import',
},
ranks,
imported,
pathGroupsExcludedImportTypes
)
}
},
TSImportEqualsDeclaration: function handleImports(node) {
let name
let displayName
let value
let type
// skip "export import"s
if (node.isExport) {
return
}
if (node.moduleReference.type === 'TSExternalModuleReference') {
name = node.moduleReference.expression.value
value = node.moduleReference.expression.value
displayName = value
type = 'import'
} else {
name = context.getSourceCode().getText(node.moduleReference)
value = ''
displayName = context.getSourceCode().getText(node.moduleReference)
type = 'import:object'
}
registerNode(
context,
node,
name,
type,
{
node,
value,
displayName,
type,
},
ranks,
imported,
pathGroupsExcludedImportTypes
Expand All @@ -643,9 +647,12 @@ module.exports = {
const name = node.arguments[0].value
registerNode(
context,
node,
name,
'require',
{
node,
value: name,
displayName: name,
type: 'require',
},
ranks,
imported,
pathGroupsExcludedImportTypes
Expand Down
31 changes: 14 additions & 17 deletions tests/src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,7 @@ ruleTester.run('order', rule, {
},
],
}),
// Object-imports should not be forced to be alphabetized
test({
code: `
import debug = console.debug;
Expand All @@ -753,6 +754,19 @@ ruleTester.run('order', rule, {
},
],
}),
test({
code: `
import log = console.log;
import debug = console.debug;`,
parser,
options: [
{
alphabetize: {
order: 'asc',
},
},
],
}),
test({
code: `
import { a } from "./a";
Expand Down Expand Up @@ -1288,23 +1302,6 @@ ruleTester.run('order', rule, {
message: '`./blah` import should occur before import of `console.log`',
}],
}),
// Alphabetization of object-imports
test({
code: `
import log = console.log;
import debug = console.debug;`,
parser,
errors: [{
message: '`console.debug` import should occur before import of `console.log`',
}],
options: [
{
alphabetize: {
order: 'asc',
},
},
],
}),
]),
// Default order using import with custom import alias
test({
Expand Down

0 comments on commit c38b169

Please sign in to comment.