Skip to content

Commit 76771cb

Browse files
committed
feat(require-example): add contexts array option (of AST types) to allow overriding enforced contexts (fixes #273)
1 parent 7400ee7 commit 76771cb

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
@@ -4056,20 +4056,33 @@ Requires that all functions have examples.
40564056
<a name="eslint-plugin-jsdoc-rules-require-example-options-7"></a>
40574057
#### Options
40584058

4059-
This rule has an object option:
4059+
This rule has an object option.
40604060

4061-
- `exemptedBy` - Array of tags (e.g., `['type']`) whose presence on the document
4062-
block avoids the need for an `@example`. Defaults to an empty array.
4061+
<a name="eslint-plugin-jsdoc-rules-require-example-options-7-exemptedby"></a>
4062+
##### <code>exemptedBy</code>
4063+
4064+
Array of tags (e.g., `['type']`) whose presence on the document
4065+
block avoids the need for an `@example`. Defaults to an empty array.
4066+
4067+
<a name="eslint-plugin-jsdoc-rules-require-example-options-7-avoidexampleonconstructors"></a>
4068+
##### <code>avoidExampleOnConstructors</code>
40634069

4064-
- `avoidExampleOnConstructors` (default: false) - Set to `true` to avoid the
4065-
need for an example on a constructor (whether indicated as such by a
4066-
jsdoc tag or by being within an ES6 `class`).
4070+
Set to `true` to avoid the need for an example on a constructor (whether
4071+
indicated as such by a jsdoc tag or by being within an ES6 `class`).
4072+
Defaults to `false`.
4073+
4074+
<a name="eslint-plugin-jsdoc-rules-require-example-options-7-contexts-1"></a>
4075+
##### <code>contexts</code>
4076+
4077+
Set this to an array of strings representing the AST context
4078+
where you wish the rule to be applied (e.g., `ClassDeclaration` for ES6 classes).
4079+
Overrides the default contexts (see below).
40674080

40684081
|||
40694082
|---|---|
4070-
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`|
4083+
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`; others when `contexts` option enabled|
40714084
|Tags|`example`|
4072-
|Options|`exemptedBy`, `avoidExampleOnConstructors`|
4085+
|Options|`exemptedBy`, `avoidExampleOnConstructors`, `contexts`|
40734086
|Settings|`overrideReplacesDocs`, `augmentsExtendsReplacesDocs`, `implementsReplacesDocs`|
40744087

40754088
The following patterns are considered problems:
@@ -4116,6 +4129,15 @@ function quux () {
41164129

41174130
}
41184131
// Message: Missing JSDoc @example description.
4132+
4133+
/**
4134+
*
4135+
*/
4136+
class quux {
4137+
4138+
}
4139+
// Options: [{"contexts":["ClassDeclaration"]}]
4140+
// Message: Missing JSDoc @example declaration.
41194141
````
41204142

41214143
The following patterns are not considered problems:
@@ -4189,6 +4211,22 @@ function quux () {
41894211

41904212
}
41914213
// Options: [{"exemptedBy":["type"]}]
4214+
4215+
/**
4216+
* @example Some example code
4217+
*/
4218+
class quux {
4219+
4220+
}
4221+
// Options: [{"contexts":["ClassDeclaration"]}]
4222+
4223+
/**
4224+
*
4225+
*/
4226+
function quux () {
4227+
4228+
}
4229+
// Options: [{"contexts":["ClassDeclaration"]}]
41924230
````
41934231

41944232

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)