Skip to content

Commit a22eaf6

Browse files
committedOct 12, 2023
fix: improve recognition of external modules
1 parent 6205d70 commit a22eaf6

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed
 

‎rules/sort-imports.ts

+30-2
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,26 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
161161
groups: [],
162162
})
163163

164+
let hasUnknownGroup = false
165+
166+
for (let group of options.groups) {
167+
if (Array.isArray(group)) {
168+
for (let subGroup of group) {
169+
if (subGroup === 'unknown') {
170+
hasUnknownGroup = true
171+
}
172+
}
173+
} else {
174+
if (group === 'unknown') {
175+
hasUnknownGroup = true
176+
}
177+
}
178+
}
179+
180+
if (!hasUnknownGroup) {
181+
options.groups = [...options.groups, 'unknown']
182+
}
183+
164184
let source = context.getSourceCode()
165185

166186
let nodes: SortingNode[] = []
@@ -218,6 +238,9 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
218238
)
219239
}
220240

241+
let isExternal = (value: string) =>
242+
!(value.startsWith('.') || value.startsWith('/'))
243+
221244
if (node.importKind === 'type') {
222245
if (node.type === 'ImportDeclaration') {
223246
setCustomGroups(options['custom-groups'].type, node.source.value)
@@ -241,9 +264,12 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
241264
if (isCoreModule(node.source.value)) {
242265
defineGroup('builtin-type')
243266
}
267+
268+
if (isExternal(node.source.value)) {
269+
defineGroup('external-type')
270+
}
244271
}
245272

246-
defineGroup('external-type')
247273
defineGroup('type')
248274
}
249275

@@ -278,7 +304,9 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
278304
defineGroup('builtin')
279305
}
280306

281-
defineGroup('external')
307+
if (isExternal(node.source.value)) {
308+
defineGroup('external')
309+
}
282310
}
283311

284312
return getGroup()

‎test/sort-imports.test.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -851,8 +851,7 @@ describe(RULE_NAME, () => {
851851
'internal',
852852
['parent-type', 'sibling-type', 'index-type'],
853853
['parent', 'sibling', 'index'],
854-
'object',
855-
'unknown',
854+
['object', 'unknown'],
856855
],
857856
},
858857
],
@@ -1989,8 +1988,7 @@ describe(RULE_NAME, () => {
19891988
'internal',
19901989
['parent-type', 'sibling-type', 'index-type'],
19911990
['parent', 'sibling', 'index'],
1992-
'object',
1993-
'unknown',
1991+
['object', 'unknown'],
19941992
],
19951993
},
19961994
],
@@ -3168,8 +3166,7 @@ describe(RULE_NAME, () => {
31683166
'internal',
31693167
['parent-type', 'sibling-type', 'index-type'],
31703168
['parent', 'sibling', 'index'],
3171-
'object',
3172-
'unknown',
3169+
['object', 'unknown'],
31733170
],
31743171
},
31753172
],

0 commit comments

Comments
 (0)
Please sign in to comment.