Skip to content

Commit

Permalink
feat(require-template, check-template-names): add support `ClassD…
Browse files Browse the repository at this point in the history
…eclaration`
  • Loading branch information
brettz9 committed Jul 30, 2024
1 parent 28bc1cb commit 31d3255
Show file tree
Hide file tree
Showing 8 changed files with 447 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .README/rules/check-template-names.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
Checks that any `@template` names are actually used in the connected
`@typedef` or type alias.

Currently checks `FunctionDeclaration`, `TSInterfaceDeclaration` or
`TSTypeAliasDeclaration` such as:
Currently checks `ClassDeclaration`, `FunctionDeclaration`,
`TSInterfaceDeclaration` or `TSTypeAliasDeclaration` such as:

```ts
/**
Expand Down
4 changes: 2 additions & 2 deletions .README/rules/require-template.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
Checks to see that `@template` tags are present for any detected type
parameters.

Currently checks `FunctionDeclaration`, `TSInterfaceDeclaration` or
`TSTypeAliasDeclaration` such as:
Currently checks `ClassDeclaration`, `FunctionDeclaration`,
`TSInterfaceDeclaration` or `TSTypeAliasDeclaration` such as:

```ts
export type Pairs<D, V> = [D, V | undefined];
Expand Down
76 changes: 74 additions & 2 deletions docs/rules/check-template-names.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
Checks that any `@template` names are actually used in the connected
`@typedef` or type alias.

Currently checks `FunctionDeclaration`, `TSInterfaceDeclaration` or
`TSTypeAliasDeclaration` such as:
Currently checks `ClassDeclaration`, `FunctionDeclaration`,
`TSInterfaceDeclaration` or `TSTypeAliasDeclaration` such as:

```ts
/**
Expand Down Expand Up @@ -150,6 +150,46 @@ export default function identity<Type>(arg: Type): Type {
return arg;
}
// Message: @template D not in use

/**
* @template D
* @template V
*/
class GenericNumber<NumType> {
zeroValue: NumType;
add: (x: NumType, y: NumType) => NumType;
}
// Message: @template D not in use

/**
* @template D
* @template V
*/
export class GenericNumber<NumType> {
zeroValue: NumType;
add: (x: NumType, y: NumType) => NumType;
}
// Message: @template D not in use

/**
* @template D
* @template V
*/
export default class GenericNumber<NumType> {
zeroValue: NumType;
add: (x: NumType, y: NumType) => NumType;
}
// Message: @template D not in use

/**
* @template D
* @template V
*/
export default class <NumType> {
zeroValue: NumType;
add: (x: NumType, y: NumType) => NumType;
}
// Message: @template D not in use
````


Expand Down Expand Up @@ -243,5 +283,37 @@ export function identity<Type>(arg: Type): Type {
export default function identity<Type>(arg: Type): Type {
return arg;
}

/**
* @template NumType
*/
class GenericNumber<NumType> {
zeroValue: NumType;
add: (x: NumType, y: NumType) => NumType;
}

/**
* @template NumType
*/
export class GenericNumber<NumType> {
zeroValue: NumType;
add: (x: NumType, y: NumType) => NumType;
}

/**
* @template NumType
*/
export default class GenericNumber<NumType> {
zeroValue: NumType;
add: (x: NumType, y: NumType) => NumType;
}

/**
* @template NumType
*/
export default class <NumType> {
zeroValue: NumType;
add: (x: NumType, y: NumType) => NumType;
}
````

72 changes: 70 additions & 2 deletions docs/rules/require-template.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
Checks to see that `@template` tags are present for any detected type
parameters.

Currently checks `FunctionDeclaration`, `TSInterfaceDeclaration` or
`TSTypeAliasDeclaration` such as:
Currently checks `ClassDeclaration`, `FunctionDeclaration`,
`TSInterfaceDeclaration` or `TSTypeAliasDeclaration` such as:

```ts
export type Pairs<D, V> = [D, V | undefined];
Expand Down Expand Up @@ -163,6 +163,42 @@ export default function identity<Type>(arg: Type): Type {
return arg;
}
// Message: Missing @template Type

