Skip to content

Commit 1fa2971

Browse files
committed
[Fix] order: partial fix for #2687
1 parent a89eadf commit 1fa2971

File tree

3 files changed

+79
-10
lines changed

3 files changed

+79
-10
lines changed

src/rules/order.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ function makeOutOfOrderReport(context, imported) {
253253
if (!outOfOrder.length) {
254254
return;
255255
}
256+
256257
// There are things to report. Try to minimize the number of reported errors.
257258
const reversedImported = reverse(imported);
258259
const reversedOrder = findOutOfOrder(reversedImported);
@@ -426,11 +427,12 @@ const types = ['builtin', 'external', 'internal', 'unknown', 'parent', 'sibling'
426427
// Example: { index: 0, sibling: 1, parent: 1, external: 1, builtin: 2, internal: 2 }
427428
// Will throw an error if it contains a type that does not exist, or has a duplicate
428429
function convertGroupsToRanks(groups) {
430+
if (groups.length === 1) {
431+
// TODO: remove this `if` and fix the bug
432+
return convertGroupsToRanks(groups[0]);
433+
}
429434
const rankObject = groups.reduce(function (res, group, index) {
430-
if (typeof group === 'string') {
431-
group = [group];
432-
}
433-
group.forEach(function (groupItem) {
435+
[].concat(group).forEach(function (groupItem) {
434436
if (types.indexOf(groupItem) === -1) {
435437
throw new Error(`Incorrect configuration of the rule: Unknown type \`${JSON.stringify(groupItem)}\``);
436438
}
@@ -443,7 +445,7 @@ function convertGroupsToRanks(groups) {
443445
}, {});
444446

445447
const omittedTypes = types.filter(function (type) {
446-
return rankObject[type] === undefined;
448+
return typeof rankObject[type] === 'undefined';
447449
});
448450

449451
const ranks = omittedTypes.reduce(function (res, type) {

tests/src/core/importType.js

+4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ describe('importType(name)', function () {
1717

1818
it("should return 'builtin' for node.js modules", function () {
1919
expect(importType('fs', context)).to.equal('builtin');
20+
expect(importType('node:fs', context)).to.equal('builtin');
21+
expect(importType('fs/promises', context)).to.equal('builtin');
22+
expect(importType('node:fs/promises', context)).to.equal('builtin');
2023
expect(importType('path', context)).to.equal('builtin');
24+
expect(importType('node:path', context)).to.equal('builtin');
2125
});
2226

2327
it("should return 'external' for non-builtin modules without a relative path", function () {

tests/src/rules/order.js

+68-5
Original file line numberDiff line numberDiff line change
@@ -3174,18 +3174,81 @@ context('TypeScript', function () {
31743174
import type { ParsedPath } from 'path';
31753175
}
31763176
`,
3177-
errors: [{
3178-
message: '`fs` type import should occur before type import of `path`',
3179-
}, {
3180-
message: '`fs` type import should occur before type import of `path`',
3181-
}],
3177+
errors: [
3178+
{ message: '`fs` type import should occur before type import of `path`' },
3179+
{ message: '`fs` type import should occur before type import of `path`' },
3180+
],
31823181
...parserConfig,
31833182
options: [
31843183
{
31853184
alphabetize: { order: 'asc' },
31863185
},
31873186
],
31883187
}),
3188+
3189+
test({
3190+
code: `
3191+
import express from 'express';
3192+
import log4js from 'log4js';
3193+
import chpro from 'node:child_process';
3194+
// import fsp from 'node:fs/promises';
3195+
`,
3196+
output: `
3197+
import chpro from 'node:child_process';
3198+
import express from 'express';
3199+
import log4js from 'log4js';
3200+
// import fsp from 'node:fs/promises';
3201+
`,
3202+
options: [{
3203+
groups: [
3204+
'builtin',
3205+
'external',
3206+
'internal',
3207+
'parent',
3208+
'sibling',
3209+
'index',
3210+
'object',
3211+
'type',
3212+
],
3213+
}],
3214+
errors: [
3215+
{ message: '`node:child_process` import should occur before import of `express`' },
3216+
// { message: '`node:fs/promises` import should occur before import of `express`' },
3217+
],
3218+
}),
3219+
3220+
test({
3221+
code: `
3222+
import express from 'express';
3223+
import log4js from 'log4js';
3224+
import chpro from 'node:child_process';
3225+
// import fsp from 'node:fs/promises';
3226+
`,
3227+
output: `
3228+
import chpro from 'node:child_process';
3229+
import express from 'express';
3230+
import log4js from 'log4js';
3231+
// import fsp from 'node:fs/promises';
3232+
`,
3233+
options: [{
3234+
groups: [
3235+
[
3236+
'builtin',
3237+
'external',
3238+
'internal',
3239+
'parent',
3240+
'sibling',
3241+
'index',
3242+
'object',
3243+
'type',
3244+
],
3245+
],
3246+
}],
3247+
errors: [
3248+
{ message: '`node:child_process` import should occur before import of `express`' },
3249+
// { message: '`node:fs/promises` import should occur before import of `express`' },
3250+
],
3251+
}),
31893252
],
31903253
});
31913254
});

0 commit comments

Comments
 (0)