Code coverage report for lib/lint.js

Statements: 100% (29 / 29)      Branches: 100% (14 / 14)      Functions: 100% (9 / 9)      Lines: 100% (29 / 29)      Ignored: none     

All files » lib/ » lint.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80    26           26                                 26 8 26 13 11             26 16 13     16     26 32 3       16 12     8     26 4 4 5 12 12 12       12         4 2       26 26  
'use strict';
 
var VFile = require('vfile'),
  walk = require('../lib/walk'),
  parseFilepath = require('parse-filepath'),
  vfileSort = require('vfile-sort'),
  reporter = require('vfile-reporter');
 
var CANONICAL = {
  'String': 'string',
  'Boolean': 'boolean',
  'Undefined': 'undefined',
  'Number': 'number',
  'array': 'Array',
  'date': 'Date',
  'object': 'Object'
};
 
/**
 * Passively lints and checks documentation data.
 *
 * @name lint
 * @param {Object} comment parsed comment
 * @returns {Array<Object>} array of errors
 */
function lintComments(comment) {
  comment.tags.forEach(function (tag) {
    function nameInvariant(name) {
      if (CANONICAL[name]) {
        comment.errors.push({
          message: 'type ' + name + ' found, ' + CANONICAL[name] + ' is standard',
          commentLineNumber: tag.lineNumber
        });
      }
    }
 
    function checkCanonical(type) {
      if (type.type === 'NameExpression') {
        nameInvariant(type.name);
      }
 
      [type.elements, type.applications].forEach(checkSubtypes);
    }
 
    function checkSubtypes(subtypes) {
      if (Array.isArray(subtypes)) {
        subtypes.forEach(checkCanonical);
      }
    }
 
    if (tag.title === 'param' && tag.type) {
      checkCanonical(tag.type);
    }
  });
  return comment;
}
 
function formatLint(comments) {
  var vFiles = {};
  walk(comments, function (comment) {
    comment.errors.forEach(function (error) {
      var p = comment.context.file;
      var parts = parseFilepath(p);
      vFiles[p] = vFiles[p] || new VFile({
        directory: parts.dirname,
        filename: parts.basename
      });
      vFiles[p].warn(error.message, {
        line: comment.loc.start.line + error.commentLineNumber || 0
      });
    });
  });
  return reporter(Object.keys(vFiles).map(function (p) {
    return vfileSort(vFiles[p]);
  }));
}
 
module.exports.lintComments = lintComments;
module.exports.formatLint = formatLint;