Skip to content

Commit

Permalink
fix: fix output with a large number of groups
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Nov 24, 2023
1 parent da903e7 commit 6847eaf
Show file tree
Hide file tree
Showing 10 changed files with 229 additions and 9 deletions.
4 changes: 3 additions & 1 deletion rules/sort-astro-attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({

let sortedNodes: SortingNode[] = []

for (let group of Object.keys(grouped).sort()) {
for (let group of Object.keys(grouped).sort(
(a, b) => Number(a) - Number(b),
)) {
sortedNodes.push(...sortNodes(grouped[group], options))
}

Expand Down
2 changes: 1 addition & 1 deletion rules/sort-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
)

let formatted = Object.keys(grouped)
.sort()
.sort((a, b) => Number(a) - Number(b))
.reduce(
(accumulator: SortingNode[], group: string) => [
...accumulator,
Expand Down
2 changes: 1 addition & 1 deletion rules/sort-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
}

let formatted = Object.keys(grouped)
.sort()
.sort((a, b) => Number(a) - Number(b))
.reduce(
(accumulator: SortingNode[], group: string) => [
...accumulator,
Expand Down
4 changes: 3 additions & 1 deletion rules/sort-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({

let sortedNodes: SortingNode[] = []

for (let group of Object.keys(grouped).sort()) {
for (let group of Object.keys(grouped).sort(
(a, b) => Number(a) - Number(b),
)) {
sortedNodes.push(...sortNodes(grouped[group], options))
}

Expand Down
4 changes: 3 additions & 1 deletion rules/sort-jsx-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({

let sortedNodes: SortingNode[] = []

for (let group of Object.keys(grouped).sort()) {
for (let group of Object.keys(grouped).sort(
(a, b) => Number(a) - Number(b),
)) {
sortedNodes.push(...sortNodes(grouped[group], options))
}

Expand Down
4 changes: 3 additions & 1 deletion rules/sort-object-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,9 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({

let sortedNodes: SortingNode[] = []

for (let group of Object.keys(grouped).sort()) {
for (let group of Object.keys(grouped).sort(
(a, b) => Number(a) - Number(b),
)) {
sortedNodes.push(...sortNodes(grouped[group], options))
}

Expand Down
4 changes: 3 additions & 1 deletion rules/sort-objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,9 @@ export default createEslintRule<Options, MESSAGE_ID>({

let sortedNodes: SortingNode[] = []

for (let group of Object.keys(grouped).sort()) {
for (let group of Object.keys(grouped).sort(
(a, b) => Number(a) - Number(b),
)) {
sortedNodes.push(...sortNodes(grouped[group], options))
}

Expand Down
4 changes: 3 additions & 1 deletion rules/sort-svelte-attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({

let sortedNodes: SortingNode[] = []

for (let group of Object.keys(grouped).sort()) {
for (let group of Object.keys(grouped).sort(
(a, b) => Number(a) - Number(b),
)) {
sortedNodes.push(...sortNodes(grouped[group], options))
}

Expand Down
4 changes: 3 additions & 1 deletion rules/sort-vue-attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,9 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({

let sortedNodes: SortingNode[] = []

for (let group of Object.keys(grouped).sort()) {
for (let group of Object.keys(grouped).sort(
(a, b) => Number(a) - Number(b),
)) {
sortedNodes.push(...sortNodes(grouped[group], options))
}

Expand Down
206 changes: 206 additions & 0 deletions test/sort-imports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3778,5 +3778,211 @@ describe(RULE_NAME, () => {
],
},
)

ruleTester.run(
`${RULE_NAME}: works with big amount of custom groups`,
rule,
{
valid: [
{
code: dedent`
import { useCartStore } from '~/stores/cartStore.ts'
import { useUserStore } from '~/stores/userStore.ts'
import { getCart } from '~/services/cartService.ts'
import { connect } from '~/utils/ws.ts'
import { formattingDate } from '~/utils/dateTime.ts'
import { useFetch } from '~/composable/useFetch.ts'
import { useDebounce } from '~/composable/useDebounce.ts'
import { useMouseMove } from '~/composable/useMouseMove.ts'
import ComponentA from '~/components/ComponentA.vue'
import ComponentB from '~/components/ComponentB.vue'
import ComponentC from '~/components/ComponentC.vue'
import CartComponentA from './cart/CartComponentA.vue'
import CartComponentB from './cart/CartComponentB.vue'
`,
options: [
{
type: SortType['line-length'],
groups: [
['builtin', 'external'],
'internal',
'stores',
'services',
'validators',
'utils',
'logics',
'composable',
'ui',
'components',
'pages',
'widgets',
'assets',
'parent',
'sibling',
'side-effect',
'index',
'style',
'object',
'unknown',
],
'custom-groups': {
value: {
stores: ['~/stores/**'],
services: ['~/services/**'],
validators: ['~/validators/**'],
utils: ['~/utils/**'],
logics: ['~/logics/**'],
composable: ['~/composable/**'],
ui: ['~/ui/**'],
components: ['~/components/**'],
pages: ['~/pages/**'],
widgets: ['~/widgets/**'],
assets: ['~/assets/**'],
},
},
'newlines-between': NewlinesBetweenValue.always,
'internal-pattern': ['~/**'],
},
],
},
],
invalid: [
{
code: dedent`
import CartComponentA from './cart/CartComponentA.vue'
import CartComponentB from './cart/CartComponentB.vue'
import { connect } from '~/utils/ws.ts'
import { getCart } from '~/services/cartService.ts'
import { useUserStore } from '~/stores/userStore.ts'
import { formattingDate } from '~/utils/dateTime.ts'
import { useFetch } from '~/composable/useFetch.ts'
import { useCartStore } from '~/stores/cartStore.ts'
import { useDebounce } from '~/composable/useDebounce.ts'
import { useMouseMove } from '~/composable/useMouseMove.ts'
import ComponentA from '~/components/ComponentA.vue'
import ComponentB from '~/components/ComponentB.vue'
import ComponentC from '~/components/ComponentC.vue'
`,
output: dedent`
import { useUserStore } from '~/stores/userStore.ts'
import { useCartStore } from '~/stores/cartStore.ts'
import { getCart } from '~/services/cartService.ts'
import { connect } from '~/utils/ws.ts'
import { formattingDate } from '~/utils/dateTime.ts'
import { useFetch } from '~/composable/useFetch.ts'
import { useDebounce } from '~/composable/useDebounce.ts'
import { useMouseMove } from '~/composable/useMouseMove.ts'
import ComponentA from '~/components/ComponentA.vue'
import ComponentB from '~/components/ComponentB.vue'
import ComponentC from '~/components/ComponentC.vue'
import CartComponentA from './cart/CartComponentA.vue'
import CartComponentB from './cart/CartComponentB.vue'
`,
options: [
{
type: SortType['line-length'],
groups: [
['builtin', 'external'],
'internal',
'stores',
'services',
'validators',
'utils',
'logics',
'composable',
'ui',
'components',
'pages',
'widgets',
'assets',
'parent',
'sibling',
'side-effect',
'index',
'style',
'object',
'unknown',
],
'custom-groups': {
value: {
stores: ['~/stores/**'],
services: ['~/services/**'],
validators: ['~/validators/**'],
utils: ['~/utils/**'],
logics: ['~/logics/**'],
composable: ['~/composable/**'],
ui: ['~/ui/**'],
components: ['~/components/**'],
pages: ['~/pages/**'],
widgets: ['~/widgets/**'],
assets: ['~/assets/**'],
},
},
'newlines-between': NewlinesBetweenValue.always,
'internal-pattern': ['~/**'],
},
],
errors: [
{
messageId: 'unexpectedImportsOrder',
data: {
left: './cart/CartComponentB.vue',
right: '~/utils/ws.ts',
},
},
{
messageId: 'unexpectedImportsOrder',
data: {
left: '~/utils/ws.ts',
right: '~/services/cartService.ts',
},
},
{
messageId: 'unexpectedImportsOrder',
data: {
left: '~/services/cartService.ts',
right: '~/stores/userStore.ts',
},
},
{
messageId: 'missedSpacingBetweenImports',
data: {
left: '~/stores/userStore.ts',
right: '~/utils/dateTime.ts',
},
},
{
messageId: 'unexpectedImportsOrder',
data: {
left: '~/composable/useFetch.ts',
right: '~/stores/cartStore.ts',
},
},
{
messageId: 'missedSpacingBetweenImports',
data: {
left: '~/stores/cartStore.ts',
right: '~/composable/useDebounce.ts',
},
},
],
},
],
},
)
})
})

0 comments on commit 6847eaf

Please sign in to comment.