Code coverage report for lib/infer/kind.js

Statements: 100% (26 / 26)      Branches: 100% (12 / 12)      Functions: 100% (6 / 6)      Lines: 100% (26 / 26)      Ignored: none     

All files » lib/infer/ » kind.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    26     26                   26 65 156 1     155 1539 1539 23   23       132   3 3     115 1 1   114 114       3 3     10 2 2   8         132      
'use strict';
 
var types = require('ast-types'),
  shouldSkipInference = require('./should_skip_inference');
 
var kindShorthands = ['class', 'constant', 'event', 'external', 'file',
  'function', 'member', 'mixin', 'module', 'namespace', 'typedef'];
 
/**
 * Infers a `kind` tag from other tags or from the context.
 *
 * @name inferKind
 * @param {Object} comment parsed comment
 * @returns {Object} comment with kind inferred
 */
module.exports = function () {
  return shouldSkipInference(function inferKind(comment) {
    if (comment.kind) {
      return comment;
    }
 
    for (var i = 0; i < kindShorthands.length; i++) {
      var kind = kindShorthands[i];
      if (kind in comment) {
        comment.kind = kind;
        // only allow a comment to have one kind
        return comment;
      }
    }
 
    types.visit(comment.context.ast, {
      visitClassDeclaration: function () {
        comment.kind = 'class';
        this.abort();
      },
      visitFunction: function (path) {
        if (path.value && path.value.id && path.value.id.name && !!/^[A-Z]/.exec(path.value.id.name)) {
          comment.kind = 'class';
          this.abort();
        } else {
          comment.kind = 'function';
          this.abort();
        }
      },
      visitTypeAlias: function () {
        comment.kind = 'typedef';
        this.abort();
      },
      visitVariableDeclaration: function (path) {
        if (path.value.kind === 'const') {
          comment.kind = 'constant';
          this.abort();
        } else {
          this.traverse(path);
        }
      }
    });
 
    return comment;
  });
};