Skip to content

Commit 31d3255

Browse files
committed
feat(require-template, check-template-names): add support ClassDeclaration
1 parent 28bc1cb commit 31d3255

File tree

8 files changed

+447
-8
lines changed

8 files changed

+447
-8
lines changed

.README/rules/check-template-names.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
Checks that any `@template` names are actually used in the connected
44
`@typedef` or type alias.
55

6-
Currently checks `FunctionDeclaration`, `TSInterfaceDeclaration` or
7-
`TSTypeAliasDeclaration` such as:
6+
Currently checks `ClassDeclaration`, `FunctionDeclaration`,
7+
`TSInterfaceDeclaration` or `TSTypeAliasDeclaration` such as:
88

99
```ts
1010
/**

.README/rules/require-template.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
Checks to see that `@template` tags are present for any detected type
44
parameters.
55

6-
Currently checks `FunctionDeclaration`, `TSInterfaceDeclaration` or
7-
`TSTypeAliasDeclaration` such as:
6+
Currently checks `ClassDeclaration`, `FunctionDeclaration`,
7+
`TSInterfaceDeclaration` or `TSTypeAliasDeclaration` such as:
88

99
```ts
1010
export type Pairs<D, V> = [D, V | undefined];

docs/rules/check-template-names.md

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
Checks that any `@template` names are actually used in the connected
66
`@typedef` or type alias.
77

8-
Currently checks `FunctionDeclaration`, `TSInterfaceDeclaration` or
9-
`TSTypeAliasDeclaration` such as:
8+
Currently checks `ClassDeclaration`, `FunctionDeclaration`,
9+
`TSInterfaceDeclaration` or `TSTypeAliasDeclaration` such as:
1010

1111
```ts
1212
/**
@@ -150,6 +150,46 @@ export default function identity<Type>(arg: Type): Type {
150150
return arg;
151151
}
152152
// Message: @template D not in use
153+
154+
/**
155+
* @template D
156+
* @template V
157+
*/
158+
class GenericNumber<NumType> {
159+
zeroValue: NumType;
160+
add: (x: NumType, y: NumType) => NumType;
161+
}
162+
// Message: @template D not in use
163+
164+
/**
165+
* @template D
166+
* @template V
167+
*/
168+
export class GenericNumber<NumType> {
169+
zeroValue: NumType;
170+
add: (x: NumType, y: NumType) => NumType;
171+
}
172+
// Message: @template D not in use
173+
174+
/**
175+
* @template D
176+
* @template V
177+
*/
178+
export default class GenericNumber<NumType> {
179+
zeroValue: NumType;
180+
add: (x: NumType, y: NumType) => NumType;
181+
}
182+
// Message: @template D not in use
183+
184+
/**
185+
* @template D
186+
* @template V
187+
*/
188+
export default class <NumType> {
189+
zeroValue: NumType;
190+
add: (x: NumType, y: NumType) => NumType;
191+
}
192+
// Message: @template D not in use
153193
````
154194

155195

@@ -243,5 +283,37 @@ export function identity<Type>(arg: Type): Type {
243283
export default function identity<Type>(arg: Type): Type {
244284
return arg;
245285
}
286+
287+
/**
288+
* @template NumType
289+
*/
290+
class GenericNumber<NumType> {
291+
zeroValue: NumType;
292+
add: (x: NumType, y: NumType) => NumType;
293+
}
294+
295+
/**
296+
* @template NumType
297+
*/
298+
export class GenericNumber<NumType> {
299+
zeroValue: NumType;
300+
add: (x: NumType, y: NumType) => NumType;
301+
}
302+
303+
/**
304+
* @template NumType
305+
*/
306+
export default class GenericNumber<NumType> {
307+
zeroValue: NumType;
308+
add: (x: NumType, y: NumType) => NumType;
309+
}
310+
311+
/**
312+
* @template NumType
313+
*/
314+
export default class <NumType> {
315+
zeroValue: NumType;
316+
add: (x: NumType, y: NumType) => NumType;
317+
}
246318
````
247319

docs/rules/require-template.md

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
Checks to see that `@template` tags are present for any detected type
66
parameters.
77

8-
Currently checks `FunctionDeclaration`, `TSInterfaceDeclaration` or
9-
`TSTypeAliasDeclaration` such as:
8+
Currently checks `ClassDeclaration`, `FunctionDeclaration`,
9+
`TSInterfaceDeclaration` or `TSTypeAliasDeclaration` such as:
1010

1111
```ts
1212
export type Pairs<D, V> = [D, V | undefined];
@@ -163,6 +163,42 @@ export default function identity<Type>(arg: Type): Type {
163163
return arg;
164164
}
165165
// Message: Missing @template Type
166+
167+
/**
168+
*
169+
*/
170+
class GenericNumber<NumType> {
171+
zeroValue: NumType;
172+
add: (x: NumType, y: NumType) => NumType;
173+
}
174+
// Message: Missing @template NumType
175+
176+
/**
177+
*
178+
*/
179+
export class GenericNumber<NumType> {
180+
zeroValue: NumType;
181+
add: (x: NumType, y: NumType) => NumType;
182+
}
183+
// Message: Missing @template NumType
184+
185+
/**
186+
*
187+
*/
188+
export default class GenericNumber<NumType> {
189+
zeroValue: NumType;
190+
add: (x: NumType, y: NumType) => NumType;
191+
}
192+
// Message: Missing @template NumType
193+
194+
/**
195+
*
196+
*/
197+
export default class <NumType> {
198+
zeroValue: NumType;
199+
add: (x: NumType, y: NumType) => NumType;
200+
}
201+
// Message: Missing @template NumType
166202
````
167203

