Skip to content

Commit

Permalink
refactor: add use-groups helper
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Jul 23, 2023
1 parent ec8e78b commit 20817e2
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 138 deletions.
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
54 changes: 16 additions & 38 deletions rules/sort-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import type { SortingNode } from '../typings'

import { getCommentBefore } from '../utils/get-comment-before'
import { createEslintRule } from '../utils/create-eslint-rule'
import { getGroupNumber } from '../utils/get-group-number'
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 Down Expand Up @@ -69,10 +71,6 @@ type ModuleDeclaration =
| TSESTree.TSImportEqualsDeclaration
| TSESTree.ImportDeclaration

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

export default createEslintRule<Options<string[]>, MESSAGE_ID>({
name: RULE_NAME,
meta: {
Expand Down Expand Up @@ -163,11 +161,9 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({

let source = context.getSourceCode()

let nodes: SortingNodeWithGroup<string[]>[] = []
let nodes: SortingNode[] = []

let computeGroup = (node: ModuleDeclaration): Group<string[]> => {
let group: Group<string[]> | undefined

let isStyle = (value: string) =>
['.less', '.scss', '.sass', '.styl', '.pcss', '.css', '.sss'].some(
extension => value.endsWith(extension),
Expand All @@ -188,11 +184,7 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({

let isSibling = (value: string) => value.indexOf('./') === 0

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

let isInternal = (nodeElement: TSESTree.ImportDeclaration) =>
(options['internal-pattern'].length &&
Expand Down Expand Up @@ -250,7 +242,7 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
defineGroup('type')
}

if (!group && node.type === AST_NODE_TYPES.ImportDeclaration) {
if (node.type === AST_NODE_TYPES.ImportDeclaration) {
determineCustomGroup('value', node.source.value)

if (isCoreModule(node.source.value)) {
Expand Down Expand Up @@ -284,7 +276,7 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
defineGroup('external')
}

return group ?? 'unknown'
return getGroup()
}

let registerNode = (node: ModuleDeclaration) => {
Expand Down Expand Up @@ -316,20 +308,6 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
TSImportEqualsDeclaration: registerNode,
ImportDeclaration: registerNode,
'Program:exit': () => {
let getGroupNumber = (node: SortingNodeWithGroup<string[]>): number => {
for (let i = 0, max = options.groups.length; i < max; i++) {
let currentGroup = options.groups[i]

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

let hasContentBetweenNodes = (
left: SortingNode,
right: SortingNode,
Expand All @@ -356,16 +334,16 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({

let fix = (
fixer: TSESLint.RuleFixer,
nodesToFix: SortingNodeWithGroup<string[]>[],
nodesToFix: SortingNode[],
): TSESLint.RuleFix[] => {
let fixes: TSESLint.RuleFix[] = []

let grouped: {
[key: string]: SortingNodeWithGroup<string[]>[]
[key: string]: SortingNode[]
} = {}

for (let node of nodesToFix) {
let groupNum = getGroupNumber(node)
let groupNum = getGroupNumber(options.groups, node)

if (!(groupNum in grouped)) {
grouped[groupNum] = [node]
Expand All @@ -381,7 +359,7 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
.sort()
.reduce(
(
accumulator: SortingNodeWithGroup<string[]>[],
accumulator: SortingNode[],
group: string,
) => [...accumulator, ...grouped[group]],
[],
Expand All @@ -408,7 +386,7 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({

if (
(options['newlines-between'] === 'always' &&
getGroupNumber(node) === getGroupNumber(nextNode) &&
getGroupNumber(options.groups, node) === getGroupNumber(options.groups, nextNode) &&
linesBetweenImports !== 0) ||
(options['newlines-between'] === 'never' &&
linesBetweenImports > 0)
Expand All @@ -424,7 +402,7 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({

if (
options['newlines-between'] === 'always' &&
getGroupNumber(node) !== getGroupNumber(nextNode) &&
getGroupNumber(options.groups, node) !== getGroupNumber(options.groups, nextNode) &&
linesBetweenImports > 1
) {
fixes.push(
Expand All @@ -442,7 +420,7 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({

if (
options['newlines-between'] === 'always' &&
getGroupNumber(node) !== getGroupNumber(nextNode) &&
getGroupNumber(options.groups, node) !== getGroupNumber(options.groups, nextNode) &&
linesBetweenImports === 0
) {
fixes.push(
Expand All @@ -459,7 +437,7 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
return fixes
}

let splittedNodes: SortingNodeWithGroup<string[]>[][] = [[]]
let splittedNodes: SortingNode[][] = [[]]

for (let node of nodes) {
let lastNode = splittedNodes.at(-1)?.at(-1)
Expand All @@ -473,8 +451,8 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({

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

let numberOfEmptyLinesBetween = getLinesBetweenImports(left, right)

Expand Down
46 changes: 12 additions & 34 deletions rules/sort-jsx-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { minimatch } from 'minimatch'
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 { makeFixes } from '../utils/make-fixes'
import { useGroups } from '../utils/use-groups'
import { sortNodes } from '../utils/sort-nodes'
import { pairwise } from '../utils/pairwise'
import { complete } from '../utils/complete'
Expand All @@ -22,10 +24,6 @@ type Group<T extends string[]> =
| 'unknown'
| T[number]

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

type Options<T extends string[]> = [
Partial<{
'custom-groups': { [key in T[number]]: string[] | string }
Expand Down Expand Up @@ -101,10 +99,10 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({

let source = context.getSourceCode()

let parts: SortingNodeWithGroup<string[]>[][] =
let parts: SortingNode[][] =
node.openingElement.attributes.reduce(
(
accumulator: SortingNodeWithGroup<string[]>[][],
accumulator: SortingNode[][],
attribute: TSESTree.JSXSpreadAttribute | TSESTree.JSXAttribute,
) => {
if (attribute.type === AST_NODE_TYPES.JSXSpreadAttribute) {
Expand All @@ -113,16 +111,10 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
}

let name = attribute.name.type === AST_NODE_TYPES.JSXNamespacedName
? `${attribute.name.namespace.name}:${attribute.name.name.name}`
: attribute.name.name

let group: Group<string[]> | undefined
? `${attribute.name.namespace.name}:${attribute.name.name.name}`
: attribute.name.name

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

for (let [key, pattern] of Object.entries(options['custom-groups'])) {
if (
Expand All @@ -147,7 +139,7 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({

let jsxNode = {
size: rangeToDiff(attribute.range),
group: group ?? 'unknown',
group: getGroup(),
node: attribute,
name,
}
Expand All @@ -159,24 +151,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 ||
(leftNum === rightNum && compare(left, right, options)))) {
Expand All @@ -189,11 +167,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
Loading

0 comments on commit 20817e2

Please sign in to comment.