Skip to content

Commit a3f29ef

Browse files
committed
Fix the ordering of object-imports
1 parent d387af3 commit a3f29ef

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

src/rules/order.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import importType from '../core/importType'
55
import isStaticRequire from '../core/staticRequire'
66
import docsUrl from '../docsUrl'
77

8-
const defaultGroups = ['builtin', 'external', 'parent', 'sibling', 'index']
8+
const defaultGroups = ['builtin', 'external', 'parent', 'sibling', 'index', 'object']
99

1010
// REPORTING AND FIXING
1111

@@ -253,9 +253,9 @@ function getSorter(ascending) {
253253
return function importsSorter(importA, importB) {
254254
let result
255255

256-
if ((importA < importB) || importB === null) {
256+
if (importA < importB) {
257257
result = -1
258-
} else if ((importA > importB) || importA === null) {
258+
} else if (importA > importB) {
259259
result = 1
260260
} else {
261261
result = 0
@@ -310,24 +310,29 @@ function computePathRank(ranks, pathGroups, path, maxPosition) {
310310
}
311311
}
312312

313-
function computeRank(context, ranks, name, type, excludedImportTypes) {
314-
const impType = importType(name, context)
313+
function computeRank(context, node, ranks, name, type, excludedImportTypes) {
314+
let impType
315+
if (type === 'import:object') {
316+
impType = 'object'
317+
} else {
318+
impType = importType(name, context)
319+
}
315320
let rank
316321
if (!excludedImportTypes.has(impType)) {
317322
rank = computePathRank(ranks.groups, ranks.pathGroups, name, ranks.maxPosition)
318323
}
319324
if (typeof rank === 'undefined') {
320325
rank = ranks.groups[impType]
321326
}
322-
if (type !== 'import') {
327+
if (!/import(:.*)?/.test(type)) {
323328
rank += 100
324329
}
325330

326331
return rank
327332
}
328333

329334
function registerNode(context, node, name, type, ranks, imported, excludedImportTypes) {
330-
const rank = computeRank(context, ranks, name, type, excludedImportTypes)
335+
const rank = computeRank(context, node, ranks, name, type, excludedImportTypes)
331336
if (rank !== -1) {
332337
imported.push({name, rank, node})
333338
}
@@ -338,7 +343,7 @@ function isInVariableDeclarator(node) {
338343
(node.type === 'VariableDeclarator' || isInVariableDeclarator(node.parent))
339344
}
340345

341-
const types = ['builtin', 'external', 'internal', 'unknown', 'parent', 'sibling', 'index']
346+
const types = ['builtin', 'external', 'internal', 'unknown', 'parent', 'sibling', 'index', 'object']
342347

343348
// Creates an object with type-rank pairs.
344349
// Example: { index: 0, sibling: 1, parent: 1, external: 1, builtin: 2, internal: 2 }
@@ -563,7 +568,7 @@ module.exports = {
563568
create: function importOrderRule (context) {
564569
const options = context.options[0] || {}
565570
const newlinesBetweenImports = options['newlines-between'] || 'ignore'
566-
const pathGroupsExcludedImportTypes = new Set(options['pathGroupsExcludedImportTypes'] || ['builtin', 'external'])
571+
const pathGroupsExcludedImportTypes = new Set(options['pathGroupsExcludedImportTypes'] || ['builtin', 'external', 'object'])
567572
const alphabetize = getAlphabetizeConfig(options)
568573
let ranks
569574

@@ -609,18 +614,20 @@ module.exports = {
609614
},
610615
TSImportEqualsDeclaration: function handleImports(node) {
611616
let name
617+
let type = 'import'
612618
if (node.moduleReference.type === 'TSExternalModuleReference') {
613619
name = node.moduleReference.expression.value
614620
} else if (node.isExport) {
615621
name = node.moduleReference.name
616622
} else {
617-
name = null
623+
name = context.getSourceCode().getText(node.moduleReference)
624+
type += ':object'
618625
}
619626
registerNode(
620627
context,
621628
node,
622629
name,
623-
'import',
630+
type,
624631
ranks,
625632
imported,
626633
pathGroupsExcludedImportTypes

0 commit comments

Comments
 (0)