-
-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
471 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# `check-template-names` | ||
|
||
Checks that any `@template` names are actually used in the connected | ||
`@typedef` or type alias. | ||
|
||
Currently checks `TSTypeAliasDeclaration` such as: | ||
|
||
```ts | ||
/** | ||
* @template D | ||
* @template V | ||
*/ | ||
export type Pairs<D, V> = [D, V | undefined]; | ||
``` | ||
|
||
or | ||
|
||
```js | ||
/** | ||
* @template D | ||
* @template V | ||
* @typedef {[D, V | undefined]} Pairs | ||
*/ | ||
``` | ||
|
||
||| | ||
|---|---| | ||
|Context|everywhere| | ||
|Tags|`@template`| | ||
|Recommended|false| | ||
|Settings|| | ||
|Options|| | ||
|
||
## Failing examples | ||
|
||
<!-- assertions-failing checkTemplateNames --> | ||
|
||
## Passing examples | ||
|
||
<!-- assertions-passing checkTemplateNames --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
<a name="user-content-check-template-names"></a> | ||
<a name="check-template-names"></a> | ||
# <code>check-template-names</code> | ||
|
||
Checks that any `@template` names are actually used in the connected | ||
`@typedef` or type alias. | ||
|
||
Currently checks `TSTypeAliasDeclaration` such as: | ||
|
||
```ts | ||
/** | ||
* @template D | ||
* @template V | ||
*/ | ||
export type Pairs<D, V> = [D, V | undefined]; | ||
``` | ||
|
||
or | ||
|
||
```js | ||
/** | ||
* @template D | ||
* @template V | ||
* @typedef {[D, V | undefined]} Pairs | ||
*/ | ||
``` | ||
|
||
||| | ||
|---|---| | ||
|Context|everywhere| | ||
|Tags|`@template`| | ||
|Recommended|false| | ||
|Settings|| | ||
|Options|| | ||
|
||
<a name="user-content-check-template-names-failing-examples"></a> | ||
<a name="check-template-names-failing-examples"></a> | ||
## Failing examples | ||
|
||
The following patterns are considered problems: | ||
|
||
````js | ||
/** | ||
* @template D | ||
* @template V | ||
*/ | ||
type Pairs<X, Y> = [X, Y | undefined]; | ||
// Message: @template D not in use | ||
|
||
/** | ||
* @template D | ||
* @template V | ||
*/ | ||
export type Pairs<X, Y> = [X, Y | undefined]; | ||
// Message: @template D not in use | ||
|
||
/** | ||
* @template D | ||
* @template V | ||
* @typedef {[X, Y | undefined]} Pairs | ||
*/ | ||
// Message: @template D not in use | ||
|
||
/** | ||
* @template D | ||
* @template V | ||
*/ | ||
export type Pairs = [number, undefined]; | ||
// Message: @template D not in use | ||
|
||
/** | ||
* @template D | ||
* @template V | ||
* @typedef {[undefined]} Pairs | ||
*/ | ||
// Settings: {"jsdoc":{"mode":"permissive"}} | ||
// Message: @template D not in use | ||
|
||
/** | ||
* @template D, U, V | ||
*/ | ||
export type Extras<D, U> = [D, U | undefined]; | ||
// Message: @template V not in use | ||
|
||
/** | ||
* @template D, U, V | ||
* @typedef {[D, U | undefined]} Extras | ||
*/ | ||
// Message: @template V not in use | ||
```` | ||
|
||
|
||
|
||
<a name="user-content-check-template-names-passing-examples"></a> | ||
<a name="check-template-names-passing-examples"></a> | ||
## Passing examples | ||
|
||
The following patterns are not considered problems: | ||
|
||
````js | ||
/** | ||
* @template D | ||
* @template V | ||
*/ | ||
export type Pairs<D, V> = [D, V | undefined]; | ||
|
||
/** | ||
* @template D | ||
* @template V | ||
* @typedef {[D, V | undefined]} Pairs | ||
*/ | ||
|
||
/** | ||
* @template D, U, V | ||
*/ | ||
export type Extras<D, U, V> = [D, U, V | undefined]; | ||
|
||
/** | ||
* @template D, U, V | ||
* @typedef {[D, U, V | undefined]} Extras | ||
*/ | ||
|
||
/** | ||
* @template X | ||
* @typedef {[D, U, V | undefined]} Extras | ||
* @typedef {[D, U, V | undefined]} Extras | ||
*/ | ||
```` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { | ||
parse as parseType, | ||
traverse, | ||
tryParse as tryParseType, | ||
} from '@es-joy/jsdoccomment'; | ||
import iterateJsdoc from '../iterateJsdoc.js'; | ||
|
||
export default iterateJsdoc(({ | ||
context, | ||
utils, | ||
node, | ||
settings, | ||
report, | ||
}) => { | ||
const { | ||
mode | ||
} = settings; | ||
|
||
const templateTags = utils.getTags('template'); | ||
|
||
const usedNames = new Set(); | ||
/** | ||
* @param {import('@typescript-eslint/types').TSESTree.TSTypeAliasDeclaration} aliasDeclaration | ||
*/ | ||
const checkParameters = (aliasDeclaration) => { | ||
/* c8 ignore next -- Guard */ | ||
const {params} = aliasDeclaration.typeParameters ?? {params: []}; | ||
for (const {name: {name}} of params) { | ||
usedNames.add(name); | ||
} | ||
for (const tag of templateTags) { | ||
const {name} = tag; | ||
const names = name.split(/,\s*/); | ||
for (const name of names) { | ||
if (!usedNames.has(name)) { | ||
report(`@template ${name} not in use`, null, tag); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
const handleTypeAliases = () => { | ||
const nde = /** @type {import('@typescript-eslint/types').TSESTree.Node} */ ( | ||
node | ||
); | ||
if (!nde) { | ||
return; | ||
} | ||
switch (nde.type) { | ||
case 'ExportNamedDeclaration': | ||
if (nde.declaration?.type === 'TSTypeAliasDeclaration') { | ||
checkParameters(nde.declaration); | ||
} | ||
break; | ||
case 'TSTypeAliasDeclaration': | ||
checkParameters(nde); | ||
break; | ||
} | ||
}; | ||
|
||
const typedefTags = utils.getTags('typedef'); | ||
if (!typedefTags.length || typedefTags.length >= 2) { | ||
handleTypeAliases(); | ||
return; | ||
} | ||
|
||
const potentialType = typedefTags[0].type; | ||
const parsedType = mode === 'permissive' ? | ||
tryParseType(/** @type {string} */ (potentialType)) : | ||
parseType(/** @type {string} */ (potentialType), mode) | ||
|
||
traverse(parsedType, (nde) => { | ||
const { | ||
type, | ||
value, | ||
} = /** @type {import('jsdoc-type-pratt-parser').NameResult} */ (nde); | ||
if (type === 'JsdocTypeName' && (/^[A-Z]$/).test(value)) { | ||
usedNames.add(value); | ||
} | ||
}); | ||
|
||
for (const tag of templateTags) { | ||
const {name} = tag; | ||
const names = name.split(/,\s*/); | ||
for (const name of names) { | ||
if (!usedNames.has(name)) { | ||
report(`@template ${name} not in use`, null, tag); | ||
} | ||
} | ||
} | ||
}, { | ||
iterateAllJsdocs: true, | ||
meta: { | ||
docs: { | ||
description: 'Checks that any `@template` names are actually used in the connected `@typedef` or type alias.', | ||
url: 'https://github.com/gajus/eslint-plugin-jsdoc/blob/main/docs/rules/require-template.md#repos-sticky-header', | ||
}, | ||
schema: [], | ||
type: 'suggestion', | ||
}, | ||
}); |
Oops, something went wrong.