Skip to content

Commit

Permalink
fix(require-example): add fixer for missing declaration (though can d…
Browse files Browse the repository at this point in the history
…o nothing with missing description); fixes part of #336
  • Loading branch information
brettz9 committed Jul 22, 2019
1 parent 5863029 commit a45f41c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .README/rules/require-example.md
Expand Up @@ -26,6 +26,11 @@ Set this to an array of strings representing the AST context
where you wish the rule to be applied (e.g., `ClassDeclaration` for ES6 classes).
Overrides the default contexts (see below).

#### Fixer

The fixer for `require-example` will add an empty `@example`, but it will still
report a missing example description after this is added.

|||
|---|---|
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`; others when `contexts` option enabled|
Expand Down
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -4441,6 +4441,12 @@ Set this to an array of strings representing the AST context
where you wish the rule to be applied (e.g., `ClassDeclaration` for ES6 classes).
Overrides the default contexts (see below).

<a name="eslint-plugin-jsdoc-rules-require-example-fixer"></a>
#### Fixer

The fixer for `require-example` will add an empty `@example`, but it will still
report a missing example description after this is added.

|||
|---|---|
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`; others when `contexts` option enabled|
Expand Down
16 changes: 15 additions & 1 deletion src/rules/requireExample.js
Expand Up @@ -33,7 +33,20 @@ export default iterateJsdoc(({
}

if (!functionExamples.length) {
report(`Missing JSDoc @${targetTagName} declaration.`);
utils.reportJSDoc(`Missing JSDoc @${targetTagName} declaration.`, null, () => {
if (!jsdoc.tags) {
jsdoc.tags = [];
}
const line = jsdoc.tags.length ? jsdoc.tags[jsdoc.tags.length - 1].line + 1 : 0;
jsdoc.tags.push({
description: '',
line,
name: '',
optional: false,
tag: targetTagName,
type: ''
});
});

return;
}
Expand All @@ -48,6 +61,7 @@ export default iterateJsdoc(({
}, {
contextDefaults: true,
meta: {
fixable: 'code',
schema: [
{
additionalProperties: false,
Expand Down
10 changes: 9 additions & 1 deletion test/rules/assertions/requireExample.js
Expand Up @@ -13,7 +13,15 @@ export default {
{
message: 'Missing JSDoc @example declaration.'
}
]
],
output: `
/**
* @example
*/
function quux () {
}
`
},
{
code: `
Expand Down

0 comments on commit a45f41c

Please sign in to comment.