/**
*
*/
class GenericNumber<NumType> {
zeroValue: NumType;
add: (x: NumType, y: NumType) => NumType;
}
// Message: Missing @template NumType

/**
*
*/
export class GenericNumber<NumType> {
zeroValue: NumType;
add: (x: NumType, y: NumType) => NumType;
}
// Message: Missing @template NumType

/**
*
*/
export default class GenericNumber<NumType> {
zeroValue: NumType;
add: (x: NumType, y: NumType) => NumType;
}
// Message: Missing @template NumType

/**
*
*/
export default class <NumType> {
zeroValue: NumType;
add: (x: NumType, y: NumType) => NumType;
}
// Message: Missing @template NumType
````


Expand Down Expand Up @@ -255,5 +291,37 @@ export function identity<Type>(arg: Type): Type {
export default function identity<Type>(arg: Type): Type {
return arg;
}

/**
* @template NumType
*/
class GenericNumber<NumType> {
zeroValue: NumType;
add: (x: NumType, y: NumType) => NumType;
}

/**
* @template NumType
*/
export class GenericNumber<NumType> {
zeroValue: NumType;
add: (x: NumType, y: NumType) => NumType;
}

/**
* @template NumType
*/
export default class GenericNumber<NumType> {
zeroValue: NumType;
add: (x: NumType, y: NumType) => NumType;
}

/**
* @template NumType
*/
export default class <NumType> {
zeroValue: NumType;
add: (x: NumType, y: NumType) => NumType;
}
````

3 changes: 3 additions & 0 deletions src/rules/checkTemplateNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default iterateJsdoc(({
const usedNames = new Set();
/**
* @param {import('@typescript-eslint/types').TSESTree.FunctionDeclaration|
* import('@typescript-eslint/types').TSESTree.ClassDeclaration|
* import('@typescript-eslint/types').TSESTree.TSInterfaceDeclaration|
* import('@typescript-eslint/types').TSESTree.TSTypeAliasDeclaration} aliasDeclaration
*/
Expand Down Expand Up @@ -52,13 +53,15 @@ export default iterateJsdoc(({
case 'ExportDefaultDeclaration':
case 'ExportNamedDeclaration':
switch (nde.declaration?.type) {
case 'ClassDeclaration':
case 'FunctionDeclaration':
case 'TSTypeAliasDeclaration':
case 'TSInterfaceDeclaration':
checkParameters(nde.declaration);
break;
}
break;
case 'ClassDeclaration':
case 'FunctionDeclaration':
case 'TSTypeAliasDeclaration':
case 'TSInterfaceDeclaration':
Expand Down
4 changes: 4 additions & 0 deletions src/rules/requireTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export default iterateJsdoc(({

/**
* @param {import('@typescript-eslint/types').TSESTree.FunctionDeclaration|
* import('@typescript-eslint/types').TSESTree.ClassDeclaration|
* import('@typescript-eslint/types').TSESTree.TSInterfaceDeclaration|
* import('@typescript-eslint/types').TSESTree.TSTypeAliasDeclaration} aliasDeclaration
*/
Expand All @@ -62,6 +63,7 @@ export default iterateJsdoc(({
switch (nde.type) {
case 'ExportDefaultDeclaration':
switch (nde.declaration?.type) {
case 'ClassDeclaration':
case 'FunctionDeclaration':
case 'TSInterfaceDeclaration':
checkTypeParams(nde.declaration);
Expand All @@ -70,13 +72,15 @@ export default iterateJsdoc(({
break;
case 'ExportNamedDeclaration':
switch (nde.declaration?.type) {
case 'ClassDeclaration':
case 'FunctionDeclaration':
case 'TSTypeAliasDeclaration':
case 'TSInterfaceDeclaration':
checkTypeParams(nde.declaration);
break;
}
break;
case 'ClassDeclaration':
case 'FunctionDeclaration':
case 'TSTypeAliasDeclaration':
case 'TSInterfaceDeclaration':
Expand Down
Loading

0 comments on commit 31d3255

Please sign in to comment.