Skip to content

Commit 5e7ea1d

Browse files
Xunnamiusljharb
authored andcommitted
[Fix] order: ensure arcane imports do not cause undefined behavior
1 parent 42ce856 commit 5e7ea1d

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
1616
### Fixed
1717
- [`no-unused-modules`]: provide more meaningful error message when no .eslintrc is present ([#3116], thanks [@michaelfaith])
1818
- configs: added missing name attribute for eslint config inspector ([#3151], thanks [@NishargShah])
19+
- [`order`]: ensure arcane imports do not cause undefined behavior ([#3128], thanks [@Xunnamius])
1920

2021
### Changed
2122
- [Docs] [`extensions`], [`order`]: improve documentation ([#3106], thanks [@Xunnamius])
@@ -1173,6 +1174,7 @@ for info on changes for earlier releases.
11731174

11741175
[#3151]: https://github.com/import-js/eslint-plugin-import/pull/3151
11751176
[#3138]: https://github.com/import-js/eslint-plugin-import/pull/3138
1177+
[#3128]: https://github.com/import-js/eslint-plugin-import/pull/3128
11761178
[#3127]: https://github.com/import-js/eslint-plugin-import/pull/3127
11771179
[#3125]: https://github.com/import-js/eslint-plugin-import/pull/3125
11781180
[#3122]: https://github.com/import-js/eslint-plugin-import/pull/3122

src/rules/order.js

+4
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

+30-2
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 'fs';
3744+
3745+
import '@scoped/package';
3746+
import type { B } from '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)