From 18edab0d96e15e89af7eb58cf2f38100265756ab Mon Sep 17 00:00:00 2001 From: Jeff Date: Wed, 18 Dec 2024 21:41:12 -0700 Subject: [PATCH] Fix grouping bug with undefined settings --- .changeset/shaggy-meals-serve.md | 5 +++++ src/utils/compute-group.ts | 6 +++--- tests/rules/order.test.ts | 15 ++++++++++++++- 3 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 .changeset/shaggy-meals-serve.md diff --git a/.changeset/shaggy-meals-serve.md b/.changeset/shaggy-meals-serve.md new file mode 100644 index 0000000..8dc1e35 --- /dev/null +++ b/.changeset/shaggy-meals-serve.md @@ -0,0 +1,5 @@ +--- +'eslint-plugin-import-sorting': patch +--- + +Fix bug with grouping when settings are not explicitly defined diff --git a/src/utils/compute-group.ts b/src/utils/compute-group.ts index 1947c6c..da13af0 100644 --- a/src/utils/compute-group.ts +++ b/src/utils/compute-group.ts @@ -32,8 +32,8 @@ export function computeGroup( if (isSideEffectImport(node, sourceCode)) defineGroup('unassigned') if (isBuiltin(value)) defineGroup('builtin') if (isStyle(value)) defineGroup('style') - if (isFramework(value, frameworkPatterns)) defineGroup('framework') - if (isInternal(value, internalPatterns)) defineGroup('internal') + if (frameworkPatterns && isFramework(value, frameworkPatterns)) defineGroup('framework') + if (internalPatterns && isInternal(value, internalPatterns)) defineGroup('internal') if (isExternal(value)) defineGroup('external') if (isLocal(value)) defineGroup('local') } @@ -122,7 +122,7 @@ function assertString(value: unknown, setting: string) { function validateSetting(settings: TSESLint.SharedConfigurationSettings, setting: string) { let value = settings[setting] as string | string[] - if (!value) return '' + if (!value) return undefined if (Array.isArray(value)) { for (let item of value) { assertString(item, setting) diff --git a/tests/rules/order.test.ts b/tests/rules/order.test.ts index b55c677..bd59544 100644 --- a/tests/rules/order.test.ts +++ b/tests/rules/order.test.ts @@ -154,19 +154,32 @@ describe('order', () => { { name: 'groups local modules together', code: dedent` + import { AST_NODE_TYPES, type TSESLint, type TSESTree } from '@typescript-eslint/utils' import prettier from 'eslint-config-prettier' import xoTypeScript from 'eslint-config-xo-typescript' import etc from 'eslint-plugin-etc' import stylistic from './stylistic.js' + import { other } from '../other.js' `, output: dedent` + import { AST_NODE_TYPES, type TSESLint, type TSESTree } from '@typescript-eslint/utils' import prettier from 'eslint-config-prettier' import xoTypeScript from 'eslint-config-xo-typescript' import etc from 'eslint-plugin-etc' + import { other } from '../other.js' import stylistic from './stylistic.js' `, - errors: [{ messageId: 'needs-newline' }], + errors: [ + { + messageId: 'needs-newline', + data: { left: 'eslint-plugin-etc', right: './stylistic.js' }, + }, + { + messageId: 'out-of-order', + data: { left: './stylistic.js', right: '../other.js' }, + }, + ], }, {