Skip to content

Commit 87a40f6

Browse files
authored
Merge pull request #321 from brettz9/require-example-contexts-option
`require-example`: add `contexts` option
2 parents 9fe4989 + 76771cb commit 87a40f6

File tree

4 files changed

+121
-16
lines changed

4 files changed

+121
-16
lines changed

.README/rules/require-example.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,30 @@ Requires that all functions have examples.
77

88
#### Options
99

10-
This rule has an object option:
10+
This rule has an object option.
1111

12-
- `exemptedBy` - Array of tags (e.g., `['type']`) whose presence on the document
13-
block avoids the need for an `@example`. Defaults to an empty array.
12+
##### `exemptedBy`
1413

15-
- `avoidExampleOnConstructors` (default: false) - Set to `true` to avoid the
16-
need for an example on a constructor (whether indicated as such by a
17-
jsdoc tag or by being within an ES6 `class`).
14+
Array of tags (e.g., `['type']`) whose presence on the document
15+
block avoids the need for an `@example`. Defaults to an empty array.
16+
17+
##### `avoidExampleOnConstructors`
18+
19+
Set to `true` to avoid the need for an example on a constructor (whether
20+
indicated as such by a jsdoc tag or by being within an ES6 `class`).
21+
Defaults to `false`.
22+
23+
##### `contexts`
24+
25+
Set this to an array of strings representing the AST context
26+
where you wish the rule to be applied (e.g., `ClassDeclaration` for ES6 classes).
27+
Overrides the default contexts (see below).
1828

1929
|||
2030
|---|---|
21-
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`|
31+
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`; others when `contexts` option enabled|
2232
|Tags|`example`|
23-
|Options|`exemptedBy`, `avoidExampleOnConstructors`|
33+
|Options|`exemptedBy`, `avoidExampleOnConstructors`, `contexts`|
2434
|Settings|`overrideReplacesDocs`, `augmentsExtendsReplacesDocs`, `implementsReplacesDocs`|
2535

2636
<!-- assertions requireExample -->

README.md

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4076,20 +4076,33 @@ Requires that all functions have examples.
40764076
<a name="eslint-plugin-jsdoc-rules-require-example-options-7"></a>
40774077
#### Options
40784078

4079-
This rule has an object option:
4079+
This rule has an object option.
40804080

4081-
- `exemptedBy` - Array of tags (e.g., `['type']`) whose presence on the document
4082-
block avoids the need for an `@example`. Defaults to an empty array.
4081+
<a name="eslint-plugin-jsdoc-rules-require-example-options-7-exemptedby"></a>
4082+
##### <code>exemptedBy</code>
4083+
4084+
Array of tags (e.g., `['type']`) whose presence on the document
4085+
block avoids the need for an `@example`. Defaults to an empty array.
4086+
4087+
<a name="eslint-plugin-jsdoc-rules-require-example-options-7-avoidexampleonconstructors"></a>
4088+
##### <code>avoidExampleOnConstructors</code>
40834089

4084-
- `avoidExampleOnConstructors` (default: false) - Set to `true` to avoid the
4085-
need for an example on a constructor (whether indicated as such by a
4086-
jsdoc tag or by being within an ES6 `class`).
4090+
Set to `true` to avoid the need for an example on a constructor (whether
4091+
indicated as such by a jsdoc tag or by being within an ES6 `class`).
4092+
Defaults to `false`.
4093+
4094+
<a name="eslint-plugin-jsdoc-rules-require-example-options-7-contexts-1"></a>
4095+
##### <code>contexts</code>
4096+
4097+
Set this to an array of strings representing the AST context
4098+
where you wish the rule to be applied (e.g., `ClassDeclaration` for ES6 classes).
4099+
Overrides the default contexts (see below).
40874100

40884101
|||
40894102
|---|---|
4090-
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`|
4103+
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`; others when `contexts` option enabled|
40914104
|Tags|`example`|
4092-
|Options|`exemptedBy`, `avoidExampleOnConstructors`|
4105+
|Options|`exemptedBy`, `avoidExampleOnConstructors`, `contexts`|
40934106
|Settings|`overrideReplacesDocs`, `augmentsExtendsReplacesDocs`, `implementsReplacesDocs`|
40944107

40954108
The following patterns are considered problems:
@@ -4136,6 +4149,15 @@ function quux () {
41364149

41374150
}
41384151
// Message: Missing JSDoc @example description.
4152+
4153+
/**
4154+
*
4155+
*/
4156+
class quux {
4157+
4158+
}
4159+
// Options: [{"contexts":["ClassDeclaration"]}]
4160+
// Message: Missing JSDoc @example declaration.
41394161
````
41404162

41414163
The following patterns are not considered problems:
@@ -4209,6 +4231,22 @@ function quux () {
42094231

42104232
}
42114233
// Options: [{"exemptedBy":["type"]}]
4234+
4235+
/**
4236+
* @example Some example code
4237+
*/
4238+
class quux {
4239+
4240+
}
4241+
// Options: [{"contexts":["ClassDeclaration"]}]
4242+
4243+
/**
4244+
*
4245+
*/
4246+
function quux () {
4247+
4248+
}
4249+
// Options: [{"contexts":["ClassDeclaration"]}]
42124250
````
42134251

42144252

src/rules/requireExample.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export default iterateJsdoc(({
4646
}
4747
});
4848
}, {
49+
contextDefaults: true,
4950
meta: {
5051
schema: [
5152
{
@@ -55,6 +56,12 @@ export default iterateJsdoc(({
5556
default: false,
5657
type: 'boolean'
5758
},
59+
contexts: {
60+
items: {
61+
type: 'string'
62+
},
63+
type: 'array'
64+
},
5865
exemptedBy: {
5966
items: {
6067
type: 'string'

test/rules/assertions/requireExample.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,26 @@ export default {
8383
message: 'Missing JSDoc @example description.'
8484
}
8585
]
86+
},
87+
{
88+
code: `
89+
/**
90+
*
91+
*/
92+
class quux {
93+
94+
}
95+
`,
96+
errors: [
97+
{
98+
message: 'Missing JSDoc @example declaration.'
99+
}
100+
],
101+
options: [
102+
{
103+
contexts: ['ClassDeclaration']
104+
}
105+
]
86106
}
87107
],
88108
valid: [
@@ -188,6 +208,36 @@ export default {
188208
exemptedBy: ['type']
189209
}
190210
]
211+
},
212+
{
213+
code: `
214+
/**
215+
* @example Some example code
216+
*/
217+
class quux {
218+
219+
}
220+
`,
221+
options: [
222+
{
223+
contexts: ['ClassDeclaration']
224+
}
225+
]
226+
},
227+
{
228+
code: `
229+
/**
230+
*
231+
*/
232+
function quux () {
233+
234+
}
235+
`,
236+
options: [
237+
{
238+
contexts: ['ClassDeclaration']
239+
}
240+
]
191241
}
192242
]
193243
};

0 commit comments

Comments
 (0)