Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add rule require-example (#35)
* feat: Add require-example rule 🎉 * test: Add require-example tests * docs: Added documentation for require-example 📝
- Loading branch information
Showing
6 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
### `require-example` | ||
|
||
Requires that all functions have examples. | ||
|
||
* All functions must have one or more `@example` tags. | ||
* Every example tag must have a non-empty description that explains the method's usage. | ||
|
||
||| | ||
|---|---| | ||
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`| | ||
|Tags|`example`| | ||
|
||
<!-- assertions requireExample --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import _ from 'lodash'; | ||
import iterateJsdoc from '../iterateJsdoc'; | ||
|
||
export default iterateJsdoc(({ | ||
jsdoc, | ||
report, | ||
utils | ||
}) => { | ||
const targetTagName = utils.getPreferredTagName('example'); | ||
|
||
const functionExamples = _.filter(jsdoc.tags, { | ||
tag: targetTagName | ||
}); | ||
|
||
if (_.isEmpty(functionExamples)) { | ||
return report('Missing JSDoc @' + targetTagName + ' declaration.'); | ||
} | ||
|
||
return _.forEach(functionExamples, (example) => { | ||
const exampleContent = _.compact((example.name + ' ' + example.description).trim().split('\n')); | ||
|
||
if (_.isEmpty(exampleContent)) { | ||
report('Missing JSDoc @' + targetTagName + ' description.'); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* eslint-disable no-restricted-syntax */ | ||
|
||
export default { | ||
invalid: [ | ||
{ | ||
code: ` | ||
/** | ||
* | ||
*/ | ||
function quux () { | ||
} | ||
`, | ||
errors: [ | ||
{ | ||
message: 'Missing JSDoc @example declaration.' | ||
} | ||
] | ||
}, | ||
{ | ||
code: ` | ||
/** | ||
* @example | ||
*/ | ||
function quux () { | ||
} | ||
`, | ||
errors: [ | ||
{ | ||
message: 'Missing JSDoc @example description.' | ||
} | ||
] | ||
} | ||
], | ||
valid: [ | ||
{ | ||
code: ` | ||
/** | ||
* @example | ||
* // arbitrary example content | ||
*/ | ||
function quux () { | ||
} | ||
` | ||
}, | ||
{ | ||
code: ` | ||
/** | ||
* @example | ||
* quux(); // does something useful | ||
*/ | ||
function quux () { | ||
} | ||
` | ||
}, | ||
{ | ||
code: ` | ||
/** | ||
* @example <caption>Valid usage</caption> | ||
* quux(); // does something useful | ||
* | ||
* @example <caption>Invalid usage</caption> | ||
* quux('random unwanted arg'); // results in an error | ||
*/ | ||
function quux () { | ||
} | ||
` | ||
} | ||
] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters