Skip to content

Commit

Permalink
feat(sort-exports): adds feature
Browse files Browse the repository at this point in the history
  • Loading branch information
hugop95 committed Dec 25, 2024
1 parent 77b4a01 commit 4a7198e
Show file tree
Hide file tree
Showing 3 changed files with 242 additions and 1 deletion.
6 changes: 6 additions & 0 deletions docs/content/rules/sort-exports.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ Specifies the sorting locales. See [String.prototype.localeCompare() - locales](

### partitionByComment

<sub>
- type:
- `boolean | string | string[]` (`BaseType`)
- `{ block: BaseType; line: BaseType }`
</sub>
<sub>default: `false`</sub>

Allows you to use comments to separate the exports into logical groups. This can help in organizing and maintaining large export blocks by creating partitions based on comments.
Expand All @@ -146,6 +151,7 @@ Allows you to use comments to separate the exports into logical groups. This can
- `false` — Comments will not be used as delimiters.
- `string` — A regexp pattern to specify which comments should act as delimiters.
- `string[]` — A list of regexp patterns to specify which comments should act as delimiters.
- `{ block: BaseType; line: BaseType }` — Specify which block and line comments should act as delimiters.

### partitionByNewLine

Expand Down
9 changes: 8 additions & 1 deletion rules/sort-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,16 @@ import { pairwise } from '../utils/pairwise'

type Options = [
Partial<{
partitionByComment:
| {
block?: string[] | boolean | string
line?: string[] | boolean | string
}
| string[]
| boolean
| string
type: 'alphabetical' | 'line-length' | 'natural' | 'custom'
groupKind: 'values-first' | 'types-first' | 'mixed'
partitionByComment: string[] | boolean | string
specialCharacters: 'remove' | 'trim' | 'keep'
locales: NonNullable<Intl.LocalesArgument>
partitionByNewLine: boolean
Expand Down
228 changes: 228 additions & 0 deletions test/rules/sort-exports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,234 @@ describe(ruleName, () => {
},
)

describe(`${ruleName}(${type}): allows to use "partitionByComment.line"`, () => {
ruleTester.run(`${ruleName}(${type}): ignores block comments`, rule, {
invalid: [
{
errors: [
{
data: {
right: './a',
left: './b',
},
messageId: 'unexpectedExportsOrder',
},
],
options: [
{
...options,
partitionByComment: {
line: true,
},
},
],
output: dedent`
/* Comment */
export * from './a'
export * from './b'
`,
code: dedent`
export * from './b'
/* Comment */
export * from './a'
`,
},
],
valid: [],
})

ruleTester.run(
`${ruleName}(${type}): allows to use all comments as parts`,
rule,
{
valid: [
{
options: [
{
...options,
partitionByComment: {
line: true,
},
},
],
code: dedent`
export * from './b'
// Comment
export * from './a'
`,
},
],
invalid: [],
},
)

ruleTester.run(
`${ruleName}(${type}): allows to use multiple partition comments`,
rule,
{
valid: [
{
options: [
{
...options,
partitionByComment: {
line: ['a', 'b'],
},
},
],
code: dedent`
export * from './c'
// b
export * from './b'
// a
export * from './a'
`,
},
],
invalid: [],
},
)

ruleTester.run(
`${ruleName}(${type}): allows to use regex for partition comments`,
rule,
{
valid: [
{
options: [
{
...options,
partitionByComment: {
line: ['^(?!.*foo).*$'],
},
},
],
code: dedent`
export * from './b'
// I am a partition comment because I don't have f o o
export * from './a'
`,
},
],
invalid: [],
},
)
})

describe(`${ruleName}(${type}): allows to use "partitionByComment.block"`, () => {
ruleTester.run(`${ruleName}(${type}): ignores line comments`, rule, {
invalid: [
{
errors: [
{
data: {
right: './a',
left: './b',
},
messageId: 'unexpectedExportsOrder',
},
],
options: [
{
...options,
partitionByComment: {
block: true,
},
},
],
output: dedent`
// Comment
export * from './a'
export * from './b'
`,
code: dedent`
export * from './b'
// Comment
export * from './a'
`,
},
],
valid: [],
})

ruleTester.run(
`${ruleName}(${type}): allows to use all comments as parts`,
rule,
{
valid: [
{
options: [
{
...options,
partitionByComment: {
block: true,
},
},
],
code: dedent`
export * from './b'
/* Comment */
export * from './a'
`,
},
],
invalid: [],
},
)

ruleTester.run(
`${ruleName}(${type}): allows to use multiple partition comments`,
rule,
{
valid: [
{
options: [
{
...options,
partitionByComment: {
block: ['a', 'b'],
},
},
],
code: dedent`
export * from './c'
/* b */
export * from './b'
/* a */
export * from './a'
`,
},
],
invalid: [],
},
)

ruleTester.run(
`${ruleName}(${type}): allows to use regex for partition comments`,
rule,
{
valid: [
{
options: [
{
...options,
partitionByComment: {
block: ['^(?!.*foo).*$'],
},
},
],
code: dedent`
export * from './b'
/* I am a partition comment because I don't have f o o */
export * from './a'
`,
},
],
invalid: [],
},
)
})

ruleTester.run(`${ruleName}(${type}): sorts by group kind`, rule, {
invalid: [
{
Expand Down

0 comments on commit 4a7198e

Please sign in to comment.