Skip to content

Commit

Permalink
feat: add dependency order errors
Browse files Browse the repository at this point in the history
  • Loading branch information
hugop95 authored Sep 19, 2024
1 parent 4db2c5e commit 125e1cb
Show file tree
Hide file tree
Showing 9 changed files with 256 additions and 96 deletions.
33 changes: 27 additions & 6 deletions rules/sort-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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<SortClassesOptions, MESSAGE_ID>({
name: 'sort-classes',
Expand Down Expand Up @@ -166,6 +172,8 @@ export default createEslintRule<SortClassesOptions, MESSAGE_ID>({
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: [
Expand Down Expand Up @@ -680,17 +688,30 @@ export default createEslintRule<SortClassesOptions, MESSAGE_ID>({

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) =>
Expand Down
17 changes: 14 additions & 3 deletions rules/sort-enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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<{
Expand Down Expand Up @@ -90,6 +93,8 @@ export default createEslintRule<Options, MESSAGE_ID>({
],
messages: {
unexpectedEnumsOrder: 'Expected "{{right}}" to come before "{{left}}".',
unexpectedEnumsDependencyOrder:
'Expected dependency "{{right}}" to come before "{{nodeDependentOnRight}}".',
},
},
defaultOptions: [
Expand Down Expand Up @@ -245,11 +250,17 @@ export default createEslintRule<Options, MESSAGE_ID>({
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 =>
Expand Down
29 changes: 24 additions & 5 deletions rules/sort-objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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',
Expand Down Expand Up @@ -159,6 +165,8 @@ export default createEslintRule<Options, MESSAGE_ID>({
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: [
Expand Down Expand Up @@ -455,7 +463,10 @@ export default createEslintRule<Options, MESSAGE_ID>({
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 =>
Expand All @@ -464,16 +475,24 @@ export default createEslintRule<Options, MESSAGE_ID>({
})
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,
Expand Down
18 changes: 15 additions & 3 deletions rules/sort-variable-declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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<{
Expand Down Expand Up @@ -58,6 +63,8 @@ export default createEslintRule<Options, MESSAGE_ID>({
messages: {
unexpectedVariableDeclarationsOrder:
'Expected "{{right}}" to come before "{{left}}".',
unexpectedVariableDeclarationsDependencyOrder:
'Expected dependency "{{right}}" to come before "{{nodeDependentOnRight}}".',
},
},
defaultOptions: [
Expand Down Expand Up @@ -195,11 +202,16 @@ export default createEslintRule<Options, MESSAGE_ID>({
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),
Expand Down
71 changes: 53 additions & 18 deletions test/sort-classes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2453,10 +2453,10 @@ describe(ruleName, () => {
options: [options],
errors: [
{
messageId: 'unexpectedClassesOrder',
messageId: 'unexpectedClassesDependencyOrder',
data: {
left: 'aaa',
right: 'b',
nodeDependentOnRight: 'aaa',
},
},
],
Expand Down Expand Up @@ -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',
},
},
],
Expand All @@ -2964,10 +2978,10 @@ describe(ruleName, () => {
],
errors: [
{
messageId: 'unexpectedClassesOrder',
messageId: 'unexpectedClassesDependencyOrder',
data: {
left: 'b',
right: 'c',
nodeDependentOnRight: 'a',
},
},
],
Expand Down Expand Up @@ -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',
},
},
],
Expand Down Expand Up @@ -3690,10 +3704,17 @@ describe(ruleName, () => {
},
},
{
messageId: 'unexpectedClassesOrder',
messageId: 'unexpectedClassesDependencyOrder',
data: {
right: 'e',
nodeDependentOnRight: 'b',
},
},
{
messageId: 'unexpectedClassesDependencyOrder',
data: {
left: 'f',
right: 'g',
nodeDependentOnRight: 'e',
},
},
],
Expand Down Expand Up @@ -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',
},
},
],
Expand Down Expand Up @@ -4827,10 +4855,10 @@ describe(ruleName, () => {
options: [options],
errors: [
{
messageId: 'unexpectedClassesOrder',
messageId: 'unexpectedClassesDependencyOrder',
data: {
left: 'aaa',
right: 'b',
nodeDependentOnRight: 'aaa',
},
},
],
Expand Down Expand Up @@ -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',
},
},
],
Expand Down
Loading

0 comments on commit 125e1cb

Please sign in to comment.