Skip to content

Commit

Permalink
feat: allow to ignore interface by pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Jun 1, 2023
1 parent 84cfc3d commit 9aaf08a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
4 changes: 4 additions & 0 deletions docs/rules/sort-interfaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ interface Hero {
- `asc` - enforce properties to be in ascending order.
- `desc` - enforce properties to be in descending order.

### `ignore-pattern`

- `[string]` (default: `[]`) - allows to ignore interface by pattern

## 鈿欙笍 Usage

### Legacy config
Expand Down
18 changes: 15 additions & 3 deletions rules/sort-interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { SortingNode } from '../typings'

import { AST_NODE_TYPES } from '@typescript-eslint/types'
import { minimatch } from 'minimatch'

import { createEslintRule } from '../utils/create-eslint-rule'
import { rangeToDiff } from '../utils/range-to-diff'
Expand All @@ -16,6 +17,7 @@ type Options = [
Partial<{
order: SortOrder
type: SortType
'ignore-pattern': string[]
}>,
]

Expand Down Expand Up @@ -46,6 +48,10 @@ export default createEslintRule<Options, MESSAGE_ID>({
enum: [SortOrder.asc, SortOrder.desc],
default: SortOrder.asc,
},
'ignore-pattern': {
type: 'array',
default: [],
},
},
additionalProperties: false,
},
Expand All @@ -62,16 +68,22 @@ export default createEslintRule<Options, MESSAGE_ID>({
},
],
create: context => ({
TSInterfaceBody: node => {
TSInterfaceDeclaration: node => {
let options = complete(context.options.at(0), {
type: SortType.alphabetical,
order: SortOrder.asc,
'ignore-pattern': [],
})

if (node.body.length > 1) {
if (
!options['ignore-pattern'].some(pattern =>
minimatch(node.id.name, pattern),
) &&
node.body.body.length > 1
) {
let source = context.getSourceCode()

let nodes: SortingNode[] = node.body.map(element => {
let nodes: SortingNode[] = node.body.body.map(element => {
let name: string

if (element.type === AST_NODE_TYPES.TSPropertySignature) {
Expand Down
38 changes: 38 additions & 0 deletions test/sort-interfaces.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1320,5 +1320,43 @@ describe(RULE_NAME, () => {
],
})
})

it(`${RULE_NAME}: allows to ignore interfaces`, () => {
ruleTester.run(RULE_NAME, rule, {
valid: [
{
code: dedent`
interface UiDiclonius {
name: 'Lucy' | 'Nyu'
type: 'diclonius'
}
`,
options: [
{
type: SortType['line-length'],
order: SortOrder.desc,
'ignore-pattern': ['Ui*'],
},
],
},
{
code: dedent`
interface UiDiclonius {
type: 'diclonius'
name: 'Lucy' | 'Nyu'
}
`,
options: [
{
type: SortType['line-length'],
order: SortOrder.desc,
'ignore-pattern': ['Ui*'],
},
],
},
],
invalid: [],
})
})
})
})

0 comments on commit 9aaf08a

Please sign in to comment.