Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: reduce JSON schema and options duplication #339

Open
wants to merge 6 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 27 additions & 62 deletions rules/sort-array-includes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ import type { TSESTree } from '@typescript-eslint/types'

import type { SortingNode } from '../typings'

import {
partitionByCommentJsonSchema,
specialCharactersJsonSchema,
ignoreCaseJsonSchema,
matcherJsonSchema,
orderJsonSchema,
typeJsonSchema,
} from '../utils/common-json-schemas'
import { hasPartitionComment } from '../utils/is-partition-comment'
import { getCommentsBefore } from '../utils/get-comments-before'
import { createEslintRule } from '../utils/create-eslint-rule'
Expand Down Expand Up @@ -35,57 +43,34 @@ export type Options = [
}>,
]

export const defaultOptions: Required<Options[0]> = {
groupKind: 'literals-first',
type: 'alphabetical',
ignoreCase: true,
specialCharacters: 'keep',
matcher: 'minimatch',
order: 'asc',
partitionByComment: false,
partitionByNewLine: false,
}

export let jsonSchema: JSONSchema4 = {
type: 'object',
properties: {
type: {
description: 'Specifies the sorting method.',
type: 'string',
enum: ['alphabetical', 'natural', 'line-length'],
},
order: {
description:
'Determines whether the sorted items should be in ascending or descending order.',
type: 'string',
enum: ['asc', 'desc'],
},
matcher: {
description: 'Specifies the string matcher.',
type: 'string',
enum: ['minimatch', 'regex'],
},
ignoreCase: {
description: 'Controls whether sorting should be case-sensitive or not.',
type: 'boolean',
},
specialCharacters: {
description:
'Controls how special characters should be handled before sorting.',
type: 'string',
enum: ['remove', 'trim', 'keep'],
},
type: typeJsonSchema,
order: orderJsonSchema,
matcher: matcherJsonSchema,
ignoreCase: ignoreCaseJsonSchema,
specialCharacters: specialCharactersJsonSchema,
groupKind: {
description: 'Specifies top-level groups.',
enum: ['mixed', 'literals-first', 'spreads-first'],
type: 'string',
},
partitionByComment: {
...partitionByCommentJsonSchema,
description:
'Allows you to use comments to separate the array members into logical groups.',
anyOf: [
{
type: 'array',
items: {
type: 'string',
},
},
{
type: 'boolean',
},
{
type: 'string',
},
],
},
partitionByNewLine: {
description:
Expand All @@ -110,18 +95,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
'Expected "{{right}}" to come before "{{left}}".',
},
},
defaultOptions: [
{
type: 'alphabetical',
order: 'asc',
ignoreCase: true,
specialCharacters: 'keep',
matcher: 'minimatch',
groupKind: 'literals-first',
partitionByComment: false,
partitionByNewLine: false,
},
],
defaultOptions: [defaultOptions],
create: context => ({
MemberExpression: node => {
if (
Expand All @@ -148,16 +122,7 @@ export let sortArray = <MessageIds extends string>(
let settings = getSettings(context.settings)

if (elements.length > 1) {
let options = complete(context.options.at(0), settings, {
groupKind: 'literals-first',
type: 'alphabetical',
ignoreCase: true,
specialCharacters: 'keep',
matcher: 'minimatch',
order: 'asc',
partitionByComment: false,
partitionByNewLine: false,
} as const)
let options = complete(context.options.at(0), settings, defaultOptions)

let sourceCode = getSourceCode(context)
let partitionComment = options.partitionByComment
Expand Down
155 changes: 51 additions & 104 deletions rules/sort-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ import type {
} from './sort-classes.types'
import type { SortingNodeWithDependencies } from '../utils/sort-nodes-by-dependencies'

import {
partitionByCommentJsonSchema,
specialCharactersJsonSchema,
ignoreCaseJsonSchema,
matcherJsonSchema,
groupsJsonSchema,
orderJsonSchema,
typeJsonSchema,
} from '../utils/common-json-schemas'
import {
validateGroupsConfiguration,
getOverloadSignatureGroups,
Expand Down Expand Up @@ -43,30 +52,39 @@ type MESSAGE_ID =
| 'unexpectedClassesGroupOrder'
| 'unexpectedClassesOrder'

const defaultGroups: SortClassesOptions[0]['groups'] = [
'index-signature',
['static-property', 'static-accessor-property'],
['static-get-method', 'static-set-method'],
['protected-static-property', 'protected-static-accessor-property'],
['protected-static-get-method', 'protected-static-set-method'],
['private-static-property', 'private-static-accessor-property'],
['private-static-get-method', 'private-static-set-method'],
'static-block',
['property', 'accessor-property'],
['get-method', 'set-method'],
['protected-property', 'protected-accessor-property'],
['protected-get-method', 'protected-set-method'],
['private-property', 'private-accessor-property'],
['private-get-method', 'private-set-method'],
'constructor',
['static-method', 'static-function-property'],
['protected-static-method', 'protected-static-function-property'],
['private-static-method', 'private-static-function-property'],
['method', 'function-property'],
['protected-method', 'protected-function-property'],
['private-method', 'private-function-property'],
'unknown',
]
const defaultOptions: Required<SortClassesOptions[0]> = {
groups: [
'index-signature',
['static-property', 'static-accessor-property'],
['static-get-method', 'static-set-method'],
['protected-static-property', 'protected-static-accessor-property'],
['protected-static-get-method', 'protected-static-set-method'],
['private-static-property', 'private-static-accessor-property'],
['private-static-get-method', 'private-static-set-method'],
'static-block',
['property', 'accessor-property'],
['get-method', 'set-method'],
['protected-property', 'protected-accessor-property'],
['protected-get-method', 'protected-set-method'],
['private-property', 'private-accessor-property'],
['private-get-method', 'private-set-method'],
'constructor',
['static-method', 'static-function-property'],
['protected-static-method', 'protected-static-function-property'],
['private-static-method', 'private-static-function-property'],
['method', 'function-property'],
['protected-method', 'protected-function-property'],
['private-method', 'private-function-property'],
'unknown',
],
matcher: 'minimatch',
partitionByComment: false,
type: 'alphabetical',
ignoreCase: true,
specialCharacters: 'keep',
customGroups: [],
order: 'asc',
}

export default createEslintRule<SortClassesOptions, MESSAGE_ID>({
name: 'sort-classes',
Expand All @@ -80,68 +98,17 @@ export default createEslintRule<SortClassesOptions, MESSAGE_ID>({
{
type: 'object',
properties: {
type: {
description: 'Specifies the sorting method.',
type: 'string',
enum: ['alphabetical', 'natural', 'line-length'],
},
order: {
description:
'Determines whether the sorted items should be in ascending or descending order.',
type: 'string',
enum: ['asc', 'desc'],
},
matcher: {
description: 'Specifies the string matcher.',
type: 'string',
enum: ['minimatch', 'regex'],
},
ignoreCase: {
description:
'Controls whether sorting should be case-sensitive or not.',
type: 'boolean',
},
specialCharacters: {
description:
'Controls how special characters should be handled before sorting.',
type: 'string',
enum: ['remove', 'trim', 'keep'],
},
type: typeJsonSchema,
order: orderJsonSchema,
matcher: matcherJsonSchema,
ignoreCase: ignoreCaseJsonSchema,
specialCharacters: specialCharactersJsonSchema,
partitionByComment: {
...partitionByCommentJsonSchema,
description:
'Allows to use comments to separate the class members into logical groups.',
anyOf: [
{
type: 'array',
items: {
type: 'string',
},
},
{
type: 'boolean',
},
{
type: 'string',
},
],
},
groups: {
description: 'Specifies the order of the groups.',
type: 'array',
items: {
oneOf: [
{
type: 'string',
},
{
type: 'array',
items: {
type: 'string',
},
},
],
},
},
groups: groupsJsonSchema,
customGroups: {
description: 'Specifies custom groups.',
type: 'array',
Expand Down Expand Up @@ -192,33 +159,13 @@ export default createEslintRule<SortClassesOptions, MESSAGE_ID>({
'Expected dependency "{{right}}" to come before "{{nodeDependentOnRight}}".',
},
},
defaultOptions: [
{
type: 'alphabetical',
order: 'asc',
ignoreCase: true,
specialCharacters: 'keep',
matcher: 'minimatch',
partitionByComment: false,
groups: defaultGroups,
customGroups: [],
},
],
defaultOptions: [defaultOptions],
create: context => ({
ClassBody: node => {
if (node.body.length > 1) {
let settings = getSettings(context.settings)

let options = complete(context.options.at(0), settings, {
groups: defaultGroups,
matcher: 'minimatch',
partitionByComment: false,
type: 'alphabetical',
ignoreCase: true,
specialCharacters: 'keep',
customGroups: [],
order: 'asc',
} as const)
let options = complete(context.options.at(0), settings, defaultOptions)

validateGroupsConfiguration(options.groups, options.customGroups)

Expand Down
Loading
Loading