Skip to content

Commit a45f41c

Browse files
committed
fix(require-example): add fixer for missing declaration (though can do nothing with missing description); fixes part of gajus#336
1 parent 5863029 commit a45f41c

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

.README/rules/require-example.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ Set this to an array of strings representing the AST context
2626
where you wish the rule to be applied (e.g., `ClassDeclaration` for ES6 classes).
2727
Overrides the default contexts (see below).
2828

29+
#### Fixer
30+
31+
The fixer for `require-example` will add an empty `@example`, but it will still
32+
report a missing example description after this is added.
33+
2934
|||
3035
|---|---|
3136
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`; others when `contexts` option enabled|

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4441,6 +4441,12 @@ Set this to an array of strings representing the AST context
44414441
where you wish the rule to be applied (e.g., `ClassDeclaration` for ES6 classes).
44424442
Overrides the default contexts (see below).
44434443

4444+
<a name="eslint-plugin-jsdoc-rules-require-example-fixer"></a>
4445+
#### Fixer
4446+
4447+
The fixer for `require-example` will add an empty `@example`, but it will still
4448+
report a missing example description after this is added.
4449+
44444450
|||
44454451
|---|---|
44464452
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`; others when `contexts` option enabled|

src/rules/requireExample.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,20 @@ export default iterateJsdoc(({
3333
}
3434

3535
if (!functionExamples.length) {
36-
report(`Missing JSDoc @${targetTagName} declaration.`);
36+
utils.reportJSDoc(`Missing JSDoc @${targetTagName} declaration.`, null, () => {
37+
if (!jsdoc.tags) {
38+
jsdoc.tags = [];
39+
}
40+
const line = jsdoc.tags.length ? jsdoc.tags[jsdoc.tags.length - 1].line + 1 : 0;
41+
jsdoc.tags.push({
42+
description: '',
43+
line,
44+
name: '',
45+
optional: false,
46+
tag: targetTagName,
47+
type: ''
48+
});
49+
});
3750

3851
return;
3952
}
@@ -48,6 +61,7 @@ export default iterateJsdoc(({
4861
}, {
4962
contextDefaults: true,
5063
meta: {
64+
fixable: 'code',
5165
schema: [
5266
{
5367
additionalProperties: false,

test/rules/assertions/requireExample.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,15 @@ export default {
1313
{
1414
message: 'Missing JSDoc @example declaration.'
1515
}
16-
]
16+
],
17+
output: `
18+
/**
19+
* @example
20+
*/
21+
function quux () {
22+
23+
}
24+
`
1725
},
1826
{
1927
code: `

0 commit comments

Comments
 (0)