Skip to content

Commit

Permalink
feat: generate exception if group does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
hugop95 committed Sep 1, 2024
1 parent d9c72d2 commit 0872bdd
Show file tree
Hide file tree
Showing 21 changed files with 480 additions and 5 deletions.
7 changes: 7 additions & 0 deletions rules/sort-astro-attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import path from 'node:path'

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

import { validateGroupsConfiguration } from '../utils/validate-groups-configuration'
import { createEslintRule } from '../utils/create-eslint-rule'
import { getGroupNumber } from '../utils/get-group-number'
import { getSourceCode } from '../utils/get-source-code'
Expand Down Expand Up @@ -138,6 +139,12 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
groups: [],
} as const)

validateGroupsConfiguration(
options.groups,
['astro-shorthand', 'multiline', 'shorthand', 'unknown'],
Object.keys(options.customGroups),
)

let sourceCode = getSourceCode(context)

let parts: SortingNode[][] = attributes.reduce(
Expand Down
29 changes: 29 additions & 0 deletions rules/sort-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { minimatch } from 'minimatch'

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

import { validateGroupsConfiguration } from '../utils/validate-groups-configuration'
import { getCommentBefore } from '../utils/get-comment-before'
import { createEslintRule } from '../utils/create-eslint-rule'
import { getLinesBetween } from '../utils/get-lines-between'
Expand Down Expand Up @@ -256,6 +257,34 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
options.groups = [...options.groups, 'unknown']
}

validateGroupsConfiguration(
options.groups,
[
'side-effect-style',
'external-type',
'internal-type',
'builtin-type',
'sibling-type',
'parent-type',
'side-effect',
'index-type',
'internal',
'external',
'sibling',
'unknown',
'builtin',
'parent',
'object',
'index',
'style',
'type',
],
[
...Object.keys(options.customGroups.type ?? {}),
...Object.keys(options.customGroups.value ?? {}),
],
)

let nodes: SortingNode[] = []

let isSideEffectImport = (node: TSESTree.Node) =>
Expand Down
7 changes: 7 additions & 0 deletions rules/sort-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { minimatch } from 'minimatch'

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

import { validateGroupsConfiguration } from '../utils/validate-groups-configuration'
import { createEslintRule } from '../utils/create-eslint-rule'
import { isMemberOptional } from '../utils/is-member-optional'
import { getLinesBetween } from '../utils/get-lines-between'
Expand Down Expand Up @@ -151,6 +152,12 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
groups: [],
} as const)

validateGroupsConfiguration(
options.groups,
['multiline', 'unknown'],
Object.keys(options.customGroups),
)

let sourceCode = getSourceCode(context)

if (
Expand Down
21 changes: 21 additions & 0 deletions rules/sort-intersection-types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { SortingNode } from '../typings'

import { validateGroupsConfiguration } from '../utils/validate-groups-configuration'
import { createEslintRule } from '../utils/create-eslint-rule'
import { getGroupNumber } from '../utils/get-group-number'
import { getSourceCode } from '../utils/get-source-code'
Expand Down Expand Up @@ -113,6 +114,26 @@ export default createEslintRule<Options, MESSAGE_ID>({
groups: [],
} as const)

validateGroupsConfiguration(
options.groups,
[
'intersection',
'conditional',
'function',
'operator',
'keyword',
'literal',
'nullish',
'unknown',
'import',
'object',
'named',
'tuple',
'union',
],
[],
)

let sourceCode = getSourceCode(context)

let nodes: SortingNode[] = node.types.map(type => {
Expand Down
7 changes: 7 additions & 0 deletions rules/sort-jsx-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { minimatch } from 'minimatch'

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

import { validateGroupsConfiguration } from '../utils/validate-groups-configuration'
import { createEslintRule } from '../utils/create-eslint-rule'
import { getGroupNumber } from '../utils/get-group-number'
import { getSourceCode } from '../utils/get-source-code'
Expand Down Expand Up @@ -139,6 +140,12 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
groups: [],
} as const)

validateGroupsConfiguration(
options.groups,
['multiline', 'shorthand', 'unknown'],
Object.keys(options.customGroups),
)

let sourceCode = getSourceCode(context)

let shouldIgnore = false
Expand Down
9 changes: 8 additions & 1 deletion rules/sort-object-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { TSESTree } from '@typescript-eslint/types'

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

import { validateGroupsConfiguration } from '../utils/validate-groups-configuration'
import { createEslintRule } from '../utils/create-eslint-rule'
import { getLinesBetween } from '../utils/get-lines-between'
import { getGroupNumber } from '../utils/get-group-number'
Expand All @@ -24,12 +25,12 @@ type Group<T extends string[]> = 'multiline' | 'unknown' | T[number]
type Options<T extends string[]> = [
Partial<{
groupKind: 'required-first' | 'optional-first' | 'mixed'
customGroups: { [key in T[number]]: string[] | string }
type: 'alphabetical' | 'line-length' | 'natural'
groups: (Group<T>[] | Group<T>)[]
partitionByNewLine: boolean
order: 'desc' | 'asc'
ignoreCase: boolean
customGroups: {}
}>,
]

Expand Down Expand Up @@ -140,6 +141,12 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
groups: [],
} as const)

validateGroupsConfiguration(
options.groups,
['multiline', 'unknown'],
Object.keys(options.customGroups),
)

let sourceCode = getSourceCode(context)

