Skip to content

Commit 64ccfb5

Browse files
committed
fix: [import/order] ensure arcane imports do not cause undefined behavior
1 parent d472b80 commit 64ccfb5

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/rules/order.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,10 @@ function computeRank(context, ranks, importEntry, excludedImportTypes, isSorting
535535

536536
if (typeof rank === 'undefined') {
537537
rank = ranks.groups[impType];
538+
539+
if (typeof rank === 'undefined') {
540+
return -1;
541+
}
538542
}
539543

540544
if (isTypeOnlyImport && isSortingTypesGroup) {

tests/src/rules/order.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3115,7 +3115,6 @@ context('TypeScript', function () {
31153115
}),
31163116
// Option alphabetize: {order: 'asc'} with type group & path group
31173117
test({
3118-
// only: true,
31193118
code: `
31203119
import c from 'Bar';
31213120
import a from 'foo';
@@ -3145,7 +3144,6 @@ context('TypeScript', function () {
31453144
}),
31463145
// Option alphabetize: {order: 'asc'} with path group
31473146
test({
3148-
// only: true,
31493147
code: `
31503148
import c from 'Bar';
31513149
import type { A } from 'foo';
@@ -3739,6 +3737,36 @@ context('TypeScript', function () {
37393737
},
37403738
],
37413739
}),
3740+
// Ensure the rule doesn't choke and die on absolute paths trying to pass NaN around
3741+
test({
3742+
code: `
3743+
import fs from 'node:fs';
3744+
3745+
import '@scoped/package';
3746+
import type { B } from 'node:fs';
3747+
3748+
import type { A1 } from '/bad/bad/bad/bad';
3749+
import './a/b/c';
3750+
import type { A2 } from '/bad/bad/bad/bad';
3751+
import type { A3 } from '/bad/bad/bad/bad';
3752+
import type { D1 } from '/bad/bad/not/good';
3753+
import type { D2 } from '/bad/bad/not/good';
3754+
import type { D3 } from '/bad/bad/not/good';
3755+
3756+
import type { C } from '@something/else';
3757+
3758+
import type { E } from './index.js';
3759+
`,
3760+
...parserConfig,
3761+
options: [
3762+
{
3763+
alphabetize: { order: 'asc' },
3764+
groups: ['builtin', 'type', 'unknown', 'external'],
3765+
sortTypesGroup: true,
3766+
'newlines-between': 'always',
3767+
},
3768+
],
3769+
}),
37423770
),
37433771
invalid: [].concat(
37443772
// Option alphabetize: {order: 'asc'}

0 commit comments

Comments
 (0)