|
| 1 | +var assert = require('assert'); |
| 2 | + |
| 3 | +var availablePresets = require('../../tags'); |
| 4 | +var jsdoc = require('../../jsdoc'); |
| 5 | + |
| 6 | +module.exports = validateAnnotations; |
| 7 | +module.exports.scopes = ['file']; |
| 8 | +module.exports.options = { |
| 9 | + checkAnnotations: true |
| 10 | +}; |
| 11 | + |
| 12 | +var tags; |
| 13 | + |
| 14 | +validateAnnotations.configure = function(options) { |
| 15 | + var o = options.checkAnnotations; |
| 16 | + |
| 17 | + assert(o === true || typeof o === 'string' || typeof o === 'object', |
| 18 | + 'jsDoc.checkAnnotation rule was not configured properly'); |
| 19 | + |
| 20 | + if (typeof o === 'string') { |
| 21 | + o = {preset: o}; |
| 22 | + } |
| 23 | + |
| 24 | + tags = {}; |
| 25 | + |
| 26 | + if (o === true) { |
| 27 | + Object.keys(availablePresets).forEach(function(preset) { |
| 28 | + var presetTags = availablePresets[preset]; |
| 29 | + Object.keys(presetTags).forEach(function(tag) { |
| 30 | + tags[tag] = tags[tag] || presetTags[tag]; |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + } else if (typeof o === 'object') { |
| 35 | + if (o.preset) { |
| 36 | + assert(typeof o.preset === 'string', 'jsDoc.checkAnnotation.preset should be preset name'); |
| 37 | + assert(availablePresets[o.preset], 'Unknown tag preset ' + o.preset); |
| 38 | + Object.keys(availablePresets[o.preset]).forEach(function(tag) { |
| 39 | + tags[tag] = tags[tag] || availablePresets[o.preset][tag]; |
| 40 | + }); |
| 41 | + } |
| 42 | + if (o.extra) { |
| 43 | + Object.keys(o.extra).forEach(function(tag) { |
| 44 | + tags[tag] = o.extra[tag]; |
| 45 | + }); |
| 46 | + } |
| 47 | + } |
| 48 | +}; |
| 49 | + |
| 50 | +/** |
| 51 | + * validator for annotations |
| 52 | + * @param {JSCS.JSFile} file |
| 53 | + * @param {JSCS.Errors} errors |
| 54 | + */ |
| 55 | +function validateAnnotations(file, errors) { |
| 56 | + var comments = file.getComments(); |
| 57 | + comments.forEach(function(commentNode) { |
| 58 | + if (commentNode.type !== 'Block' || commentNode.value[0] !== '*') { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + // trying to create DocComment object |
| 63 | + var node = jsdoc.createDocCommentByCommentNode(commentNode); |
| 64 | + if (!node.valid) { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + node.iterate(function(tag) { |
| 69 | + if (!tags.hasOwnProperty[tag.id]) { |
| 70 | + errors.add('unavailable tag ' + tag.id, tag.loc); |
| 71 | + } |
| 72 | + else if (tags[tag.id] && (!tag.name || !tag.type)) { |
| 73 | + errors.add('incomplete tag ' + tag.id + ' data', tag.loc); |
| 74 | + } |
| 75 | + }); |
| 76 | + }); |
| 77 | +} |
0 commit comments