Skip to content

Commit

Permalink
feat: add sort by value related-options
Browse files Browse the repository at this point in the history
  • Loading branch information
hugop95 authored Feb 18, 2025
1 parent 8b78710 commit 94caac8
Show file tree
Hide file tree
Showing 37 changed files with 1,075 additions and 120 deletions.
38 changes: 38 additions & 0 deletions docs/content/rules/sort-interfaces.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,36 @@ Controls whether special characters should be trimmed, removed or kept before so
- `'trim'` — Trim special characters when sorting alphabetically or naturally (e.g., “_a” and “a” are the same).
- `'remove'` — Remove special characters when sorting (e.g., “/a/b” and “ab” are the same).

### sortBy

<sub>default: `name`</sub>

Controls whether sorting should be done only using the interface's values.

- `name` — Use interface types keys.
- `value` — Use the values of properties. Non-properties will not have an enforced sort order.

Example

```ts
interface User {
createdAt: Date
lastLoginAt: Date
_id: ObjectId
groupId: ObjectId
city: string
}
```

`sortBy` option configuration:

```js
{
sortBy: 'value',
}

```

### locales

<sub>default: `'en-US'`</sub>
Expand Down Expand Up @@ -524,10 +554,12 @@ interface CustomGroupDefinition {
groupName: string
type?: 'alphabetical' | 'natural' | 'line-length' | 'unsorted'
order?: 'asc' | 'desc'
sortBy?: 'name' | 'value'
newlinesInside?: 'always' | 'never'
selector?: string
modifiers?: string[]
elementNamePattern?: string | string[] | { pattern: string; flags?: string } | { pattern: string; flags?: string }[]
elementValuePattern?: string | string[] | { pattern: string; flags?: string } | { pattern: string; flags?: string }[]
}

