Skip to content

Commit 2c3c596

Browse files
authored
Merge pull request #242 from brettz9/require-description
`require-description` in additional contexts
2 parents b64431f + 9e0e951 commit 2c3c596

File tree

5 files changed

+174
-5
lines changed

5 files changed

+174
-5
lines changed

.README/rules/require-description.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,19 @@ Requires that all functions have a description.
55
* All functions must have a `@description` tag.
66
* Every description tag must have a non-empty description that explains the purpose of the method.
77

8+
#### Options
9+
10+
- `contexts` - Set to a string or array of strings representing the AST context
11+
where you wish the rule to be applied (e.g., `ClassDeclaration` for ES6 classes).
12+
- `noDefaults` - By default, `contexts` will permit `ArrowFunctionExpression`,
13+
`FunctionDeclaration`, and `FunctionExpression`. Set this instead to `true` to
14+
have `contexts` override these.
15+
816
|||
917
|---|---|
10-
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`|
18+
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`; others when `contexts` option enabled|
1119
|Tags|`description`|
1220
|Aliases|`desc`|
21+
|Options|`contexts`, `noDefaults`|
1322

1423
<!-- assertions requireDescription -->

README.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2192,11 +2192,21 @@ Requires that all functions have a description.
21922192
* All functions must have a `@description` tag.
21932193
* Every description tag must have a non-empty description that explains the purpose of the method.
21942194

2195+
<a name="eslint-plugin-jsdoc-rules-require-description-options"></a>
2196+
#### Options
2197+
2198+
- `contexts` - Set to a string or array of strings representing the AST context
2199+
where you wish the rule to be applied (e.g., `ClassDeclaration` for ES6 classes).
2200+
- `noDefaults` - By default, `contexts` will permit `ArrowFunctionExpression`,
2201+
`FunctionDeclaration`, and `FunctionExpression`. Set this instead to `true` to
2202+
have `contexts` override these.
2203+
21952204
|||
21962205
|---|---|
2197-
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`|
2206+
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`; others when `contexts` option enabled|
21982207
|Tags|`description`|
21992208
|Aliases|`desc`|
2209+
|Options|`contexts`, `noDefaults`|
22002210

22012211
The following patterns are considered problems:
22022212

@@ -2209,6 +2219,33 @@ function quux () {
22092219
}
22102220
// Message: Missing JSDoc @description declaration.
22112221

2222+
/**
2223+
*
2224+
*/
2225+
class quux {
2226+
2227+
}
2228+
// Options: [{"contexts":"ClassDeclaration"}]
2229+
// Message: Missing JSDoc @description declaration.
2230+
2231+
/**
2232+
*
2233+
*/
2234+
class quux {
2235+
2236+
}
2237+
// Options: [{"contexts":"ClassDeclaration","noDefaults":true}]
2238+
// Message: Missing JSDoc @description declaration.
2239+
2240+
/**
2241+
*
2242+
*/
2243+
class quux {
2244+
2245+
}
2246+
// Options: [{"contexts":["ClassDeclaration"]}]
2247+
// Message: Missing JSDoc @description declaration.
2248+
22122249
/**
22132250
* @description
22142251
*/
@@ -2247,6 +2284,21 @@ function quux () {
22472284
function quux () {
22482285

22492286
}
2287+
2288+
/**
2289+
*
2290+
*/
2291+
class quux {
2292+
2293+
}
2294+
2295+
/**
2296+
*
2297+
*/
2298+
function quux () {
2299+
2300+
}
2301+
// Options: [{"noDefaults":true}]
22502302
````
22512303

22522304

src/iterateJsdoc.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,16 +371,19 @@ export default (iterator, opts = {}) => {
371371
});
372372
};
373373

374+
let contexts = opts.returns;
374375
if (typeof opts.returns === 'function') {
375-
return opts.returns(context, sourceCode, checkJsdoc);
376+
contexts = opts.returns(context, sourceCode, checkJsdoc);
376377
}
377378

378-
if (Array.isArray(opts.returns)) {
379-
return opts.returns.reduce((obj, prop) => {
379+
if (Array.isArray(contexts)) {
380+
return contexts.reduce((obj, prop) => {
380381
obj[prop] = checkJsdoc;
381382

382383
return obj;
383384
}, {});
385+
} else if (contexts) {
386+
return contexts;
384387
}
385388

386389
return {

src/rules/requireDescription.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,23 @@ export default iterateJsdoc(({
2525
report('Missing JSDoc @' + targetTagName + ' description.');
2626
}
2727
});
28+
}, {
29+
returns (context) {
30+
const defaultContexts = [
31+
'ArrowFunctionExpression',
32+
'FunctionDeclaration',
33+
'FunctionExpression'
34+
];
35+
36+
const {
37+
noDefaults,
38+
contexts: ctxts = []
39+
} = context.options[0] || {};
40+
41+
const contexts = typeof ctxts === 'string' ? [ctxts] : ctxts;
42+
43+
return noDefaults ?
44+
contexts :
45+
[...new Set([...defaultContexts, ...contexts])];
46+
}
2847
});

test/rules/assertions/requireDescription.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,67 @@ export default {
1515
}
1616
]
1717
},
18+
{
19+
code: `
20+
/**
21+
*
22+
*/
23+
class quux {
24+
25+
}
26+
`,
27+
errors: [
28+
{
29+
message: 'Missing JSDoc @description declaration.'
30+
}
31+
],
32+
options: [
33+
{
34+
contexts: 'ClassDeclaration'
35+
}
36+
]
37+
},
38+
{
39+
code: `
40+
/**
41+
*
42+
*/
43+
class quux {
44+
45+
}
46+
`,
47+
errors: [
48+
{
49+
message: 'Missing JSDoc @description declaration.'
50+
}
51+
],
52+
options: [
53+
{
54+
contexts: 'ClassDeclaration',
55+
noDefaults: true
56+
}
57+
]
58+
},
59+
{
60+
code: `
61+
/**
62+
*
63+
*/
64+
class quux {
65+
66+
}
67+
`,
68+
errors: [
69+
{
70+
message: 'Missing JSDoc @description declaration.'
71+
}
72+
],
73+
options: [
74+
{
75+
contexts: ['ClassDeclaration']
76+
}
77+
]
78+
},
1879
{
1980
code: `
2081
/**
@@ -67,6 +128,31 @@ export default {
67128
68129
}
69130
`
131+
},
132+
{
133+
code: `
134+
/**
135+
*
136+
*/
137+
class quux {
138+
139+
}
140+
`
141+
},
142+
{
143+
code: `
144+
/**
145+
*
146+
*/
147+
function quux () {
148+
149+
}
150+
`,
151+
options: [
152+
{
153+
noDefaults: true
154+
}
155+
]
70156
}
71157
]
72158
};

0 commit comments

Comments
 (0)