Skip to content

Commit 3aaba73

Browse files
committed
feat(require-param): add contexts option; closes #511
1 parent 62610db commit 3aaba73

File tree

4 files changed

+142
-12
lines changed

4 files changed

+142
-12
lines changed

.README/rules/require-param.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,27 @@ Requires that all function parameters are documented.
44

55
#### Options
66

7-
An options object accepts one optional property:
7+
An options object accepts two optional properties:
88

9-
- `exemptedBy` - Array of tags (e.g., `['type']`) whose presence on the document
10-
block avoids the need for a `@param`. Defaults to an empty array.
9+
##### `exemptedBy`
10+
11+
Array of tags (e.g., `['type']`) whose presence on the document block
12+
avoids the need for a `@param`. Defaults to an empty array.
13+
14+
##### `contexts`
15+
16+
Set this to an array of strings representing the AST context
17+
where you wish the rule to be applied. Overrides the default
18+
contexts (see below). May be useful for adding such as
19+
`TSMethodSignature` in TypeScript or restricting the contexts
20+
which are checked.
1121

1222
|||
1323
|---|---|
14-
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`|
24+
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`; others when `contexts` option enabled|
1525
|Tags|`param`|
1626
|Aliases|`arg`, `argument`|
17-
|Options|`exemptedBy`|
27+
|Options|`contexts`, `exemptedBy`|
1828
|Settings|`overrideReplacesDocs`, `augmentsExtendsReplacesDocs`, `implementsReplacesDocs`|
1929

2030
<!-- assertions requireParam -->

README.md

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8763,17 +8763,29 @@ Requires that all function parameters are documented.
87638763
<a name="eslint-plugin-jsdoc-rules-require-param-options-23"></a>
87648764
#### Options
87658765

8766-
An options object accepts one optional property:
8766+
An options object accepts two optional properties:
87678767

8768-
- `exemptedBy` - Array of tags (e.g., `['type']`) whose presence on the document
8769-
block avoids the need for a `@param`. Defaults to an empty array.
8768+
<a name="eslint-plugin-jsdoc-rules-require-param-options-23-exemptedby-1"></a>
8769+
##### <code>exemptedBy</code>
8770+
8771+
Array of tags (e.g., `['type']`) whose presence on the document block
8772+
avoids the need for a `@param`. Defaults to an empty array.
8773+
8774+
<a name="eslint-plugin-jsdoc-rules-require-param-options-23-contexts-7"></a>
8775+
##### <code>contexts</code>
8776+
8777+
Set this to an array of strings representing the AST context
8778+
where you wish the rule to be applied. Overrides the default
8779+
contexts (see below). May be useful for adding such as
8780+
`TSMethodSignature` in TypeScript or restricting the contexts
8781+
which are checked.
87708782

87718783
|||
87728784
|---|---|
8773-
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`|
8785+
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`; others when `contexts` option enabled|
87748786
|Tags|`param`|
87758787
|Aliases|`arg`, `argument`|
8776-
|Options|`exemptedBy`|
8788+
|Options|`contexts`, `exemptedBy`|
87778789
|Settings|`overrideReplacesDocs`, `augmentsExtendsReplacesDocs`, `implementsReplacesDocs`|
87788790

87798791
The following patterns are considered problems:
@@ -8787,6 +8799,15 @@ function quux (foo) {
87878799
}
87888800
// Message: Missing JSDoc @param "foo" declaration.
87898801

8802+
/**
8803+
*
8804+
*/
8805+
function quux (foo) {
8806+
8807+
}
8808+
// Options: [{"contexts":["FunctionDeclaration"]}]
8809+
// Message: Missing JSDoc @param "foo" declaration.
8810+
87908811
/**
87918812
*
87928813
*/
@@ -8989,6 +9010,15 @@ function assign (employees, name) {
89899010

89909011
};
89919012
// Message: Missing JSDoc @param "name" declaration.
9013+
9014+
interface ITest {
9015+
/**
9016+
* Test description.
9017+
*/
9018+
TestMethod(id: number): void;
9019+
}
9020+
// Options: [{"contexts":["TSMethodSignature"]}]
9021+
// Message: Missing JSDoc @param "id" declaration.
89929022
````
89939023

89949024
The following patterns are not considered problems:
@@ -9347,6 +9377,14 @@ export abstract class StephanPlugin<O, D> {
93479377

93489378
}
93499379
}
9380+
9381+
/**
9382+
*
9383+
*/
9384+
function quux (foo) {
9385+
9386+
}
9387+
// Options: [{"contexts":["ArrowFunctionExpression"]}]
93509388
````
93519389

93529390

@@ -10030,7 +10068,7 @@ will not be reported if the return value is `void` or `undefined`.
1003010068
<a name="eslint-plugin-jsdoc-rules-require-returns-description-options-24"></a>
1003110069
#### Options
1003210070

10033-
<a name="eslint-plugin-jsdoc-rules-require-returns-description-options-24-contexts-7"></a>
10071+
<a name="eslint-plugin-jsdoc-rules-require-returns-description-options-24-contexts-8"></a>
1003410072
##### <code>contexts</code>
1003510073

1003610074
Set this to an array of strings representing the AST context
@@ -10168,7 +10206,7 @@ Requires that `@returns` tag has `type` value.
1016810206
<a name="eslint-plugin-jsdoc-rules-require-returns-type-options-25"></a>
1016910207
#### Options
1017010208

10171-
<a name="eslint-plugin-jsdoc-rules-require-returns-type-options-25-contexts-8"></a>
10209+
<a name="eslint-plugin-jsdoc-rules-require-returns-type-options-25-contexts-9"></a>
1017210210
##### <code>contexts</code>
1017310211

1017410212
Set this to an array of strings representing the AST context

src/rules/requireParam.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,19 @@ export default iterateJsdoc(({
7777
utils.reportJSDoc(`Missing JSDoc @${preferredTagName} "${functionParameterName}" declaration.`, null, fixer);
7878
});
7979
}, {
80+
contextDefaults: true,
8081
meta: {
8182
fixable: 'code',
8283
schema: [
8384
{
8485
additionalProperties: false,
8586
properties: {
87+
contexts: {
88+
items: {
89+
type: 'string',
90+
},
91+
type: 'array',
92+
},
8693
exemptedBy: {
8794
items: {
8895
type: 'string',

test/rules/assertions/requireParam.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,34 @@ export default {
2929
}
3030
`,
3131
},
32+
{
33+
code: `
34+
/**
35+
*
36+
*/
37+
function quux (foo) {
38+
39+
}
40+
`,
41+
errors: [
42+
{
43+
message: 'Missing JSDoc @param "foo" declaration.',
44+
},
45+
],
46+
options: [
47+
{
48+
contexts: ['FunctionDeclaration'],
49+
},
50+
],
51+
output: `
52+
/**
53+
* @param foo
54+
*/
55+
function quux (foo) {
56+
57+
}
58+
`,
59+
},
3260
{
3361
code: `
3462
/**
@@ -511,6 +539,38 @@ export default {
511539
},
512540
],
513541
},
542+
{
543+
code: `
544+
interface ITest {
545+
/**
546+
* Test description.
547+
*/
548+
TestMethod(id: number): void;
549+
}
550+
`,
551+
errors: [
552+
{
553+
line: 3,
554+
message: 'Missing JSDoc @param "id" declaration.',
555+
},
556+
],
557+
options: [
558+
{
559+
contexts: ['TSMethodSignature'],
560+
},
561+
],
562+
output: `
563+
interface ITest {
564+
/**
565+
* Test description.
566+
*
567+
* @param id
568+
*/
569+
TestMethod(id: number): void;
570+
}
571+
`,
572+
parser: require.resolve('@typescript-eslint/parser'),
573+
},
514574
],
515575
valid: [
516576
{
@@ -1047,5 +1107,20 @@ export default {
10471107
`,
10481108
parser: require.resolve('@typescript-eslint/parser'),
10491109
},
1110+
{
1111+
code: `
1112+
/**
1113+
*
1114+
*/
1115+
function quux (foo) {
1116+
1117+
}
1118+
`,
1119+
options: [
1120+
{
1121+
contexts: ['ArrowFunctionExpression'],
1122+
},
1123+
],
1124+
},
10501125
],
10511126
};

0 commit comments

Comments
 (0)