From 125e1cb49b56553636c34c81bb54feb5f5e3f55a Mon Sep 17 00:00:00 2001 From: Hugo <60015232+hugop95@users.noreply.github.com> Date: Thu, 19 Sep 2024 13:15:27 +0200 Subject: [PATCH] feat: add dependency order errors --- rules/sort-classes.ts | 33 +++++++-- rules/sort-enums.ts | 17 ++++- rules/sort-objects.ts | 29 ++++++-- rules/sort-variable-declarations.ts | 18 ++++- test/sort-classes.test.ts | 71 +++++++++++++++----- test/sort-enums.test.ts | 8 +-- test/sort-objects.test.ts | 64 +++++++++--------- test/sort-variable-declarations.test.ts | 89 ++++++++++++++++++------- utils/sort-nodes-by-dependencies.ts | 23 +++++++ 9 files changed, 256 insertions(+), 96 deletions(-) diff --git a/rules/sort-classes.ts b/rules/sort-classes.ts index 4c02740f..1f3ae47a 100644 --- a/rules/sort-classes.ts +++ b/rules/sort-classes.ts @@ -19,7 +19,10 @@ import { customGroupNameJsonSchema, customGroupSortJsonSchema, } from './sort-classes.types' -import { sortNodesByDependencies } from '../utils/sort-nodes-by-dependencies' +import { + getFirstUnorderedNodeDependentOn, + sortNodesByDependencies, +} from '../utils/sort-nodes-by-dependencies' import { hasPartitionComment } from '../utils/is-partition-comment' import { getCommentsBefore } from '../utils/get-comments-before' import { createEslintRule } from '../utils/create-eslint-rule' @@ -34,7 +37,10 @@ import { makeFixes } from '../utils/make-fixes' import { complete } from '../utils/complete' import { pairwise } from '../utils/pairwise' -type MESSAGE_ID = 'unexpectedClassesGroupOrder' | 'unexpectedClassesOrder' +type MESSAGE_ID = + | 'unexpectedClassesDependencyOrder' + | 'unexpectedClassesGroupOrder' + | 'unexpectedClassesOrder' export default createEslintRule({ name: 'sort-classes', @@ -166,6 +172,8 @@ export default createEslintRule({ unexpectedClassesGroupOrder: 'Expected "{{right}}" ({{rightGroup}}) to come before "{{left}}" ({{leftGroup}}).', unexpectedClassesOrder: 'Expected "{{right}}" to come before "{{left}}".', + unexpectedClassesDependencyOrder: + 'Expected dependency "{{right}}" to come before "{{nodeDependentOnRight}}".', }, }, defaultOptions: [ @@ -680,17 +688,30 @@ export default createEslintRule({ let indexOfLeft = sortedNodes.indexOf(left) let indexOfRight = sortedNodes.indexOf(right) - if (!isLeftOrRightIgnored && indexOfLeft > indexOfRight) { - context.report({ - messageId: + let firstUnorderedNodeDependentOnRight = + getFirstUnorderedNodeDependentOn(right, nodes) + if ( + firstUnorderedNodeDependentOnRight || + (!isLeftOrRightIgnored && indexOfLeft > indexOfRight) + ) { + let messageId: MESSAGE_ID + if (firstUnorderedNodeDependentOnRight) { + messageId = 'unexpectedClassesDependencyOrder' + } else { + messageId = leftNum !== rightNum ? 'unexpectedClassesGroupOrder' - : 'unexpectedClassesOrder', + : 'unexpectedClassesOrder' + } + context.report({ + messageId, data: { left: toSingleLine(left.name), leftGroup: left.group, right: toSingleLine(right.name), rightGroup: right.group, + nodeDependentOnRight: + firstUnorderedNodeDependentOnRight?.name, }, node: right.node, fix: (fixer: TSESLint.RuleFixer) => diff --git a/rules/sort-enums.ts b/rules/sort-enums.ts index 5580301e..0e1c2bcd 100644 --- a/rules/sort-enums.ts +++ b/rules/sort-enums.ts @@ -3,7 +3,10 @@ import type { TSESTree } from '@typescript-eslint/types' import type { SortingNodeWithDependencies } from '../utils/sort-nodes-by-dependencies' import type { CompareOptions } from '../utils/compare' -import { sortNodesByDependencies } from '../utils/sort-nodes-by-dependencies' +import { + getFirstUnorderedNodeDependentOn, + sortNodesByDependencies, +} from '../utils/sort-nodes-by-dependencies' import { hasPartitionComment } from '../utils/is-partition-comment' import { getCommentsBefore } from '../utils/get-comments-before' import { createEslintRule } from '../utils/create-eslint-rule' @@ -16,7 +19,7 @@ import { makeFixes } from '../utils/make-fixes' import { complete } from '../utils/complete' import { pairwise } from '../utils/pairwise' -type MESSAGE_ID = 'unexpectedEnumsOrder' +type MESSAGE_ID = 'unexpectedEnumsDependencyOrder' | 'unexpectedEnumsOrder' export type Options = [ Partial<{ @@ -90,6 +93,8 @@ export default createEslintRule({ ], messages: { unexpectedEnumsOrder: 'Expected "{{right}}" to come before "{{left}}".', + unexpectedEnumsDependencyOrder: + 'Expected dependency "{{right}}" to come before "{{nodeDependentOnRight}}".', }, }, defaultOptions: [ @@ -245,11 +250,17 @@ export default createEslintRule({ let indexOfLeft = sortedNodes.indexOf(left) let indexOfRight = sortedNodes.indexOf(right) if (indexOfLeft > indexOfRight) { + let firstUnorderedNodeDependentOnRight = + getFirstUnorderedNodeDependentOn(right, nodes) context.report({ - messageId: 'unexpectedEnumsOrder', + messageId: firstUnorderedNodeDependentOnRight + ? 'unexpectedEnumsDependencyOrder' + : 'unexpectedEnumsOrder', data: { left: toSingleLine(left.name), right: toSingleLine(right.name), + nodeDependentOnRight: + firstUnorderedNodeDependentOnRight?.name, }, node: right.node, fix: fixer => diff --git a/rules/sort-objects.ts b/rules/sort-objects.ts index dad08356..3240ed4c 100644 --- a/rules/sort-objects.ts +++ b/rules/sort-objects.ts @@ -5,8 +5,11 @@ import { minimatch } from 'minimatch' import type { SortingNodeWithDependencies } from '../utils/sort-nodes-by-dependencies' +import { + getFirstUnorderedNodeDependentOn, + sortNodesByDependencies, +} from '../utils/sort-nodes-by-dependencies' import { validateGroupsConfiguration } from '../utils/validate-groups-configuration' -import { sortNodesByDependencies } from '../utils/sort-nodes-by-dependencies' import { hasPartitionComment } from '../utils/is-partition-comment' import { getCommentsBefore } from '../utils/get-comments-before' import { createEslintRule } from '../utils/create-eslint-rule' @@ -23,7 +26,10 @@ import { sortNodes } from '../utils/sort-nodes' import { complete } from '../utils/complete' import { pairwise } from '../utils/pairwise' -type MESSAGE_ID = 'unexpectedObjectsGroupOrder' | 'unexpectedObjectsOrder' +type MESSAGE_ID = + | 'unexpectedObjectsDependencyOrder' + | 'unexpectedObjectsGroupOrder' + | 'unexpectedObjectsOrder' export enum Position { 'exception' = 'exception', @@ -159,6 +165,8 @@ export default createEslintRule({ unexpectedObjectsGroupOrder: 'Expected "{{right}}" ({{rightGroup}}) to come before "{{left}}" ({{leftGroup}}).', unexpectedObjectsOrder: 'Expected "{{right}}" to come before "{{left}}".', + unexpectedObjectsDependencyOrder: + 'Expected dependency "{{right}}" to come before "{{nodeDependentOnRight}}".', }, }, defaultOptions: [ @@ -455,7 +463,10 @@ export default createEslintRule({ pairwise(nodes, (left, right) => { let indexOfLeft = sortedNodes.indexOf(left) let indexOfRight = sortedNodes.indexOf(right) + if (indexOfLeft > indexOfRight) { + let firstUnorderedNodeDependentOnRight = + getFirstUnorderedNodeDependentOn(right, nodes) let fix: | ((fixer: TSESLint.RuleFixer) => TSESLint.RuleFix[]) | undefined = fixer => @@ -464,16 +475,24 @@ export default createEslintRule({ }) let leftNum = getGroupNumber(options.groups, left) let rightNum = getGroupNumber(options.groups, right) - context.report({ - messageId: + let messageId: MESSAGE_ID + if (firstUnorderedNodeDependentOnRight) { + messageId = 'unexpectedObjectsDependencyOrder' + } else { + messageId = leftNum !== rightNum ? 'unexpectedObjectsGroupOrder' - : 'unexpectedObjectsOrder', + : 'unexpectedObjectsOrder' + } + context.report({ + messageId, data: { left: toSingleLine(left.name), leftGroup: left.group, right: toSingleLine(right.name), rightGroup: right.group, + nodeDependentOnRight: + firstUnorderedNodeDependentOnRight?.name, }, node: right.node, fix, diff --git a/rules/sort-variable-declarations.ts b/rules/sort-variable-declarations.ts index 067e668e..da6597e6 100644 --- a/rules/sort-variable-declarations.ts +++ b/rules/sort-variable-declarations.ts @@ -2,7 +2,10 @@ import type { TSESTree } from '@typescript-eslint/types' import type { SortingNodeWithDependencies } from '../utils/sort-nodes-by-dependencies' -import { sortNodesByDependencies } from '../utils/sort-nodes-by-dependencies' +import { + getFirstUnorderedNodeDependentOn, + sortNodesByDependencies, +} from '../utils/sort-nodes-by-dependencies' import { createEslintRule } from '../utils/create-eslint-rule' import { getSourceCode } from '../utils/get-source-code' import { toSingleLine } from '../utils/to-single-line' @@ -13,7 +16,9 @@ import { makeFixes } from '../utils/make-fixes' import { complete } from '../utils/complete' import { pairwise } from '../utils/pairwise' -type MESSAGE_ID = 'unexpectedVariableDeclarationsOrder' +type MESSAGE_ID = + | 'unexpectedVariableDeclarationsDependencyOrder' + | 'unexpectedVariableDeclarationsOrder' type Options = [ Partial<{ @@ -58,6 +63,8 @@ export default createEslintRule({ messages: { unexpectedVariableDeclarationsOrder: 'Expected "{{right}}" to come before "{{left}}".', + unexpectedVariableDeclarationsDependencyOrder: + 'Expected dependency "{{right}}" to come before "{{nodeDependentOnRight}}".', }, }, defaultOptions: [ @@ -195,11 +202,16 @@ export default createEslintRule({ let indexOfLeft = sortedNodes.indexOf(left) let indexOfRight = sortedNodes.indexOf(right) if (indexOfLeft > indexOfRight) { + let firstUnorderedNodeDependentOnRight = + getFirstUnorderedNodeDependentOn(right, nodes) context.report({ - messageId: 'unexpectedVariableDeclarationsOrder', + messageId: firstUnorderedNodeDependentOnRight + ? 'unexpectedVariableDeclarationsDependencyOrder' + : 'unexpectedVariableDeclarationsOrder', data: { left: toSingleLine(left.name), right: toSingleLine(right.name), + nodeDependentOnRight: firstUnorderedNodeDependentOnRight?.name, }, node: right.node, fix: fixer => makeFixes(fixer, nodes, sortedNodes, sourceCode), diff --git a/test/sort-classes.test.ts b/test/sort-classes.test.ts index 90250539..38cfdd26 100644 --- a/test/sort-classes.test.ts +++ b/test/sort-classes.test.ts @@ -2453,10 +2453,10 @@ describe(ruleName, () => { options: [options], errors: [ { - messageId: 'unexpectedClassesOrder', + messageId: 'unexpectedClassesDependencyOrder', data: { - left: 'aaa', right: 'b', + nodeDependentOnRight: 'aaa', }, }, ], @@ -2934,10 +2934,24 @@ describe(ruleName, () => { }, }, { - messageId: 'unexpectedClassesOrder', + messageId: 'unexpectedClassesDependencyOrder', + data: { + right: 'b', + nodeDependentOnRight: 'd', + }, + }, + { + messageId: 'unexpectedClassesDependencyOrder', + data: { + right: 'c', + nodeDependentOnRight: 'e', + }, + }, + { + messageId: 'unexpectedClassesDependencyOrder', data: { - left: 'c', right: 'z', + nodeDependentOnRight: 'b', }, }, ], @@ -2964,10 +2978,10 @@ describe(ruleName, () => { ], errors: [ { - messageId: 'unexpectedClassesOrder', + messageId: 'unexpectedClassesDependencyOrder', data: { - left: 'b', right: 'c', + nodeDependentOnRight: 'a', }, }, ], @@ -3631,17 +3645,17 @@ describe(ruleName, () => { ], errors: [ { - messageId: 'unexpectedClassesOrder', + messageId: 'unexpectedClassesDependencyOrder', data: { - left: 'b', right: 'c', + nodeDependentOnRight: 'b', }, }, { - messageId: 'unexpectedClassesOrder', + messageId: 'unexpectedClassesDependencyOrder', data: { - left: 'c', right: 'c', + nodeDependentOnRight: 'a', }, }, ], @@ -3690,10 +3704,17 @@ describe(ruleName, () => { }, }, { - messageId: 'unexpectedClassesOrder', + messageId: 'unexpectedClassesDependencyOrder', + data: { + right: 'e', + nodeDependentOnRight: 'b', + }, + }, + { + messageId: 'unexpectedClassesDependencyOrder', data: { - left: 'f', right: 'g', + nodeDependentOnRight: 'e', }, }, ], @@ -3797,10 +3818,17 @@ describe(ruleName, () => { options: [options], errors: [ { - messageId: 'unexpectedClassesOrder', + messageId: 'unexpectedClassesDependencyOrder', data: { - left: 'aaa', right: 'left', + nodeDependentOnRight: 'aaa', + }, + }, + { + messageId: 'unexpectedClassesDependencyOrder', + data: { + right: 'right', + nodeDependentOnRight: 'aaa', }, }, ], @@ -4827,10 +4855,10 @@ describe(ruleName, () => { options: [options], errors: [ { - messageId: 'unexpectedClassesOrder', + messageId: 'unexpectedClassesDependencyOrder', data: { - left: 'aaa', right: 'b', + nodeDependentOnRight: 'aaa', }, }, ], @@ -5003,10 +5031,17 @@ describe(ruleName, () => { options: [options], errors: [ { - messageId: 'unexpectedClassesOrder', + messageId: 'unexpectedClassesDependencyOrder', data: { - left: 'aaa', right: 'left', + nodeDependentOnRight: 'aaa', + }, + }, + { + messageId: 'unexpectedClassesDependencyOrder', + data: { + right: 'right', + nodeDependentOnRight: 'aaa', }, }, ], diff --git a/test/sort-enums.test.ts b/test/sort-enums.test.ts index 4adb4049..e0fca690 100644 --- a/test/sort-enums.test.ts +++ b/test/sort-enums.test.ts @@ -1850,10 +1850,10 @@ describe(ruleName, () => { ], errors: [ { - messageId: 'unexpectedEnumsOrder', + messageId: 'unexpectedEnumsDependencyOrder', data: { - left: 'B', right: 'C', + nodeDependentOnRight: 'A', }, }, ], @@ -1924,10 +1924,10 @@ describe(ruleName, () => { }, }, { - messageId: 'unexpectedEnumsOrder', + messageId: 'unexpectedEnumsDependencyOrder', data: { - left: 'E', right: 'F', + nodeDependentOnRight: 'B', }, }, ], diff --git a/test/sort-objects.test.ts b/test/sort-objects.test.ts index 8d45e3ca..45cb5740 100644 --- a/test/sort-objects.test.ts +++ b/test/sort-objects.test.ts @@ -511,10 +511,10 @@ describe(ruleName, () => { options: [options], errors: [ { - messageId: 'unexpectedObjectsOrder', + messageId: 'unexpectedObjectsDependencyOrder', data: { - left: 'b', right: 'c', + nodeDependentOnRight: 'b', }, }, ], @@ -543,10 +543,10 @@ describe(ruleName, () => { options: [options], errors: [ { - messageId: 'unexpectedObjectsOrder', + messageId: 'unexpectedObjectsDependencyOrder', data: { - left: 'c', right: 'b', + nodeDependentOnRight: 'c', }, }, ], @@ -580,10 +580,10 @@ describe(ruleName, () => { ], errors: [ { - messageId: 'unexpectedObjectsOrder', + messageId: 'unexpectedObjectsDependencyOrder', data: { - left: 'b', right: 'c', + nodeDependentOnRight: 'b', }, }, ], @@ -612,10 +612,10 @@ describe(ruleName, () => { options: [options], errors: [ { - messageId: 'unexpectedObjectsOrder', + messageId: 'unexpectedObjectsDependencyOrder', data: { - left: 'b', right: 'c', + nodeDependentOnRight: 'b', }, }, ], @@ -644,10 +644,10 @@ describe(ruleName, () => { options: [options], errors: [ { - messageId: 'unexpectedObjectsOrder', + messageId: 'unexpectedObjectsDependencyOrder', data: { - left: 'b', right: 'c', + nodeDependentOnRight: 'b', }, }, ], @@ -674,10 +674,10 @@ describe(ruleName, () => { options: [options], errors: [ { - messageId: 'unexpectedObjectsOrder', + messageId: 'unexpectedObjectsDependencyOrder', data: { - left: 'b', right: 'c', + nodeDependentOnRight: 'a', }, }, ], @@ -1158,10 +1158,10 @@ describe(ruleName, () => { }, }, { - messageId: 'unexpectedObjectsOrder', + messageId: 'unexpectedObjectsDependencyOrder', data: { - left: 'e', right: 'f', + nodeDependentOnRight: 'b', }, }, ], @@ -1916,10 +1916,10 @@ describe(ruleName, () => { options: [options], errors: [ { - messageId: 'unexpectedObjectsOrder', + messageId: 'unexpectedObjectsDependencyOrder', data: { - left: 'b', right: 'c', + nodeDependentOnRight: 'b', }, }, ], @@ -1948,10 +1948,10 @@ describe(ruleName, () => { options: [options], errors: [ { - messageId: 'unexpectedObjectsOrder', + messageId: 'unexpectedObjectsDependencyOrder', data: { - left: 'c', right: 'b', + nodeDependentOnRight: 'c', }, }, ], @@ -1985,10 +1985,10 @@ describe(ruleName, () => { ], errors: [ { - messageId: 'unexpectedObjectsOrder', + messageId: 'unexpectedObjectsDependencyOrder', data: { - left: 'b', right: 'c', + nodeDependentOnRight: 'b', }, }, ], @@ -2017,10 +2017,10 @@ describe(ruleName, () => { options: [options], errors: [ { - messageId: 'unexpectedObjectsOrder', + messageId: 'unexpectedObjectsDependencyOrder', data: { - left: 'b', right: 'c', + nodeDependentOnRight: 'b', }, }, ], @@ -2049,10 +2049,10 @@ describe(ruleName, () => { options: [options], errors: [ { - messageId: 'unexpectedObjectsOrder', + messageId: 'unexpectedObjectsDependencyOrder', data: { - left: 'b', right: 'c', + nodeDependentOnRight: 'b', }, }, ], @@ -2751,10 +2751,10 @@ describe(ruleName, () => { options: [options], errors: [ { - messageId: 'unexpectedObjectsOrder', + messageId: 'unexpectedObjectsDependencyOrder', data: { - left: 'b', right: 'c', + nodeDependentOnRight: 'b', }, }, ], @@ -2783,10 +2783,10 @@ describe(ruleName, () => { options: [options], errors: [ { - messageId: 'unexpectedObjectsOrder', + messageId: 'unexpectedObjectsDependencyOrder', data: { - left: 'c', right: 'b', + nodeDependentOnRight: 'c', }, }, ], @@ -2820,10 +2820,10 @@ describe(ruleName, () => { ], errors: [ { - messageId: 'unexpectedObjectsOrder', + messageId: 'unexpectedObjectsDependencyOrder', data: { - left: 'b', right: 'c', + nodeDependentOnRight: 'b', }, }, ], @@ -2852,10 +2852,10 @@ describe(ruleName, () => { options: [options], errors: [ { - messageId: 'unexpectedObjectsOrder', + messageId: 'unexpectedObjectsDependencyOrder', data: { - left: 'b', right: 'c', + nodeDependentOnRight: 'b', }, }, ], diff --git a/test/sort-variable-declarations.test.ts b/test/sort-variable-declarations.test.ts index 581ef159..4861bfe3 100644 --- a/test/sort-variable-declarations.test.ts +++ b/test/sort-variable-declarations.test.ts @@ -184,8 +184,11 @@ describe(ruleName, () => { options: [options], errors: [ { - messageId: 'unexpectedVariableDeclarationsOrder', - data: { left: 'aaa', right: 'bb' }, + messageId: 'unexpectedVariableDeclarationsDependencyOrder', + data: { + right: 'bb', + nodeDependentOnRight: 'aaa', + }, }, ], }, @@ -203,8 +206,11 @@ describe(ruleName, () => { options: [options], errors: [ { - messageId: 'unexpectedVariableDeclarationsOrder', - data: { left: 'b', right: 'a' }, + messageId: 'unexpectedVariableDeclarationsDependencyOrder', + data: { + right: 'a', + nodeDependentOnRight: 'b', + }, }, ], }, @@ -222,8 +228,11 @@ describe(ruleName, () => { options: [options], errors: [ { - messageId: 'unexpectedVariableDeclarationsOrder', - data: { left: 'y', right: 'x' }, + messageId: 'unexpectedVariableDeclarationsDependencyOrder', + data: { + right: 'x', + nodeDependentOnRight: 'y', + }, }, ], }, @@ -241,8 +250,11 @@ describe(ruleName, () => { options: [options], errors: [ { - messageId: 'unexpectedVariableDeclarationsOrder', - data: { left: 'sum', right: 'arr' }, + messageId: 'unexpectedVariableDeclarationsDependencyOrder', + data: { + right: 'arr', + nodeDependentOnRight: 'sum', + }, }, ], }, @@ -260,8 +272,11 @@ describe(ruleName, () => { options: [options], errors: [ { - messageId: 'unexpectedVariableDeclarationsOrder', - data: { left: 'value', right: 'getValue' }, + messageId: 'unexpectedVariableDeclarationsDependencyOrder', + data: { + right: 'getValue', + nodeDependentOnRight: 'value', + }, }, ], }, @@ -279,8 +294,11 @@ describe(ruleName, () => { options: [options], errors: [ { - messageId: 'unexpectedVariableDeclarationsOrder', - data: { left: 'b', right: 'c' }, + messageId: 'unexpectedVariableDeclarationsDependencyOrder', + data: { + right: 'c', + nodeDependentOnRight: 'a', + }, }, ], }, @@ -775,7 +793,10 @@ describe(ruleName, () => { errors: [ { messageId: 'unexpectedVariableDeclarationsOrder', - data: { left: 'b', right: 'a' }, + data: { + left: 'b', + right: 'a', + }, }, ], }, @@ -793,8 +814,11 @@ describe(ruleName, () => { options: [options], errors: [ { - messageId: 'unexpectedVariableDeclarationsOrder', - data: { left: 'aaa', right: 'bb' }, + messageId: 'unexpectedVariableDeclarationsDependencyOrder', + data: { + right: 'bb', + nodeDependentOnRight: 'aaa', + }, }, ], }, @@ -812,8 +836,11 @@ describe(ruleName, () => { options: [options], errors: [ { - messageId: 'unexpectedVariableDeclarationsOrder', - data: { left: 'b', right: 'a' }, + messageId: 'unexpectedVariableDeclarationsDependencyOrder', + data: { + right: 'a', + nodeDependentOnRight: 'b', + }, }, ], }, @@ -831,8 +858,11 @@ describe(ruleName, () => { options: [options], errors: [ { - messageId: 'unexpectedVariableDeclarationsOrder', - data: { left: 'y', right: 'x' }, + messageId: 'unexpectedVariableDeclarationsDependencyOrder', + data: { + right: 'x', + nodeDependentOnRight: 'y', + }, }, ], }, @@ -850,8 +880,11 @@ describe(ruleName, () => { options: [options], errors: [ { - messageId: 'unexpectedVariableDeclarationsOrder', - data: { left: 'sum', right: 'arr' }, + messageId: 'unexpectedVariableDeclarationsDependencyOrder', + data: { + right: 'arr', + nodeDependentOnRight: 'sum', + }, }, ], }, @@ -869,8 +902,11 @@ describe(ruleName, () => { options: [options], errors: [ { - messageId: 'unexpectedVariableDeclarationsOrder', - data: { left: 'value', right: 'getValue' }, + messageId: 'unexpectedVariableDeclarationsDependencyOrder', + data: { + right: 'getValue', + nodeDependentOnRight: 'value', + }, }, ], }, @@ -908,8 +944,11 @@ describe(ruleName, () => { data: { left: 'c', right: 'd' }, }, { - messageId: 'unexpectedVariableDeclarationsOrder', - data: { left: 'e', right: 'f' }, + messageId: 'unexpectedVariableDeclarationsDependencyOrder', + data: { + right: 'f', + nodeDependentOnRight: 'b', + }, }, ], }, diff --git a/utils/sort-nodes-by-dependencies.ts b/utils/sort-nodes-by-dependencies.ts index cccb08eb..e26d735c 100644 --- a/utils/sort-nodes-by-dependencies.ts +++ b/utils/sort-nodes-by-dependencies.ts @@ -48,3 +48,26 @@ export let sortNodesByDependencies = ( return result } + +/** + * Returns the first node that is dependent on the given node, but is not + * ordered before it + */ +export let getFirstUnorderedNodeDependentOn = ( + node: SortingNodeWithDependencies, + currentlyOrderedNodes: SortingNodeWithDependencies[], +): SortingNodeWithDependencies | undefined => { + let nodesDependentOnNode = currentlyOrderedNodes.filter( + currentlyOrderedNode => + currentlyOrderedNode.dependencies.includes( + node.dependencyName ?? node.name, + ), + ) + return nodesDependentOnNode.find(firstNodeDependentOnNode => { + let currentIndexOfNode = currentlyOrderedNodes.indexOf(node) + let currentIndexOfFirstNodeDependentOnNode = currentlyOrderedNodes.indexOf( + firstNodeDependentOnNode, + ) + return currentIndexOfFirstNodeDependentOnNode < currentIndexOfNode + }) +}