Skip to content

Commit d929108

Browse files
committed
fix: [import/order] ensure arcane imports do not cause undefined behavior
1 parent 2b9683d commit d929108

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

src/rules/order.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,12 @@ function computeRank(context, ranks, importEntry, excludedImportTypes, isSorting
533533
rank = computePathRank(ranks.groups, ranks.pathGroups, importEntry.value, ranks.maxPosition);
534534
}
535535

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

540544
if (isTypeOnlyImport && isSortingTypesAmongThemselves) {

tests/src/rules/order.js

Lines changed: 30 additions & 4 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+
sortTypesAmongThemselves: true,
3766+
'newlines-between': 'always'
3767+
},
3768+
],
3769+
}),
37423770
),
37433771
invalid: [].concat(
37443772
// Option alphabetize: {order: 'asc'}
@@ -3992,7 +4020,6 @@ context('TypeScript', function () {
39924020
message: '`A` export should occur before export of `B`',
39934021
}],
39944022
}),
3995-
39964023
// Options: sortTypesAmongThemselves + newlines-between-types example #1 from the documentation (fail)
39974024
test({
39984025
code: `
@@ -4053,7 +4080,6 @@ context('TypeScript', function () {
40534080
},
40544081
],
40554082
}),
4056-
40574083
// Options: sortTypesAmongThemselves + newlines-between-types example #2 from the documentation (fail)
40584084
test({
40594085
code: `

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy