Skip to content

Commit

Permalink
feat: improve error messages when sorting different groups
Browse files Browse the repository at this point in the history
  • Loading branch information
hugop95 committed Sep 9, 2024
1 parent c9367eb commit e0f375a
Show file tree
Hide file tree
Showing 20 changed files with 458 additions and 137 deletions.
13 changes: 11 additions & 2 deletions rules/sort-astro-attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ type Group<T extends string[]> =
| 'unknown'
| T[number]

type MESSAGE_ID = 'unexpectedAstroAttributesOrder'
type MESSAGE_ID =
| 'unexpectedAstroAttributesGroupOrder'
| 'unexpectedAstroAttributesOrder'

type Options<T extends string[]> = [
Partial<{
Expand Down Expand Up @@ -105,6 +107,8 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
},
],
messages: {
unexpectedAstroAttributesGroupOrder:
'Expected "{{right}}" ({{rightGroup}}) to come before "{{left}}" ({{leftGroup}}).',
unexpectedAstroAttributesOrder:
'Expected "{{right}}" to come before "{{left}}".',
},
Expand Down Expand Up @@ -201,10 +205,15 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
isPositive(compare(left, right, options)))
) {
context.report({
messageId: 'unexpectedAstroAttributesOrder',
messageId:
leftNum !== rightNum
? 'unexpectedAstroAttributesGroupOrder'
: 'unexpectedAstroAttributesOrder',
data: {
left: left.name,
right: right.name,
leftGroup: left.group,
rightGroup: right.group,
},
node: right.node,
fix: fixer => {
Expand Down
10 changes: 9 additions & 1 deletion rules/sort-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { compare } from '../utils/compare'

type MESSAGE_ID =
| 'missedSpacingBetweenImports'
| 'unexpectedImportsGroupOrder'
| 'extraSpacingBetweenImports'
| 'unexpectedImportsOrder'

Expand Down Expand Up @@ -190,6 +191,8 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
},
],
messages: {
unexpectedImportsGroupOrder:
'Expected "{{right}}" ({{rightGroup}}) to come before "{{left}}" ({{leftGroup}}).',
unexpectedImportsOrder: 'Expected "{{right}}" to come before "{{left}}".',
missedSpacingBetweenImports:
'Missed spacing between "{{left}}" and "{{right}}" imports.',
Expand Down Expand Up @@ -665,10 +668,15 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
isPositive(compare(left, right, options))))
) {
context.report({
messageId: 'unexpectedImportsOrder',
messageId:
leftNum !== rightNum
? 'unexpectedImportsGroupOrder'
: 'unexpectedImportsOrder',
data: {
left: left.name,
leftGroup: left.group,
right: right.name,
rightGroup: right.group,
},
node: right.node,
fix: fixer => fix(fixer, nodeList),
Expand Down
15 changes: 13 additions & 2 deletions rules/sort-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import { complete } from '../utils/complete'
import { pairwise } from '../utils/pairwise'
import { compare } from '../utils/compare'

type MESSAGE_ID = 'unexpectedInterfacePropertiesOrder'
type MESSAGE_ID =
| 'unexpectedInterfacePropertiesGroupOrder'
| 'unexpectedInterfacePropertiesOrder'

type Group<T extends string[]> = 'multiline' | 'unknown' | T[number]

Expand Down Expand Up @@ -121,6 +123,8 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
},
],
messages: {
unexpectedInterfacePropertiesGroupOrder:
'Expected "{{right}}" ({{rightGroup}}) to come before "{{left}}" ({{leftGroup}}).',
unexpectedInterfacePropertiesOrder:
'Expected "{{right}}" to come before "{{left}}".',
},
Expand Down Expand Up @@ -310,11 +314,18 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
for (let nodes of formattedMembers) {
pairwise(nodes, (left, right, iteration) => {
if (checkOrder(nodes, left, right, iteration)) {
let leftNum = getGroupNumber(options.groups, left)
let rightNum = getGroupNumber(options.groups, right)
context.report({
messageId: 'unexpectedInterfacePropertiesOrder',
messageId:
leftNum !== rightNum
? 'unexpectedInterfacePropertiesGroupOrder'
: 'unexpectedInterfacePropertiesOrder',
data: {
left: toSingleLine(left.name),
leftGroup: left.group,
right: toSingleLine(right.name),
rightGroup: right.group,
},
node: right.node,
fix: fixer => {
Expand Down
13 changes: 11 additions & 2 deletions rules/sort-intersection-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import { complete } from '../utils/complete'
import { pairwise } from '../utils/pairwise'
import { compare } from '../utils/compare'

type MESSAGE_ID = 'unexpectedIntersectionTypesOrder'
type MESSAGE_ID =
| 'unexpectedIntersectionTypesGroupOrder'
| 'unexpectedIntersectionTypesOrder'

type Group =
| 'intersection'
Expand Down Expand Up @@ -91,6 +93,8 @@ export default createEslintRule<Options, MESSAGE_ID>({
},
],
messages: {
unexpectedIntersectionTypesGroupOrder:
'Expected "{{right}}" ({{rightGroup}}) to come before "{{left}}" ({{leftGroup}}).',
unexpectedIntersectionTypesOrder:
'Expected "{{right}}" to come before "{{left}}".',
},
Expand Down Expand Up @@ -215,10 +219,15 @@ export default createEslintRule<Options, MESSAGE_ID>({
(leftNum === rightNum && isPositive(compare(left, right, options)))
) {
context.report({
messageId: 'unexpectedIntersectionTypesOrder',
messageId:
leftNum !== rightNum
? 'unexpectedIntersectionTypesGroupOrder'
: 'unexpectedIntersectionTypesOrder',
data: {
left: toSingleLine(left.name),
leftGroup: left.group,
right: toSingleLine(right.name),
rightGroup: right.group,
},
node: right.node,
fix: fixer => {
Expand Down
11 changes: 9 additions & 2 deletions rules/sort-jsx-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { pairwise } from '../utils/pairwise'
import { complete } from '../utils/complete'
import { compare } from '../utils/compare'

type MESSAGE_ID = 'unexpectedJSXPropsOrder'
type MESSAGE_ID = 'unexpectedJSXPropsGroupOrder' | 'unexpectedJSXPropsOrder'

type Group<T extends string[]> =
| 'multiline'
Expand Down Expand Up @@ -112,6 +112,8 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
},
],
messages: {
unexpectedJSXPropsGroupOrder:
'Expected "{{right}}" ({{rightGroup}}) to come before "{{left}}" ({{leftGroup}}).',
unexpectedJSXPropsOrder:
'Expected "{{right}}" to come before "{{left}}".',
},
Expand Down Expand Up @@ -211,10 +213,15 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
isPositive(compare(left, right, options)))
) {
context.report({
messageId: 'unexpectedJSXPropsOrder',
messageId:
leftNum !== rightNum
? 'unexpectedJSXPropsGroupOrder'
: 'unexpectedJSXPropsOrder',
data: {
left: left.name,
leftGroup: left.group,
right: right.name,
rightGroup: right.group,
},
node: right.node,
fix: fixer => {
Expand Down
13 changes: 11 additions & 2 deletions rules/sort-object-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import { complete } from '../utils/complete'
import { pairwise } from '../utils/pairwise'
import { compare } from '../utils/compare'

type MESSAGE_ID = 'unexpectedObjectTypesOrder'
type MESSAGE_ID =
| 'unexpectedObjectTypesGroupOrder'
| 'unexpectedObjectTypesOrder'

type Group<T extends string[]> = 'multiline' | 'unknown' | T[number]

Expand Down Expand Up @@ -111,6 +113,8 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
},
],
messages: {
unexpectedObjectTypesGroupOrder:
'Expected "{{right}}" ({{rightGroup}}) to come before "{{left}}" ({{leftGroup}}).',
unexpectedObjectTypesOrder:
'Expected "{{right}}" to come before "{{left}}".',
},
Expand Down Expand Up @@ -277,10 +281,15 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({

if (compareValue) {
context.report({
messageId: 'unexpectedObjectTypesOrder',
messageId:
leftNum !== rightNum
? 'unexpectedObjectTypesGroupOrder'
: 'unexpectedObjectTypesOrder',
data: {
left: toSingleLine(left.name),
leftGroup: left.group,
right: toSingleLine(right.name),
rightGroup: right.group,
},
node: right.node,
fix: fixer => {
Expand Down
15 changes: 12 additions & 3 deletions rules/sort-objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ import { sortNodes } from '../utils/sort-nodes'
import { complete } from '../utils/complete'
import { pairwise } from '../utils/pairwise'

type MESSAGE_ID = 'unexpectedObjectsOrder'
type MESSAGE_ID = 'unexpectedObjectsGroupOrder' | 'unexpectedObjectsOrder'

export enum Position {
'exception' = 'exception',
'ignore' = 'ignore',
}

type Group = 'unknown' | string
type SortingNodeWithPosition = SortingNodeWithDependencies & {
position: Position
Expand Down Expand Up @@ -155,6 +156,8 @@ export default createEslintRule<Options, MESSAGE_ID>({
},
],
messages: {
unexpectedObjectsGroupOrder:
'Expected "{{right}}" ({{rightGroup}}) to come before "{{left}}" ({{leftGroup}}).',
unexpectedObjectsOrder: 'Expected "{{right}}" to come before "{{left}}".',
},
},
Expand Down Expand Up @@ -435,12 +438,18 @@ export default createEslintRule<Options, MESSAGE_ID>({
makeFixes(fixer, nodes, sortedNodes, sourceCode, {
partitionComment: options.partitionByComment,
})

let leftNum = getGroupNumber(options.groups, left)
let rightNum = getGroupNumber(options.groups, right)
context.report({
messageId: 'unexpectedObjectsOrder',
messageId:
leftNum !== rightNum
? 'unexpectedObjectsGroupOrder'
: 'unexpectedObjectsOrder',
data: {
left: toSingleLine(left.name),
leftGroup: left.group,
right: toSingleLine(right.name),
rightGroup: right.group,
},
node: right.node,
fix,
Expand Down
13 changes: 11 additions & 2 deletions rules/sort-svelte-attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import { complete } from '../utils/complete'
import { pairwise } from '../utils/pairwise'
import { compare } from '../utils/compare'

type MESSAGE_ID = 'unexpectedSvelteAttributesOrder'
type MESSAGE_ID =
| 'unexpectedSvelteAttributesGroupOrder'
| 'unexpectedSvelteAttributesOrder'

type Group<T extends string[]> =
| 'svelte-shorthand'
Expand Down Expand Up @@ -105,6 +107,8 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
},
],
messages: {
unexpectedSvelteAttributesGroupOrder:
'Expected "{{right}}" ({{rightGroup}}) to come before "{{left}}" ({{leftGroup}}).',
unexpectedSvelteAttributesOrder:
'Expected "{{right}}" to come before "{{left}}".',
},
Expand Down Expand Up @@ -208,10 +212,15 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
isPositive(compare(left, right, options)))
) {
context.report({
messageId: 'unexpectedSvelteAttributesOrder',
messageId:
leftNum !== rightNum
? 'unexpectedSvelteAttributesGroupOrder'
: 'unexpectedSvelteAttributesOrder',
data: {
left: left.name,
leftGroup: left.group,
right: right.name,
rightGroup: right.group,
},
node: right.node,
fix: fixer => {
Expand Down
11 changes: 9 additions & 2 deletions rules/sort-union-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { complete } from '../utils/complete'
import { pairwise } from '../utils/pairwise'
import { compare } from '../utils/compare'

type MESSAGE_ID = 'unexpectedUnionTypesOrder'
type MESSAGE_ID = 'unexpectedUnionTypesGroupOrder' | 'unexpectedUnionTypesOrder'

type Group =
| 'intersection'
Expand Down Expand Up @@ -91,6 +91,8 @@ export default createEslintRule<Options, MESSAGE_ID>({
},
],
messages: {
unexpectedUnionTypesGroupOrder:
'Expected "{{right}}" ({{rightGroup}}) to come before "{{left}}" ({{leftGroup}}).',
unexpectedUnionTypesOrder:
'Expected "{{right}}" to come before "{{left}}".',
},
Expand Down Expand Up @@ -215,10 +217,15 @@ export default createEslintRule<Options, MESSAGE_ID>({
(leftNum === rightNum && isPositive(compare(left, right, options)))
) {
context.report({
messageId: 'unexpectedUnionTypesOrder',
messageId:
leftNum !== rightNum
? 'unexpectedUnionTypesGroupOrder'
: 'unexpectedUnionTypesOrder',
data: {
left: toSingleLine(left.name),
leftGroup: left.group,
right: toSingleLine(right.name),
rightGroup: right.group,
},
node: right.node,
fix: fixer => {
Expand Down
13 changes: 11 additions & 2 deletions rules/sort-vue-attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import { complete } from '../utils/complete'
import { pairwise } from '../utils/pairwise'
import { compare } from '../utils/compare'

type MESSAGE_ID = 'unexpectedVueAttributesOrder'
type MESSAGE_ID =
| 'unexpectedVueAttributesGroupOrder'
| 'unexpectedVueAttributesOrder'

type Group<T extends string[]> =
| 'multiline'
Expand Down Expand Up @@ -103,6 +105,8 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
},
],
messages: {
unexpectedVueAttributesGroupOrder:
'Expected "{{right}}" ({{rightGroup}}) to come before "{{left}}" ({{leftGroup}}).',
unexpectedVueAttributesOrder:
'Expected "{{right}}" to come before "{{left}}".',
},
Expand Down Expand Up @@ -212,10 +216,15 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
isPositive(compare(left, right, options)))
) {
context.report({
messageId: 'unexpectedVueAttributesOrder',
messageId:
leftNum !== rightNum
? 'unexpectedVueAttributesGroupOrder'
: 'unexpectedVueAttributesOrder',
data: {
left: left.name,
leftGroup: left.group,
right: right.name,
rightGroup: right.group,
},
// @ts-ignore
node: right.node,
Expand Down
Loading

0 comments on commit e0f375a

Please sign in to comment.