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: add use-groups helper #42

Merged
merged 3 commits into from
Jul 23, 2023
Merged
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
63 changes: 13 additions & 50 deletions rules/sort-astro-attributes.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import type { TSESTree } from '@typescript-eslint/types'
import type { AST } from 'astro-eslint-parser'

import { minimatch } from 'minimatch'
import path from 'path'

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

import { createEslintRule } from '../utils/create-eslint-rule'
import { getGroupNumber } from '../utils/get-group-number'
import { rangeToDiff } from '../utils/range-to-diff'
import { SortOrder, SortType } from '../typings'
import { useGroups } from '../utils/use-groups'
import { makeFixes } from '../utils/make-fixes'
import { sortNodes } from '../utils/sort-nodes'
import { pairwise } from '../utils/pairwise'
Expand All @@ -22,10 +23,6 @@ type Group<T extends string[]> =
| 'unknown'
| T[number]

type SortingNodeWithGroup<T extends string[]> = SortingNode & {
group: Group<T>
}

type MESSAGE_ID = 'unexpectedAstroAttributesOrder'

type Options<T extends string[]> = [
Expand Down Expand Up @@ -112,8 +109,8 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({

let source = context.getSourceCode()

let parts: SortingNodeWithGroup<string[]>[][] = attributes.reduce(
(accumulator: SortingNodeWithGroup<string[]>[][], attribute) => {
let parts: SortingNode[][] = attributes.reduce(
(accumulator: SortingNode[][], attribute) => {
if (attribute.type === 'JSXSpreadAttribute') {
accumulator.push([])
return accumulator
Expand All @@ -124,28 +121,11 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
? attribute.name.name
: source.text.slice(...attribute.name.range)

let group: Group<string[]> | undefined
let { getGroup, defineGroup, setCustomGroups } = useGroups(
options.groups,
)

let defineGroup = (nodeGroup: Group<string[]>) => {
if (!group && options.groups.flat().includes(nodeGroup)) {
group = nodeGroup
}
}

for (let [key, pattern] of Object.entries(
options['custom-groups'],
)) {
if (
Array.isArray(pattern) &&
pattern.some(patternValue => minimatch(name, patternValue))
) {
defineGroup(key)
}

if (typeof pattern === 'string' && minimatch(name, pattern)) {
defineGroup(key)
}
}
setCustomGroups(options['custom-groups'], name)

if (attribute.type === 'AstroShorthandAttribute') {
defineGroup('astro-shorthand')
Expand All @@ -163,7 +143,7 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
accumulator.at(-1)!.push({
size: rangeToDiff(attribute.range),
node: attribute as unknown as TSESTree.Node,
group: group ?? 'unknown',
group: getGroup(),
name,
})

Expand All @@ -172,27 +152,10 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
[[]],
)

let getGroupNumber = (
nodeWithGroup: SortingNodeWithGroup<string[]>,
): number => {
for (let i = 0, max = options.groups.length; i < max; i++) {
let currentGroup = options.groups[i]

if (
nodeWithGroup.group === currentGroup ||
(Array.isArray(currentGroup) &&
currentGroup.includes(nodeWithGroup.group))
) {
return i
}
}
return options.groups.length
}

for (let nodes of parts) {
pairwise(nodes, (left, right) => {
let leftNum = getGroupNumber(left)
let rightNum = getGroupNumber(right)
let leftNum = getGroupNumber(options.groups, left)
let rightNum = getGroupNumber(options.groups, right)

if (
leftNum > rightNum ||
Expand All @@ -207,11 +170,11 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
node: right.node,
fix: fixer => {
let grouped: {
[key: string]: SortingNodeWithGroup<string[]>[]
[key: string]: SortingNode[]
} = {}

for (let currentNode of nodes) {
let groupNum = getGroupNumber(currentNode)
let groupNum = getGroupNumber(options.groups, currentNode)

if (!(groupNum in grouped)) {
grouped[groupNum] = [currentNode]
Expand Down
41 changes: 10 additions & 31 deletions rules/sort-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { AST_NODE_TYPES } from '@typescript-eslint/types'
import type { SortingNode } from '../typings'

import { createEslintRule } from '../utils/create-eslint-rule'
import { getGroupNumber } from '../utils/get-group-number'
import { toSingleLine } from '../utils/to-single-line'
import { getNodeRange } from '../utils/get-node-range'
import { rangeToDiff } from '../utils/range-to-diff'
import { SortOrder, SortType } from '../typings'
import { useGroups } from '../utils/use-groups'
import { sortNodes } from '../utils/sort-nodes'
import { complete } from '../utils/complete'
import { pairwise } from '../utils/pairwise'
Expand All @@ -35,8 +37,6 @@ type Options = [
}>,
]

type SortingNodeWithGroup = SortingNode & { group: Group }

export const RULE_NAME = 'sort-classes'

export default createEslintRule<Options, MESSAGE_ID>({
Expand Down Expand Up @@ -98,9 +98,9 @@ export default createEslintRule<Options, MESSAGE_ID>({

let source = context.getSourceCode()

let nodes: SortingNodeWithGroup[] = node.body.map(member => {
let group: undefined | Group
let nodes: SortingNode[] = node.body.map(member => {
let name: string
let { getGroup, defineGroup } = useGroups(options.groups)

if (member.type === AST_NODE_TYPES.StaticBlock) {
name = 'static'
Expand All @@ -117,12 +117,6 @@ export default createEslintRule<Options, MESSAGE_ID>({
}
}

let defineGroup = (nodeGroup: Group) => {
if (!group && options.groups.flat().includes(nodeGroup)) {
group = nodeGroup
}
}

if (member.type === AST_NODE_TYPES.MethodDefinition) {
if (member.kind === 'constructor') {
defineGroup('constructor')
Expand Down Expand Up @@ -151,30 +145,15 @@ export default createEslintRule<Options, MESSAGE_ID>({

return {
size: rangeToDiff(member.range),
group: group ?? 'unknown',
group: getGroup(),
node: member,
name,
}
})

let getGroupNumber = (sortingNode: SortingNodeWithGroup): number => {
for (let i = 0, max = options.groups.length; i < max; i++) {
let currentGroup = options.groups[i]

if (
sortingNode.group === currentGroup ||
(Array.isArray(currentGroup) &&
currentGroup.includes(sortingNode.group))
) {
return i
}
}
return options.groups.length
}

pairwise(nodes, (left, right) => {
let leftNum = getGroupNumber(left)
let rightNum = getGroupNumber(right)
let leftNum = getGroupNumber(options.groups, left)
let rightNum = getGroupNumber(options.groups, right)

if (
leftNum > rightNum ||
Expand All @@ -193,11 +172,11 @@ export default createEslintRule<Options, MESSAGE_ID>({
let grouped = nodes.reduce(
(
accumulator: {
[key: string]: SortingNodeWithGroup[]
[key: string]: SortingNode[]
},
sortingNode,
) => {
let groupNum = getGroupNumber(sortingNode)
let groupNum = getGroupNumber(options.groups, sortingNode)

if (!(groupNum in accumulator)) {
accumulator[groupNum] = [sortingNode]
Expand All @@ -216,7 +195,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
let formatted = Object.keys(grouped)
.sort()
.reduce(
(accumulator: SortingNodeWithGroup[], group: string) => [
(accumulator: SortingNode[], group: string) => [
...accumulator,
...grouped[group],
],
Expand Down
Loading
Loading