-
-
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.
feat(
require-template
): add rule; fixes part of #1120
- Loading branch information
Showing
13 changed files
with
692 additions
and
71 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,7 @@ jobs: | |
node_js_version: | ||
- '18' | ||
- '20' | ||
- '22' | ||
build: | ||
runs-on: ubuntu-latest | ||
name: Build | ||
|
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
Oops, something went wrong.