168204

@@ -255,5 +291,37 @@ export function identity<Type>(arg: Type): Type {
255291
export default function identity<Type>(arg: Type): Type {
256292
return arg;
257293
}
294+
295+
/**
296+
* @template NumType
297+
*/
298+
class GenericNumber<NumType> {
299+
zeroValue: NumType;
300+
add: (x: NumType, y: NumType) => NumType;
301+
}
302+
303+
/**
304+
* @template NumType
305+
*/
306+
export class GenericNumber<NumType> {
307+
zeroValue: NumType;
308+
add: (x: NumType, y: NumType) => NumType;
309+
}
310+
311+
/**
312+
* @template NumType
313+
*/
314+
export default class GenericNumber<NumType> {
315+
zeroValue: NumType;
316+
add: (x: NumType, y: NumType) => NumType;
317+
}
318+
319+
/**
320+
* @template NumType
321+
*/
322+
export default class <NumType> {
323+
zeroValue: NumType;
324+
add: (x: NumType, y: NumType) => NumType;
325+
}
258326
````
259327

src/rules/checkTemplateNames.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export default iterateJsdoc(({
2121
const usedNames = new Set();
2222
/**
2323
* @param {import('@typescript-eslint/types').TSESTree.FunctionDeclaration|
24+
* import('@typescript-eslint/types').TSESTree.ClassDeclaration|
2425
* import('@typescript-eslint/types').TSESTree.TSInterfaceDeclaration|
2526
* import('@typescript-eslint/types').TSESTree.TSTypeAliasDeclaration} aliasDeclaration
2627
*/
@@ -52,13 +53,15 @@ export default iterateJsdoc(({
5253
case 'ExportDefaultDeclaration':
5354
case 'ExportNamedDeclaration':
5455
switch (nde.declaration?.type) {
56+
case 'ClassDeclaration':
5557
case 'FunctionDeclaration':
5658
case 'TSTypeAliasDeclaration':
5759
case 'TSInterfaceDeclaration':
5860
checkParameters(nde.declaration);
5961
break;
6062
}
6163
break;
64+
case 'ClassDeclaration':
6265
case 'FunctionDeclaration':
6366
case 'TSTypeAliasDeclaration':
6467
case 'TSInterfaceDeclaration':

src/rules/requireTemplate.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export default iterateJsdoc(({
3636

3737
/**
3838
* @param {import('@typescript-eslint/types').TSESTree.FunctionDeclaration|
39+
* import('@typescript-eslint/types').TSESTree.ClassDeclaration|
3940
* import('@typescript-eslint/types').TSESTree.TSInterfaceDeclaration|
4041
* import('@typescript-eslint/types').TSESTree.TSTypeAliasDeclaration} aliasDeclaration
4142
*/
@@ -62,6 +63,7 @@ export default iterateJsdoc(({
6263
switch (nde.type) {
6364
case 'ExportDefaultDeclaration':
6465
switch (nde.declaration?.type) {
66+
case 'ClassDeclaration':
6567
case 'FunctionDeclaration':
6668
case 'TSInterfaceDeclaration':
6769
checkTypeParams(nde.declaration);
@@ -70,13 +72,15 @@ export default iterateJsdoc(({
7072
break;
7173
case 'ExportNamedDeclaration':
7274
switch (nde.declaration?.type) {
75+
case 'ClassDeclaration':
7376
case 'FunctionDeclaration':
7477
case 'TSTypeAliasDeclaration':
7578
case 'TSInterfaceDeclaration':
7679
checkTypeParams(nde.declaration);
7780
break;
7881
}
7982
break;
83+
case 'ClassDeclaration':
8084
case 'FunctionDeclaration':
8185
case 'TSTypeAliasDeclaration':
8286
case 'TSInterfaceDeclaration':

0 commit comments

Comments
 (0)