Skip to content

Commit aed0460

Browse files
117616: Created custom sort-standalone-imports rule
1 parent 6017537 commit aed0460

File tree

5 files changed

+532
-0
lines changed

5 files changed

+532
-0
lines changed

.eslintrc.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,15 @@
295295
"listableObjectComponent"
296296
]
297297
}
298+
],
299+
"dspace-angular-ts/sort-standalone-imports": [
300+
"error",
301+
{
302+
"locale": "en-US",
303+
"maxItems": 0,
304+
"indent": 2,
305+
"trailingComma": true
306+
}
298307
]
299308
}
300309
},

docs/lint/ts/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
_______
33

44
- [`dspace-angular-ts/alias-imports`](./rules/alias-imports.md): Unclear imports should be aliased for clarity
5+
- [`dspace-angular-ts/sort-standalone-imports`](./rules/sort-standalone-imports.md): Sorts the standalone `@Component` imports alphabetically
56
- [`dspace-angular-ts/themed-component-classes`](./rules/themed-component-classes.md): Formatting rules for themeable component classes
67
- [`dspace-angular-ts/themed-component-selectors`](./rules/themed-component-selectors.md): Themeable component selectors should follow the DSpace convention
78
- [`dspace-angular-ts/themed-component-usages`](./rules/themed-component-usages.md): Themeable components should be used via their `ThemedComponent` wrapper class
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
[DSpace ESLint plugins](../../../../lint/README.md) > [TypeScript rules](../index.md) > `dspace-angular-ts/sort-standalone-imports`
2+
_______
3+
4+
Sorts the standalone `@Component` imports alphabetically
5+
6+
_______
7+
8+
[Source code](../../../../lint/src/rules/ts/sort-standalone-imports.ts)
9+
10+
11+
### Options
12+
13+
#### `locale`
14+
15+
The locale used to sort the imports.,
16+
#### `maxItems`
17+
18+
The maximum number of imports that should be displayed before each import is separated onto its own line.,
19+
#### `indent`
20+
21+
The indent used for the project.,
22+
#### `trailingComma`
23+
24+
Whether the last import should have a trailing comma (only applicable for multiline imports).
25+
26+
27+
### Examples
28+
29+
30+
#### Valid code
31+
32+
##### should sort multiple imports on separate lines
33+
34+
```typescript
35+
@Component({
36+
selector: 'ds-app',
37+
templateUrl: './app.component.html',
38+
styleUrls: ['./app.component.scss'],
39+
standalone: true,
40+
imports: [
41+
AsyncPipe,
42+
RootComponent,
43+
],
44+
})
45+
export class AppComponent {}
46+
```
47+
48+
##### should not inlines singular imports when maxItems is 0
49+
50+
```typescript
51+
@Component({
52+
selector: 'ds-app',
53+
templateUrl: './app.component.html',
54+
styleUrls: ['./app.component.scss'],
55+
standalone: true,
56+
imports: [
57+
RootComponent,
58+
],
59+
})
60+
export class AppComponent {}
61+
```
62+
63+
##### should inline singular imports when maxItems is 1
64+
65+
```typescript
66+
@Component({
67+
selector: 'ds-app',
68+
templateUrl: './app.component.html',
69+
styleUrls: ['./app.component.scss'],
70+
standalone: true,
71+
imports: [RootComponent],
72+
})
73+
export class AppComponent {}
74+
```
75+
76+
77+
78+
79+
#### Invalid code & automatic fixes
80+
81+
##### should sort multiple imports alphabetically
82+
83+
```typescript
84+
@Component({
85+
selector: 'ds-app',
86+
templateUrl: './app.component.html',
87+
styleUrls: ['./app.component.scss'],
88+
standalone: true,
89+
imports: [
90+
RootComponent,
91+
AsyncPipe,
92+
],
93+
})
94+
export class AppComponent {}
95+
```
96+
Will produce the following error(s):
97+
```
98+
Standalone imports should be sorted alphabetically
99+
```
100+
101+
Result of `yarn lint --fix`:
102+
```typescript
103+
@Component({
104+
selector: 'ds-app',
105+
templateUrl: './app.component.html',
106+
styleUrls: ['./app.component.scss'],
107+
standalone: true,
108+
imports: [
109+
AsyncPipe,
110+
RootComponent,
111+
],
112+
})
113+
export class AppComponent {}
114+
```
115+
116+
117+
##### should not put singular imports on one line when maxItems is 0
118+
119+
```typescript
120+
@Component({
121+
selector: 'ds-app',
122+
templateUrl: './app.component.html',
123+
styleUrls: ['./app.component.scss'],
124+
standalone: true,
125+
imports: [RootComponent],
126+
})
127+
export class AppComponent {}
128+
```
129+
Will produce the following error(s):
130+
```
131+
Standalone imports should be sorted alphabetically
132+
```
133+
134+
Result of `yarn lint --fix`:
135+
```typescript
136+
@Component({
137+
selector: 'ds-app',
138+
templateUrl: './app.component.html',
139+
styleUrls: ['./app.component.scss'],
140+
standalone: true,
141+
imports: [
142+
RootComponent,
143+
],
144+
})
145+
export class AppComponent {}
146+
```
147+
148+
149+
##### should not put singular imports on a separate line when maxItems is 1
150+
151+
```typescript
152+
@Component({
153+
selector: 'ds-app',
154+
templateUrl: './app.component.html',
155+
styleUrls: ['./app.component.scss'],
156+
standalone: true,
157+
imports: [
158+
RootComponent,
159+
],
160+
})
161+
export class AppComponent {}
162+
```
163+
Will produce the following error(s):
164+
```
165+
Standalone imports should be sorted alphabetically
166+
```
167+
168+
Result of `yarn lint --fix`:
169+
```typescript
170+
@Component({
171+
selector: 'ds-app',
172+
templateUrl: './app.component.html',
173+
styleUrls: ['./app.component.scss'],
174+
standalone: true,
175+
imports: [RootComponent],
176+
})
177+
export class AppComponent {}
178+
```
179+
180+
181+
##### should not display multiple imports on the same line
182+
183+
```typescript
184+
@Component({
185+
selector: 'ds-app',
186+
templateUrl: './app.component.html',
187+
styleUrls: ['./app.component.scss'],
188+
standalone: true,
189+
imports: [AsyncPipe, RootComponent],
190+
})
191+
export class AppComponent {}
192+
```
193+
Will produce the following error(s):
194+
```
195+
Standalone imports should be sorted alphabetically
196+
```
197+
198+
Result of `yarn lint --fix`:
199+
```typescript
200+
@Component({
201+
selector: 'ds-app',
202+
templateUrl: './app.component.html',
203+
styleUrls: ['./app.component.scss'],
204+
standalone: true,
205+
imports: [
206+
AsyncPipe,
207+
RootComponent,
208+
],
209+
})
210+
export class AppComponent {}
211+
```
212+
213+
214+

lint/src/rules/ts/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
} from '../../util/structure';
1212
/* eslint-disable import/no-namespace */
1313
import * as aliasImports from './alias-imports';
14+
import * as sortStandaloneImports from './sort-standalone-imports';
1415
import * as themedComponentClasses from './themed-component-classes';
1516
import * as themedComponentSelectors from './themed-component-selectors';
1617
import * as themedComponentUsages from './themed-component-usages';
@@ -20,6 +21,7 @@ import * as uniqueDecorators from './unique-decorators';
2021

2122
const index = [
2223
aliasImports,
24+
sortStandaloneImports,
2325
themedComponentClasses,
2426
themedComponentSelectors,
2527
themedComponentUsages,

0 commit comments

Comments
 (0)