Code coverage report for lib/infer/name.js

Statements: 100% (29 / 29)      Branches: 100% (20 / 20)      Functions: 100% (5 / 5)      Lines: 100% (29 / 29)      Ignored: none     

All files » lib/infer/ » name.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    26                       26 65   155 3 3     152 1 1     151 1 1     150 9 9     141 4 4     137 1 1               136   308 131 131   177         273       35       136      
'use strict';
 
var types = require('ast-types'),
  shouldSkipInference = require('./should_skip_inference'),
  pathParse = require('parse-filepath');
 
/**
 * Infers a `name` tag from the context,
 * and adopt `@class` and other other tags as implied name tags.
 *
 * @name inferName
 * @param {Object} comment parsed comment
 * @returns {Object} comment with name inferred
 */
module.exports = function () {
  return shouldSkipInference(function inferName(comment) {
 
    if (comment.event) {
      comment.name = comment.event;
      return comment;
    }
 
    if (comment.alias) {
      comment.name = comment.alias;
      return comment;
    }
 
    if (comment.callback) {
      comment.name = comment.callback;
      return comment;
    }
 
    if (comment.class && comment.class.name) {
      comment.name = comment.class.name;
      return comment;
    }
 
    if (comment.module) {
      comment.name = comment.module.name || pathParse(comment.context.file).name;
      return comment;
    }
 
    if (comment.typedef) {
      comment.name = comment.typedef.name;
      return comment;
    }
 
    // The strategy here is to do a depth-first traversal of the AST,
    // looking for nodes with a "name" property, with exceptions as needed.
    // For example, name inference for a MemberExpression `foo.bar = baz` will
    // infer the named based on the `property` of the MemberExpression (`bar`)
    // rather than the `object` (`foo`).
    types.visit(comment.context.ast, {
      inferName: function (path, value) {
        if (value && value.name) {
          comment.name = value.name;
          this.abort();
        } else {
          this.traverse(path);
        }
      },
 
      visitNode: function (path) {
        this.inferName(path, path.value);
      },
 
      visitMemberExpression: function (path) {
        this.inferName(path, path.value.property);
      }
    });
 
    return comment;
  });
};