|
| 1 | +import _ from 'lodash'; |
| 2 | +import iterateJsdoc from '../iterateJsdoc'; |
| 3 | + |
| 4 | +const tagsWithDescriptions = ['param', 'arg', 'argument', 'returns', 'return']; |
| 5 | + |
| 6 | +export default iterateJsdoc(({ |
| 7 | + jsdoc, |
| 8 | + report, |
| 9 | + context |
| 10 | +}) => { |
| 11 | + const options = context.options[0] || {}; |
| 12 | + |
| 13 | + const validateDescription = (description, tag) => { |
| 14 | + const regex = new RegExp( |
| 15 | + (tag && typeof options.tags[tag] === 'string' ? options.tags[tag] : |
| 16 | + options.matchDescription, |
| 17 | + |
| 18 | + // If supporting Node >= 10, we could loosen to this for the |
| 19 | + // initial letter: \\p{Upper} |
| 20 | + options.matchDescription) || '^([A-Z]|[`\\d_])([\\s\\S]*[.?!`])?$', |
| 21 | + 'u' |
| 22 | + ); |
| 23 | + |
| 24 | + if (!regex.test(description)) { |
| 25 | + report('JSDoc description does not satisfy the regex pattern.'); |
| 26 | + |
| 27 | + return true; |
| 28 | + } |
| 29 | + |
| 30 | + return false; |
| 31 | + }; |
| 32 | + |
| 33 | + if (jsdoc.description && validateDescription(jsdoc.description)) { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + if (!options.tags || !Object.keys(options.tags).length) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + const tags = _.filter(jsdoc.tags, (tag) => { |
| 42 | + return _.includes(tagsWithDescriptions, tag.tag) && |
| 43 | + {}.hasOwnProperty.call(options.tags, tag.tag) && options.tags[tag.tag]; |
| 44 | + }); |
| 45 | + |
| 46 | + _.some(tags, (tag) => { |
| 47 | + const description = _.trimStart(tag.description, '- '); |
| 48 | + |
| 49 | + return validateDescription(description, tag.tag); |
| 50 | + }); |
| 51 | +}); |
0 commit comments