Skip to content

Commit

Permalink
fix: [import/order] ensure arcane imports do not cause undefined beha…
Browse files Browse the repository at this point in the history
…vior
  • Loading branch information
Xunnamius committed Dec 23, 2024
1 parent 2b9683d commit d929108
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,12 @@ function computeRank(context, ranks, importEntry, excludedImportTypes, isSorting
rank = computePathRank(ranks.groups, ranks.pathGroups, importEntry.value, ranks.maxPosition);
}

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

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

if (isTypeOnlyImport && isSortingTypesAmongThemselves) {
Expand Down
34 changes: 30 additions & 4 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 'node:fs';
import '@scoped/package';
import type { B } from 'node: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'],
sortTypesAmongThemselves: true,
'newlines-between': 'always'
},
],
}),
),
invalid: [].concat(
// Option alphabetize: {order: 'asc'}
Expand Down Expand Up @@ -3992,7 +4020,6 @@ context('TypeScript', function () {
message: '`A` export should occur before export of `B`',
}],
}),

// Options: sortTypesAmongThemselves + newlines-between-types example #1 from the documentation (fail)
test({
code: `
Expand Down Expand Up @@ -4053,7 +4080,6 @@ context('TypeScript', function () {
},
],
}),

// Options: sortTypesAmongThemselves + newlines-between-types example #2 from the documentation (fail)
test({
code: `
Expand Down

0 comments on commit d929108

Please sign in to comment.