|
| 1 | +var assert = require('assert'); |
| 2 | + |
1 | 3 | module.exports = enforceExistence;
|
2 | 4 | module.exports.scopes = ['function'];
|
3 | 5 | module.exports.options = {
|
4 |
| - enforceExistence: {allowedValues: [true, 'exceptExports']} |
| 6 | + enforceExistence: true |
5 | 7 | };
|
6 | 8 |
|
7 | 9 | /**
|
8 |
| - * Validator for jsdoc data existance |
| 10 | + * @param {Object} options |
| 11 | + */ |
| 12 | +enforceExistence.configure = function(options) { |
| 13 | + |
| 14 | + // set default policy |
| 15 | + var policy = this._enforceExistencePolicy = { |
| 16 | + all: true, |
| 17 | + anonymous: false, |
| 18 | + exports: true, |
| 19 | + expressions: true |
| 20 | + }; |
| 21 | + |
| 22 | + // check options are valid |
| 23 | + var o = options.enforceExistence; |
| 24 | + assert( |
| 25 | + o === true || o === false || typeof o === 'string' || (typeof o === 'object' && Array.isArray(o.allExcept)), |
| 26 | + 'jsDoc.enforceExistence rule was not configured properly' |
| 27 | + ); |
| 28 | + |
| 29 | + // parse options for policies |
| 30 | + if (o === false) { |
| 31 | + policy.all = false; |
| 32 | + } else if (typeof o === 'string' && o === 'exceptExports') { // backward compatible string option |
| 33 | + policy.exports = false; |
| 34 | + } else if (typeof o === 'object' && Array.isArray(o.allExcept)) { |
| 35 | + if (o.allExcept.indexOf('exports') > -1) { |
| 36 | + policy.exports = false; |
| 37 | + } |
| 38 | + if (o.allExcept.indexOf('expressions') > -1) { |
| 39 | + policy.expressions = false; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | +}; |
| 44 | + |
| 45 | +/** |
| 46 | + * Validator for jsdoc data existence |
9 | 47 | *
|
10 | 48 | * @param {(FunctionDeclaration|FunctionExpression)} node
|
11 | 49 | * @param {Function} err
|
12 | 50 | */
|
13 | 51 | function enforceExistence(node, err) {
|
14 |
| - var reportExports = this._options.enforceExistence !== 'exceptExports'; |
| 52 | + var policy = this._enforceExistencePolicy; |
15 | 53 |
|
16 |
| - // if function is not anonymous or in variabledeclarator or in assignmentexpression |
| 54 | + // enforce 'break-out' policies |
17 | 55 | var parentNode = node.parentNode || {};
|
18 |
| - var needCheck = node.id || |
19 |
| - parentNode.type === 'VariableDeclarator' || |
20 |
| - parentNode.type === 'Property' || |
21 |
| - parentNode.type === 'AssignmentExpression' && parentNode.operator === '='; |
22 |
| - |
23 |
| - if (!reportExports && needCheck && parentNode.type === 'AssignmentExpression') { |
24 |
| - var left = parentNode.left; |
25 |
| - if ((left.object && left.object.name) === 'module' && |
26 |
| - (left.property && left.property.name) === 'exports') { |
27 |
| - needCheck = false; |
| 56 | + |
| 57 | + if (policy.all === false) { |
| 58 | + // don't check anything |
| 59 | + return; |
| 60 | + } |
| 61 | + if (policy.anonymous === false) { |
| 62 | + if (!node.id && ['AssignmentExpression', 'VariableDeclarator', 'Property'].indexOf(parentNode.type) === -1) { |
| 63 | + // don't check anonymous functions |
| 64 | + return; |
| 65 | + } |
| 66 | + } |
| 67 | + if (policy.exports === false) { |
| 68 | + if (parentNode.type === 'AssignmentExpression') { |
| 69 | + var left = parentNode.left; |
| 70 | + if ((left.object && left.object.name) === 'module' && |
| 71 | + (left.property && left.property.name) === 'exports') { |
| 72 | + // don't check exports functions |
| 73 | + return; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + if (policy.expressions === false) { |
| 78 | + if (['AssignmentExpression', 'VariableDeclarator', 'Property'].indexOf(parentNode.type) > -1) { |
| 79 | + // don't check expression functions |
| 80 | + return; |
28 | 81 | }
|
29 | 82 | }
|
30 | 83 |
|
31 |
| - // skip unless jsdoc exists and check is needed |
32 |
| - if (node.jsdoc || !needCheck) { |
| 84 | + // now clear to check for documentation |
| 85 | + if (node.jsdoc) { |
| 86 | + if (!node.jsdoc.valid) { |
| 87 | + err('Invalid jsdoc-block definition', node.jsdoc.loc); |
| 88 | + } |
33 | 89 | return;
|
34 | 90 | }
|
35 | 91 |
|
36 | 92 | // report absence
|
37 |
| - err('jsdoc definition required', node.loc.start); |
| 93 | + err('Expect valid jsdoc-block definition', node.loc.start); |
| 94 | + |
38 | 95 | }
|
0 commit comments