```
Expand All @@ -540,11 +572,13 @@ interface CustomGroupAnyOfDefinition {
groupName: string
type?: 'alphabetical' | 'natural' | 'line-length' | 'unsorted'
order?: 'asc' | 'desc'
sortBy?: 'name' | 'value'
newlinesInside?: 'always' | 'never'
anyOf: Array<{
selector?: string
modifiers?: string[]
elementNamePattern?: string | string[] | { pattern: string; flags?: string } | { pattern: string; flags?: string }[]
elementValuePattern?: string | string[] | { pattern: string; flags?: string } | { pattern: string; flags?: string }[]
}>
}
```
Expand All @@ -557,8 +591,10 @@ An interface member will match a `CustomGroupAnyOfDefinition` group if it matche
- `selector`: Filter on the `selector` of the element.
- `modifiers`: Filter on the `modifiers` of the element. (All the modifiers of the element must be present in that list)
- `elementNamePattern`: If entered, will check that the name of the element matches the pattern entered.
- `elementValuePattern`: Only for properties. If entered, will check that the value of the property matches the pattern entered.
- `type`: Overrides the sort type for that custom group. `unsorted` will not sort the group.
- `order`: Overrides the sort order for that custom group
- `sortBy`: Overrides the `sortBy` option for that custom group
- `newlinesInside`: Enforces a specific newline behavior between elements of the group.
#### Match importance
Expand Down Expand Up @@ -634,6 +670,7 @@ interface User {
fallbackSort: { type: 'unsorted' },
ignoreCase: true,
specialCharacters: 'keep',
sortBy: 'name',
ignorePattern: [],
partitionByComment: false,
partitionByNewLine: false,
Expand Down Expand Up @@ -667,6 +704,7 @@ interface User {
fallbackSort: { type: 'unsorted' },
ignoreCase: true,
specialCharacters: 'keep',
sortBy: 'name',
ignorePattern: [],
partitionByComment: false,
partitionByNewLine: false,
Expand Down
38 changes: 38 additions & 0 deletions docs/content/rules/sort-object-types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,36 @@ Controls whether special characters should be trimmed, removed or kept before so
- `'trim'` — Trim special characters when sorting alphabetically or naturally (e.g., “_a” and “a” are the same).
- `'remove'` — Remove special characters when sorting (e.g., “/a/b” and “ab” are the same).

### sortBy

<sub>default: `name`</sub>

Controls whether sorting should be done only using the object type's values.

- `name` — Use object types keys.
- `value` — Use the values of properties. Non-properties will not have an enforced sort order.

Example

```ts
type User = {
createdAt: Date
lastLoginAt: Date
_id: ObjectId
groupId: ObjectId
city: string
}
```
`sortBy` option configuration:
```js
{
sortBy: 'value',
}

```

### locales

<sub>default: `'en-US'`</sub>
Expand Down Expand Up @@ -471,10 +501,12 @@ interface CustomGroupDefinition {
groupName: string
type?: 'alphabetical' | 'natural' | 'line-length' | 'unsorted'
order?: 'asc' | 'desc'
sortBy?: 'name' | 'value'
newlinesInside?: 'always' | 'never'
selector?: string
modifiers?: string[]
elementNamePattern?: string | string[] | { pattern: string; flags?: string } | { pattern: string; flags?: string }[]
elementValuePattern?: string | string[] | { pattern: string; flags?: string } | { pattern: string; flags?: string }[]
}

```
Expand All @@ -487,11 +519,13 @@ interface CustomGroupAnyOfDefinition {
groupName: string
type?: 'alphabetical' | 'natural' | 'line-length' | 'unsorted'
order?: 'asc' | 'desc'
sortBy?: 'name' | 'value'
newlinesInside?: 'always' | 'never'
anyOf: Array<{
selector?: string
modifiers?: string[]
elementNamePattern?: string | string[] | { pattern: string; flags?: string } | { pattern: string; flags?: string }[]
elementValuePattern?: string | string[] | { pattern: string; flags?: string } | { pattern: string; flags?: string }[]
}>
}
```
Expand All @@ -504,8 +538,10 @@ An object type will match a `CustomGroupAnyOfDefinition` group if it matches all
- `selector`: Filter on the `selector` of the element.
- `modifiers`: Filter on the `modifiers` of the element. (All the modifiers of the element must be present in that list)
- `elementNamePattern`: If entered, will check that the name of the element matches the pattern entered.
- `elementValuePattern`: Only for properties. If entered, will check that the value of the property matches the pattern entered.
- `type`: Overrides the sort type for that custom group. `unsorted` will not sort the group.
- `order`: Overrides the sort order for that custom group
- `sortBy`: Overrides the `sortBy` option for that custom group
- `newlinesInside`: Enforces a specific newline behavior between elements of the group.
#### Match importance
Expand Down Expand Up @@ -600,6 +636,7 @@ This feature is only applicable when `partitionByNewLine` is false.
fallbackSort: { type: 'unsorted' },
ignoreCase: true,
specialCharacters: 'keep',
sortBy: 'name',
ignorePattern: [],
partitionByComment: false,
partitionByNewLine: false,
Expand Down Expand Up @@ -632,6 +669,7 @@ This feature is only applicable when `partitionByNewLine` is false.
fallbackSort: { type: 'unsorted' },
ignoreCase: true,
specialCharacters: 'keep',
sortBy: 'name',
ignorePattern: [],
partitionByComment: false,
partitionByNewLine: false,
Expand Down
2 changes: 0 additions & 2 deletions rules/sort-array-includes/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import type {
CommonOptions,
GroupsOptions,
RegexOption,
TypeOption,
} from '../../types/common-options'

import {
Expand All @@ -29,7 +28,6 @@ export type Options = Partial<
newlinesBetween: NewlinesBetweenOption
groups: GroupsOptions<Group>
partitionByNewLine: boolean
type: TypeOption
} & CommonOptions
>[]

Expand Down
2 changes: 0 additions & 2 deletions rules/sort-classes/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import type {
CommonOptions,
GroupsOptions,
RegexOption,
TypeOption,
} from '../../types/common-options'
import type { JoinWithDash } from '../../types/join-with-dash'

Expand Down Expand Up @@ -37,7 +36,6 @@ export type SortClassesOptions = [
newlinesBetween: NewlinesBetweenOption
groups: GroupsOptions<Group>
partitionByNewLine: boolean
type: TypeOption
} & CommonOptions
>,
]
Expand Down
2 changes: 0 additions & 2 deletions rules/sort-decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type {
PartitionByCommentOption,
CommonOptions,
GroupsOptions,
TypeOption,
} from '../types/common-options'
import type { SortingNode } from '../types/sorting-node'

Expand Down Expand Up @@ -45,7 +44,6 @@ export type Options = [
sortOnAccessors: boolean
sortOnMethods: boolean
sortOnClasses: boolean
type: TypeOption
} & CommonOptions
>,
]
Expand Down
10 changes: 6 additions & 4 deletions rules/sort-enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type MESSAGE_ID =
interface SortEnumsSortingNode
extends SortingNodeWithDependencies<TSESTree.TSEnumMember> {
numericValue: number | null
value: string | null
}

