Skip to content

Commit 8f6f21d

Browse files
authored
feat(sort-import-attributes): support conditional config by ast selector and name pattern
1 parent 1d3dd30 commit 8f6f21d

11 files changed

Lines changed: 1160 additions & 344 deletions

docs/content/rules/sort-export-attributes.mdx

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,64 @@ or [`customGroups`](#customgroups) options.
188188

189189
This option is only applicable when [`partitionByNewLine`](#partitionbynewline) is `false`.
190190

191+
### useConfigurationIf
192+
193+
<sub>
194+
type:
195+
```ts
196+
{
197+
allNamesMatchPattern?:
198+
| string
199+
| string[]
200+
| { pattern: string; flags: string }
201+
| { pattern: string; flags: string }[]
202+
matchesAstSelector?: string
203+
}
204+
```
205+
</sub>
206+
<sub>default: `{}`</sub>
207+
208+
Specifies filters to match a particular options configuration for a given export.
209+
210+
The first matching options configuration will be used. If no configuration matches, the default options configuration will be used.
211+
212+
- `allNamesMatchPattern` — A regexp pattern that all export attributes must match.
213+
214+
Example configuration:
215+
```ts
216+
{
217+
'perfectionist/sort-export-attributes': [
218+
'error',
219+
{
220+
groups: ['r', 'g', 'b'], // Sort colors by RGB
221+
customGroups: [
222+
{
223+
elementNamePattern: '^r$',
224+
groupName: 'r',
225+
},
226+
{
227+
elementNamePattern: '^g$',
228+
groupName: 'g',
229+
},
230+
{
231+
elementNamePattern: '^b$',
232+
groupName: 'b',
233+
},
234+
],
235+
useConfigurationIf: {
236+
allNamesMatchPattern: '^[rgb]$',
237+
},
238+
},
239+
{
240+
type: 'alphabetical' // Fallback configuration
241+
}
242+
],
243+
}
244+
```
245+
246+
- `matchesAstSelector` — An [AST selector](https://eslint.org/docs/latest/extend/selectors) matching an `ExportNamedDeclaration` node.
247+
To avoid unexpected behavior, do not use `:exit` or `:enter` pseudo-selectors.
248+
191249
### groups
192250

193251
<sub>
@@ -309,6 +367,7 @@ An attribute matches a custom group when its name satisfies `elementNamePattern`
309367
partitionByNewLine: false,
310368
newlinesBetween: 'ignore',
311369
newlinesInside: 'ignore',
370+
useConfigurationIf: {},
312371
groups: [],
313372
customGroups: [],
314373
},
@@ -340,6 +399,7 @@ An attribute matches a custom group when its name satisfies `elementNamePattern`
340399
partitionByNewLine: false,
341400
newlinesBetween: 'ignore',
342401
newlinesInside: 'ignore',
402+
useConfigurationIf: {},
343403
groups: [],
344404
customGroups: [],
345405
},

docs/content/rules/sort-import-attributes.mdx

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,64 @@ or [`customGroups`](#customgroups) options.
188188

189189
This option is only applicable when [`partitionByNewLine`](#partitionbynewline) is `false`.
190190

191+
### useConfigurationIf
192+
193+
<sub>
194+
type:
195+
```ts
196+
{
197+
allNamesMatchPattern?:
198+
| string
199+
| string[]
200+
| { pattern: string; flags: string }
201+
| { pattern: string; flags: string }[]
202+
matchesAstSelector?: string
203+
}
204+
```
205+
</sub>
206+
<sub>default: `{}`</sub>
207+
208+
Specifies filters to match a particular options configuration for a given import.
209+
210+
The first matching options configuration will be used. If no configuration matches, the default options configuration will be used.
211+
212+
- `allNamesMatchPattern` — A regexp pattern that all import attributes must match.
213+
214+
Example configuration:
215+
```ts
216+
{
217+
'perfectionist/sort-import-attributes': [
218+
'error',
219+
{
220+
groups: ['r', 'g', 'b'], // Sort colors by RGB
221+
customGroups: [
222+
{
223+
elementNamePattern: '^r$',
224+
groupName: 'r',
225+
},
226+
{
227+
elementNamePattern: '^g$',
228+
groupName: 'g',
229+
},
230+
{
231+
elementNamePattern: '^b$',
232+
groupName: 'b',
233+
},
234+
],
235+
useConfigurationIf: {
236+
allNamesMatchPattern: '^[rgb]$',
237+
},
238+
},
239+
{
240+
type: 'alphabetical' // Fallback configuration
241+
}
242+
],
243+
}
244+
```
245+
246+
- `matchesAstSelector` — An [AST selector](https://eslint.org/docs/latest/extend/selectors) matching an `ImportDeclaration` node.
247+
To avoid unexpected behavior, do not use `:exit` or `:enter` pseudo-selectors.
248+
191249
### groups
192250

193251
<sub>
@@ -309,6 +367,7 @@ An attribute matches a custom group when its name satisfies `elementNamePattern`
309367
partitionByNewLine: false,
310368
newlinesBetween: 'ignore',
311369
newlinesInside: 'ignore',
370+
useConfigurationIf: {},
312371
groups: [],
313372
customGroups: [],
314373
},
@@ -340,6 +399,7 @@ An attribute matches a custom group when its name satisfies `elementNamePattern`
340399
partitionByNewLine: false,
341400
newlinesBetween: 'ignore',
342401
newlinesInside: 'ignore',
402+
useConfigurationIf: {},
343403
groups: [],
344404
customGroups: [],
345405
},

0 commit comments

Comments
 (0)