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

[Fix] order: ensure arcane imports do not cause undefined behavior #3128

Merged
merged 1 commit into from
Jan 29, 2025
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 @@ -16,6 +16,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
### Fixed
- [`no-unused-modules`]: provide more meaningful error message when no .eslintrc is present ([#3116], thanks [@michaelfaith])
- configs: added missing name attribute for eslint config inspector ([#3151], thanks [@NishargShah])
- [`order`]: ensure arcane imports do not cause undefined behavior ([#3128], thanks [@Xunnamius])

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

[#3151]: https://github.com/import-js/eslint-plugin-import/pull/3151
[#3138]: https://github.com/import-js/eslint-plugin-import/pull/3138
[#3128]: https://github.com/import-js/eslint-plugin-import/pull/3128
[#3127]: https://github.com/import-js/eslint-plugin-import/pull/3127
[#3125]: https://github.com/import-js/eslint-plugin-import/pull/3125
[#3122]: https://github.com/import-js/eslint-plugin-import/pull/3122
Expand Down
4 changes: 4 additions & 0 deletions src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,10 @@ function computeRank(context, ranks, importEntry, excludedImportTypes, isSorting

if (typeof rank === 'undefined') {
rank = ranks.groups[impType];

if (typeof rank === 'undefined') {
return -1;
}
}

if (isTypeOnlyImport && isSortingTypesGroup) {
Expand Down
32 changes: 30 additions & 2 deletions tests/src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -3115,7 +3115,6 @@ context('TypeScript', function () {
}),
// Option alphabetize: {order: 'asc'} with type group & path group
test({
// only: true,
code: `
import c from 'Bar';
import a from 'foo';
Expand Down Expand Up @@ -3145,7 +3144,6 @@ context('TypeScript', function () {
}),
// Option alphabetize: {order: 'asc'} with path group
test({
// only: true,
code: `
import c from 'Bar';
import type { A } from 'foo';
Expand Down Expand Up @@ -3739,6 +3737,36 @@ context('TypeScript', function () {
},
],
}),
// Ensure the rule doesn't choke and die on absolute paths trying to pass NaN around
test({
code: `
import fs from 'fs';

import '@scoped/package';
import type { B } from 'fs';

import type { A1 } from '/bad/bad/bad/bad';
import './a/b/c';
import type { A2 } from '/bad/bad/bad/bad';
import type { A3 } from '/bad/bad/bad/bad';
import type { D1 } from '/bad/bad/not/good';
import type { D2 } from '/bad/bad/not/good';
import type { D3 } from '/bad/bad/not/good';

import type { C } from '@something/else';

import type { E } from './index.js';
`,
...parserConfig,
options: [
{
alphabetize: { order: 'asc' },
groups: ['builtin', 'type', 'unknown', 'external'],
sortTypesGroup: true,
'newlines-between': 'always',
},
],
}),
),
invalid: [].concat(
// Option alphabetize: {order: 'asc'}
Expand Down