let defaultOptions: Required<Options[0]> = {
Expand Down Expand Up @@ -173,6 +174,10 @@ export default createEslintRule<Options, MESSAGE_ID>({
member.initializer,
) /* v8 ignore next - Unsure how we can reach that case */
: null,
value:
member.initializer?.type === 'Literal'
? (member.initializer.value?.toString() ?? null)
: null,
isEslintDisabled: isNodeEslintDisabled(member, eslintDisabledLines),
size: rangeToDiff(member, sourceCode),
group: getGroup(),
Expand Down Expand Up @@ -216,10 +221,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
if (isNumericEnum) {
return sortingNode.numericValue!.toString()
}
if (sortingNode.node.initializer?.type === 'Literal') {
return sortingNode.node.initializer.value?.toString() ?? ''
}
return ''
return sortingNode.value ?? ''
}
: null,
/**
Expand Down
2 changes: 0 additions & 2 deletions rules/sort-enums/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import type {
CommonOptions,
GroupsOptions,
RegexOption,
TypeOption,
} from '../../types/common-options'

import { regexJsonSchema } from '../../utils/common-json-schemas'
Expand All @@ -21,7 +20,6 @@ export type Options = Partial<
partitionByNewLine: boolean
forceNumericSort: boolean
sortByValue: boolean
type: TypeOption
} & CommonOptions
>[]

Expand Down
2 changes: 0 additions & 2 deletions rules/sort-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { TSESTree } from '@typescript-eslint/types'
import type {
PartitionByCommentOption,
CommonOptions,
TypeOption,
} from '../types/common-options'
import type { SortingNode } from '../types/sorting-node'

Expand Down Expand Up @@ -31,7 +30,6 @@ type Options = [
groupKind: 'values-first' | 'types-first' | 'mixed'
partitionByComment: PartitionByCommentOption
partitionByNewLine: boolean
type: TypeOption
} & CommonOptions
>,
]
Expand Down
2 changes: 0 additions & 2 deletions rules/sort-heritage-clauses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type {
DeprecatedCustomGroupsOption,
CommonOptions,
GroupsOptions,
TypeOption,
} from '../types/common-options'
import type { SortingNode } from '../types/sorting-node'

Expand Down Expand Up @@ -34,7 +33,6 @@ export type Options = [
{
customGroups: DeprecatedCustomGroupsOption
groups: GroupsOptions<Group>
type: TypeOption
} & CommonOptions
>,
]
Expand Down
7 changes: 6 additions & 1 deletion rules/sort-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,12 @@ export default createEslintRule<Options, MESSAGE_ID>({
return options
}
let group = options.groups[groupNumber]
return isSideEffectOnlyGroup(group) ? null : options
return {
...options,
type: isSideEffectOnlyGroup(group)
? 'unsorted'
: options.type,
}
},
isNodeIgnored: node => node.isIgnored,
ignoreEslintDisabledNodes,
Expand Down
1 change: 1 addition & 0 deletions rules/sort-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ let defaultOptions: Required<Options[0]> = {
ignoreCase: true,
customGroups: {},
locales: 'en-US',
sortBy: 'name',
alphabet: '',
order: 'asc',
groups: [],
Expand Down
3 changes: 1 addition & 2 deletions rules/sort-intersection-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import {
GROUP_ORDER_ERROR,
ORDER_ERROR,
} from '../utils/report-errors'
import { sortUnionOrIntersectionTypes } from './sort-union-types'
import { sortUnionOrIntersectionTypes, jsonSchema } from './sort-union-types'
import { createEslintRule } from '../utils/create-eslint-rule'
import { jsonSchema } from './sort-union-types'

type MESSAGE_ID =
| 'missedSpacingBetweenIntersectionTypes'
Expand Down
2 changes: 0 additions & 2 deletions rules/sort-jsx-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type {
CommonOptions,
GroupsOptions,
RegexOption,
TypeOption,
} from '../types/common-options'
import type { SortingNode } from '../types/sorting-node'

Expand Down Expand Up @@ -57,7 +56,6 @@ type Options = Partial<
* @deprecated for {@link `useConfigurationIf.tagMatchesPattern`}
*/
ignorePattern: RegexOption
type: TypeOption
} & CommonOptions
>[]

Expand Down
2 changes: 0 additions & 2 deletions rules/sort-maps/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import type {
CommonOptions,
GroupsOptions,
RegexOption,
TypeOption,
} from '../../types/common-options'

import { regexJsonSchema } from '../../utils/common-json-schemas'
Expand All @@ -22,7 +21,6 @@ export type Options = Partial<
newlinesBetween: NewlinesBetweenOption
groups: GroupsOptions<Group>
partitionByNewLine: boolean
type: TypeOption
} & CommonOptions
>[]

Expand Down
2 changes: 0 additions & 2 deletions rules/sort-modules/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import type {
CommonOptions,
GroupsOptions,
RegexOption,
TypeOption,
} from '../../types/common-options'
import type { JoinWithDash } from '../../types/join-with-dash'

Expand Down Expand Up @@ -35,7 +34,6 @@ export type SortModulesOptions = [
newlinesBetween: NewlinesBetweenOption
groups: GroupsOptions<Group>
partitionByNewLine: boolean
type: TypeOption
} & CommonOptions
>,
]
Expand Down
Loading

0 comments on commit 94caac8

Please sign in to comment.