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

[patch] order/TypeScript: ignore ordering of object imports #1831

Merged
merged 1 commit into from
Jun 23, 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
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)
manuth marked this conversation as resolved.
Show resolved Hide resolved
}
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