let formattedMembers: SortingNode<TSESTree.TypeElement>[][] =
Expand Down
11 changes: 10 additions & 1 deletion rules/sort-objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { minimatch } from 'minimatch'

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

import { validateGroupsConfiguration } from '../utils/validate-groups-configuration'
import { isPartitionComment } from '../utils/is-partition-comment'
import { getCommentBefore } from '../utils/get-comment-before'
import { createEslintRule } from '../utils/create-eslint-rule'
Expand All @@ -30,6 +31,8 @@ export enum Position {
'ignore' = 'ignore',
}

type Group = 'unknown' | string

type SortingNodeWithPosition = {
position: Position
} & SortingNode
Expand All @@ -39,7 +42,7 @@ type Options = [
customGroups: { [key: string]: string[] | string }
type: 'alphabetical' | 'line-length' | 'natural'
partitionByComment: string[] | boolean | string
groups: (string[] | string)[]
groups: (Group[] | Group)[]
partitionByNewLine: boolean
styledComponents: boolean
destructureOnly: boolean
Expand Down Expand Up @@ -191,6 +194,12 @@ export default createEslintRule<Options, MESSAGE_ID>({
groups: [],
} as const)

validateGroupsConfiguration(
options.groups,
['unknown'],
Object.keys(options.customGroups),
)

let shouldIgnore = false

if (options.destructureOnly) {
Expand Down
7 changes: 7 additions & 0 deletions rules/sort-svelte-attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import path from 'node:path'

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

import { validateGroupsConfiguration } from '../utils/validate-groups-configuration'
import { createEslintRule } from '../utils/create-eslint-rule'
import { getGroupNumber } from '../utils/get-group-number'
import { getSourceCode } from '../utils/get-source-code'
Expand Down Expand Up @@ -135,6 +136,12 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
groups: [],
} as const)

validateGroupsConfiguration(
options.groups,
['svelte-shorthand', 'multiline', 'shorthand', 'unknown'],
Object.keys(options.customGroups),
)

let sourceCode = getSourceCode(context)

let parts: SortingNode[][] = node.attributes.reduce(
Expand Down
21 changes: 21 additions & 0 deletions rules/sort-union-types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { SortingNode } from '../typings'

import { validateGroupsConfiguration } from '../utils/validate-groups-configuration'
import { createEslintRule } from '../utils/create-eslint-rule'
import { getGroupNumber } from '../utils/get-group-number'
import { getSourceCode } from '../utils/get-source-code'
Expand Down Expand Up @@ -113,6 +114,26 @@ export default createEslintRule<Options, MESSAGE_ID>({
groups: [],
} as const)

validateGroupsConfiguration(
options.groups,
[
'intersection',
'conditional',
'function',
'operator',
'keyword',
'literal',
'nullish',
'unknown',
'import',
'object',
'named',
'tuple',
'union',
],
[],
)

let sourceCode = getSourceCode(context)

let nodes: SortingNode[] = node.types.map(type => {
Expand Down
7 changes: 7 additions & 0 deletions rules/sort-vue-attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import path from 'node:path'

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

import { validateGroupsConfiguration } from '../utils/validate-groups-configuration'
import { createEslintRule } from '../utils/create-eslint-rule'
import { getGroupNumber } from '../utils/get-group-number'
import { getSourceCode } from '../utils/get-source-code'
Expand Down Expand Up @@ -147,6 +148,12 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
groups: [],
} as const)

validateGroupsConfiguration(
options.groups,
['multiline', 'shorthand', 'unknown'],
Object.keys(options.customGroups),
)

let parts: SortingNode[][] = node.attributes.reduce(
(accumulator: SortingNode[][], attribute) => {
if (
Expand Down
41 changes: 38 additions & 3 deletions test/sort-astro-attributes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ describe(ruleName, () => {
options: [
{
...options,
groups: ['unknown', ['svelte-shorthand', 'shorthand']],
groups: ['unknown', ['shorthand']],
},
],
errors: [
Expand Down Expand Up @@ -699,7 +699,7 @@ describe(ruleName, () => {
options: [
{
...options,
groups: ['unknown', ['svelte-shorthand', 'shorthand']],
groups: ['unknown', ['shorthand']],
},
],
errors: [
Expand Down Expand Up @@ -1130,7 +1130,7 @@ describe(ruleName, () => {
options: [
{
...options,
groups: ['unknown', ['svelte-shorthand', 'shorthand']],
groups: ['unknown', ['shorthand']],
},
],
errors: [
Expand Down Expand Up @@ -1333,6 +1333,41 @@ describe(ruleName, () => {
})
})

describe(`${ruleName}: validating group configuration`, () => {
ruleTester.run(
`${ruleName}: allows predefined groups and defined custom groups`,
rule,
{
valid: [
{
filename: 'file.astro',
code: dedent`
---
import Component from '../file2.astro'
---
<Component a="a" bb="b" />
`,
options: [
{
groups: [
'astro-shorthand',
'multiline',
'shorthand',
'unknown',
'myCustomGroup',
],
customGroups: {
myCustomGroup: 'x',
},
},
],
},
],
invalid: [],
},
)
})

describe(`${ruleName}: misc`, () => {
ruleTester.run(`${ruleName}: works only for .astro files`, rule, {
valid: [
Expand Down
Loading

0 comments on commit 0872bdd

Please sign in to comment.