diff --git a/src/rules/order.js b/src/rules/order.js index 5921989d2..27c3f4b0f 100644 --- a/src/rules/order.js +++ b/src/rules/order.js @@ -253,6 +253,7 @@ function makeOutOfOrderReport(context, imported) { if (!outOfOrder.length) { return; } + // There are things to report. Try to minimize the number of reported errors. const reversedImported = reverse(imported); const reversedOrder = findOutOfOrder(reversedImported); @@ -426,11 +427,12 @@ const types = ['builtin', 'external', 'internal', 'unknown', 'parent', 'sibling' // Example: { index: 0, sibling: 1, parent: 1, external: 1, builtin: 2, internal: 2 } // Will throw an error if it contains a type that does not exist, or has a duplicate function convertGroupsToRanks(groups) { + if (groups.length === 1) { + // TODO: remove this `if` and fix the bug + return convertGroupsToRanks(groups[0]); + } const rankObject = groups.reduce(function (res, group, index) { - if (typeof group === 'string') { - group = [group]; - } - group.forEach(function (groupItem) { + [].concat(group).forEach(function (groupItem) { if (types.indexOf(groupItem) === -1) { throw new Error(`Incorrect configuration of the rule: Unknown type \`${JSON.stringify(groupItem)}\``); } @@ -443,7 +445,7 @@ function convertGroupsToRanks(groups) { }, {}); const omittedTypes = types.filter(function (type) { - return rankObject[type] === undefined; + return typeof rankObject[type] === 'undefined'; }); const ranks = omittedTypes.reduce(function (res, type) { diff --git a/tests/src/core/importType.js b/tests/src/core/importType.js index 937f19303..bdc93d8a3 100644 --- a/tests/src/core/importType.js +++ b/tests/src/core/importType.js @@ -17,7 +17,11 @@ describe('importType(name)', function () { it("should return 'builtin' for node.js modules", function () { expect(importType('fs', context)).to.equal('builtin'); + expect(importType('node:fs', context)).to.equal('builtin'); + expect(importType('fs/promises', context)).to.equal('builtin'); + expect(importType('node:fs/promises', context)).to.equal('builtin'); expect(importType('path', context)).to.equal('builtin'); + expect(importType('node:path', context)).to.equal('builtin'); }); it("should return 'external' for non-builtin modules without a relative path", function () { diff --git a/tests/src/rules/order.js b/tests/src/rules/order.js index 8d0b315e7..e7a9cb9c9 100644 --- a/tests/src/rules/order.js +++ b/tests/src/rules/order.js @@ -3174,11 +3174,10 @@ context('TypeScript', function () { import type { ParsedPath } from 'path'; } `, - errors: [{ - message: '`fs` type import should occur before type import of `path`', - }, { - message: '`fs` type import should occur before type import of `path`', - }], + errors: [ + { message: '`fs` type import should occur before type import of `path`' }, + { message: '`fs` type import should occur before type import of `path`' }, + ], ...parserConfig, options: [ { @@ -3186,6 +3185,70 @@ context('TypeScript', function () { }, ], }), + + test({ + code: ` + import express from 'express'; + import log4js from 'log4js'; + import chpro from 'node:child_process'; + // import fsp from 'node:fs/promises'; + `, + output: ` + import chpro from 'node:child_process'; + import express from 'express'; + import log4js from 'log4js'; + // import fsp from 'node:fs/promises'; + `, + options: [{ + groups: [ + 'builtin', + 'external', + 'internal', + 'parent', + 'sibling', + 'index', + 'object', + 'type', + ], + }], + errors: [ + { message: '`node:child_process` import should occur before import of `express`' }, + // { message: '`node:fs/promises` import should occur before import of `express`' }, + ], + }), + + test({ + code: ` + import express from 'express'; + import log4js from 'log4js'; + import chpro from 'node:child_process'; + // import fsp from 'node:fs/promises'; + `, + output: ` + import chpro from 'node:child_process'; + import express from 'express'; + import log4js from 'log4js'; + // import fsp from 'node:fs/promises'; + `, + options: [{ + groups: [ + [ + 'builtin', + 'external', + 'internal', + 'parent', + 'sibling', + 'index', + 'object', + 'type', + ], + ], + }], + errors: [ + { message: '`node:child_process` import should occur before import of `express`' }, + // { message: '`node:fs/promises` import should occur before import of `express`' }, + ], + }), ], }); });