forked from gajus/eslint-plugin-jsdoc
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(
require-template
): add rule; fixes part of gajus#1120
- Loading branch information
Showing
9 changed files
with
538 additions
and
1 deletion.
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
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,54 @@ | ||
# `require-template` | ||
|
||
Checks to see that `@template` tags are present for any detected type | ||
parameters. | ||
|
||
Currently checks `TSTypeAliasDeclaration` such as: | ||
|
||
```ts | ||
export type Pairs<D, V> = [D, V | undefined]; | ||
``` | ||
|
||
or | ||
|
||
```js | ||
/** | ||
* @typedef {[D, V | undefined]} Pairs | ||
*/ | ||
``` | ||
|
||
Note that in the latter TypeScript-flavor JavaScript example, there is no way | ||
for us to firmly distinguish between `D` and `V` as type parameters or as some | ||
other identifiers, so we use an algorithm that any single capital letters | ||
are assumed to be templates. | ||
|
||
## Options | ||
|
||
### `requireSeparateTemplates` | ||
|
||
Requires that each template have its own separate line, i.e., preventing | ||
templates of this format: | ||
|
||
```js | ||
/** | ||
* @template T, U, V | ||
*/ | ||
``` | ||
|
||
Defaults to `false`. | ||
|
||
||| | ||
|---|---| | ||
|Context|everywhere| | ||
|Tags|`template`| | ||
|Recommended|true| | ||
|Settings|| | ||
|Options|`requireSeparateTemplates`| | ||
|
||
## Failing examples | ||
|
||
<!-- assertions-failing requireTemplate --> | ||
|
||
## Passing examples | ||
|
||
<!-- assertions-passing requireTemplate --> |
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
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,147 @@ | ||
<a name="user-content-require-template"></a> | ||
<a name="require-template"></a> | ||
# <code>require-template</code> | ||
|
||
Checks to see that `@template` tags are present for any detected type | ||
parameters. | ||
|
||
Currently checks `TSTypeAliasDeclaration` such as: | ||
|
||
```ts | ||
export type Pairs<D, V> = [D, V | undefined]; | ||
``` | ||
|
||
or | ||
|
||
```js | ||
/** | ||
* @typedef {[D, V | undefined]} Pairs | ||
*/ | ||
``` | ||
|
||
Note that in the latter TypeScript-flavor JavaScript example, there is no way | ||
for us to firmly distinguish between `D` and `V` as type parameters or as some | ||
other identifiers, so we use an algorithm that any single capital letters | ||
are assumed to be templates. | ||
|
||
<a name="user-content-require-template-options"></a> | ||
<a name="require-template-options"></a> | ||
## Options | ||
|
||
<a name="user-content-require-template-options-requireseparatetemplates"></a> | ||
<a name="require-template-options-requireseparatetemplates"></a> | ||
### <code>requireSeparateTemplates</code> | ||
|
||
Requires that each template have its own separate line, i.e., preventing | ||
templates of this format: | ||
|
||
```js | ||
/** | ||
* @template T, U, V | ||
*/ | ||
``` | ||
|
||
Defaults to `false`. | ||
|
||
||| | ||
|---|---| | ||
|Context|everywhere| | ||
|Tags|`template`| | ||
|Recommended|true| | ||
|Settings|| | ||
|Options|`requireSeparateTemplates`| | ||
|
||
<a name="user-content-require-template-failing-examples"></a> | ||
<a name="require-template-failing-examples"></a> | ||
## Failing examples | ||
|
||
The following patterns are considered problems: | ||
|
||
````js | ||
/** | ||
* | ||
*/ | ||
type Pairs<D, V> = [D, V | undefined]; | ||
// Message: Missing @template D | ||
|
||
/** | ||
* | ||
*/ | ||
export type Pairs<D, V> = [D, V | undefined]; | ||
// Message: Missing @template D | ||
|
||
/** | ||
* @typedef {[D, V | undefined]} Pairs | ||
*/ | ||
// Message: Missing @template D | ||
|
||
/** | ||
* @typedef {[D, V | undefined]} Pairs | ||
*/ | ||
// Settings: {"jsdoc":{"mode":"permissive"}} | ||
// Message: Missing @template D | ||
|
||
/** | ||
* @template D, U | ||
*/ | ||
export type Extras<D, U, V> = [D, U, V | undefined]; | ||
// Message: Missing @template V | ||
|
||
/** | ||
* @template D, U | ||
* @typedef {[D, U, V | undefined]} Extras | ||
*/ | ||
// Message: Missing @template V | ||
|
||
/** | ||
* @template D, V | ||
*/ | ||
export type Pairs<D, V> = [D, V | undefined]; | ||
// "jsdoc/require-template": ["error"|"warn", {"requireSeparateTemplates":true}] | ||
// Message: Missing separate @template for V | ||
|
||
/** | ||
* @template D, V | ||
* @typedef {[D, V | undefined]} Pairs | ||
*/ | ||
// "jsdoc/require-template": ["error"|"warn", {"requireSeparateTemplates":true}] | ||
// Message: Missing separate @template for V | ||
```` | ||
|
||
|
||
|
||
<a name="user-content-require-template-passing-examples"></a> | ||
<a name="require-template-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 | ||
*/ | ||
|
||
/** | ||
* @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,119 @@ | ||
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 { | ||
requireSeparateTemplates = false, | ||
} = context.options[0] || {}; | ||
|
||
const { | ||
mode | ||
} = settings; | ||
|
||
const usedNames = new Set(); | ||
const templateTags = utils.getTags('template'); | ||
const templateNames = templateTags.flatMap(({name}) => { | ||
return name.split(/,\s*/); | ||
}); | ||
|
||
for (const tag of templateTags) { | ||
const {name} = tag; | ||
const names = name.split(/,\s*/); | ||
if (requireSeparateTemplates && names.length > 1) { | ||
report(`Missing separate @template for ${names[1]}`, null, tag); | ||
} | ||
} | ||
|
||
/** | ||
* @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 usedName of usedNames) { | ||
if (!templateNames.includes(usedName)) { | ||
report(`Missing @template ${usedName}`); | ||
} | ||
} | ||
}; | ||
|
||
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); | ||
} | ||
}); | ||
|
||
// Could check against whitelist/blacklist | ||
for (const usedName of usedNames) { | ||
if (!templateNames.includes(usedName)) { | ||
report(`Missing @template ${usedName}`, null, typedefTags[0]); | ||
} | ||
} | ||
}, { | ||
iterateAllJsdocs: true, | ||
meta: { | ||
docs: { | ||
description: 'Requires template tags for each generic type parameter', | ||
url: 'https://github.com/gajus/eslint-plugin-jsdoc/blob/main/docs/rules/require-template.md#repos-sticky-header', | ||
}, | ||
schema: [ | ||
{ | ||
additionalProperties: false, | ||
properties: { | ||
requireSeparateTemplates: { | ||
type: 'boolean' | ||
} | ||
}, | ||
type: 'object', | ||
}, | ||
], | ||
type: 'suggestion', | ||
}, | ||
}); |
Oops, something went wrong.