Code coverage report for lib/infer/membership.js

Statements: 100% (86 / 86)      Branches: 97.83% (90 / 92)      Functions: 100% (11 / 11)      Lines: 100% (86 / 86)      Ignored: none     

All files » lib/infer/ » membership.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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245    26           26   26 30 14     16 16 16 16 16 4                           26 80   80   14       13       119       198 198       80                 26 69 17     52 17     35                     26 65   26 56                                   26 91 22 22             69 69 34 34     69 15 15   54 54 8   46         65   196 17     196 6     190 4     186 186           186     67           186   17           186 67 67 66         186         17   17 4             186       11 11 11         186       2 2       186     6 6 6 2   6       186       2 2 2 1   2     186      
'use strict';
 
var types = require('ast-types'),
  pathParse = require('parse-filepath'),
  shouldSkipInference = require('./should_skip_inference'),
  isJSDocComment = require('../../lib/is_jsdoc_comment'),
  parse = require('../../lib/parse');
 
var n = types.namedTypes;
 
function findLendsIdentifiers(node) {
  if (!node || !node.leadingComments) {
    return;
  }
 
  for (var i = 0; i < node.leadingComments.length; i++) {
    var comment = node.leadingComments[i];
    Eif (isJSDocComment(comment)) {
      var lends = parse(comment.value).lends;
      if (lends) {
        return lends.split('.');
      }
    }
  }
}
 
/**
 * Extract and return the chain of identifiers from the left hand side of expressions
 * of the forms `Foo = ...`, `Foo.bar = ...`, `Foo.bar.baz = ...`, etc.
 *
 * @param {NodePath} path AssignmentExpression, MemberExpression, or Identifier
 * @returns {Array<string>} identifiers
 * @private
 */
function extractIdentifiers(path) {
  var identifiers = [];
 
  types.visit(path, {
    visitNode: function () {
      return false;
    },
 
    visitAssignmentExpression: function (path) {
      this.traverse(path);
    },
 
    visitMemberExpression: function (path) {
      this.traverse(path);
    },
 
    visitIdentifier: function (path) {
      identifiers.push(path.node.name);
      return false;
    }
  });
 
  return identifiers;
}
 
/**
 * Count leading identifiers that refer to a module export (`exports` or `module.exports`).
 * @param {Object} comment parsed comment
 * @param {Array<string>} identifiers array of identifier names
 * @returns {number} number of identifiers referring to a module export (0, 1 or 2)
 */
function countModuleIdentifiers(comment, identifiers) {
  if (identifiers.length >= 1 && identifiers[0] === 'exports') {
    return 1;
  }
 
  if (identifiers.length >= 2 && identifiers[0] === 'module' && identifiers[1] === 'exports') {
    return 2;
  }
 
  return 0;
}
 
/**
 * Uses code structure to infer `memberof`, `instance`, and `static`
 * tags from the placement of JSDoc
 * annotations within a file
 *
 * @param {Object} comment parsed comment
 * @returns {Object} comment with membership inferred
 */
module.exports = function () {
  var currentModule;
 
  function inferModuleName(comment) {
    return (comment.module && comment.module.name) ||
      pathParse(comment.context.file).name;
  }
 
  /**
   * Set `memberof` and `instance`/`static` tags on `comment` based on the
   * array of `identifiers`. If the last element of the `identifiers` is
   * `"prototype"`, it is assumed to be an instance member; otherwise static.
   * If the `identifiers` start with `exports` or `module.exports`, assign
   * membership based on the last seen @module tag or name of the current file.
   *
   * @param {Object} comment comment for which to infer memberships
   * @param {Array<string>} identifiers array of identifier names
   * @param {string} explicitScope if derived from an es6 class, whether or
   * not this method had the static keyword
   * @returns {undefined} mutates `comment`
   * @private
   */
  function inferMembershipFromIdentifiers(comment, identifiers, explicitScope) {
    if (identifiers.length === 1 && identifiers[0] === 'module' && comment.name === 'exports') {
      comment.name = inferModuleName(currentModule || comment);
      return;
    }
 
    /*
     * Test whether identifiers start with a module export (`exports` or `module.exports`),
     * and if so replace those identifiers with the name of the current module.
     */
    var moduleIdentifierCount = countModuleIdentifiers(comment, identifiers);
    if (moduleIdentifierCount) {
      identifiers = identifiers.slice(moduleIdentifierCount);
      identifiers.unshift(inferModuleName(currentModule || comment));
    }
 
    if (identifiers[identifiers.length - 1] === 'prototype') {
      comment.memberof = identifiers.slice(0, -1).join('.');
      comment.scope = 'instance';
    } else {
      comment.memberof = identifiers.join('.');
      if (explicitScope !== undefined) {
        comment.scope = explicitScope;
      } else {
        comment.scope = 'static';
      }
    }
  }
 
  return shouldSkipInference(function inferMembership(comment) {
 
    if (comment.module) {
      currentModule = comment;
    }
 
    if (comment.lends) {
      return;
    }
 
    if (comment.memberof) {
      return comment;
    }
 
    var path = comment.context.ast;
    var identifiers;
 
    /*
     * Deal with an oddity of espree: the jsdoc comment is attached to a different
     * node in the two expressions `a.b = c` vs `a.b = function () {}`.
     */
    if (n.ExpressionStatement.check(path.node) &&
        n.AssignmentExpression.check(path.node.expression) &&
        n.MemberExpression.check(path.node.expression.left)) {
      path = path.get('expression').get('left');
    }
 
    /*
     * Same as above but for `b: c` vs `b: function () {}`.
     */
    if (n.Property.check(path.node) &&
        n.Identifier.check(path.node.key)) {
      path = path.get('key');
    }
 
    // Foo.bar = ...;
    // Foo.prototype.bar = ...;
    // Foo.bar.baz = ...;
    if (n.MemberExpression.check(path.node)) {
      identifiers = extractIdentifiers(path);
      if (identifiers.length >= 2) {
        inferMembershipFromIdentifiers(comment, identifiers.slice(0, -1));
      }
    }
 
    // /** @lends Foo */{ bar: ... }
    if (n.Identifier.check(path.node) &&
      n.Property.check(path.parent.node) &&
      n.ObjectExpression.check(path.parent.parent.node)) {
      // The @lends comment is sometimes attached to the first property rather than
      // the object expression itself.
      identifiers = findLendsIdentifiers(path.parent.parent.node) ||
          findLendsIdentifiers(path.parent.parent.node.properties[0]);
      if (identifiers) {
        inferMembershipFromIdentifiers(comment, identifiers);
      }
    }
 
    // Foo = { bar: ... };
    // Foo.prototype = { bar: ... };
    // Foo.bar = { baz: ... };
    if (n.Identifier.check(path.node) &&
        n.Property.check(path.parent.node) &&
        n.ObjectExpression.check(path.parent.parent.node) &&
        n.AssignmentExpression.check(path.parent.parent.parent.node)) {
      identifiers = extractIdentifiers(path.parent.parent.parent);
      Eif (identifiers.length >= 1) {
        inferMembershipFromIdentifiers(comment, identifiers);
      }
    }
 
    // var Foo = { bar: ... }
    if (n.Identifier.check(path.node) &&
        n.Property.check(path.parent.node) &&
        n.ObjectExpression.check(path.parent.parent.node) &&
        n.VariableDeclarator.check(path.parent.parent.parent.node)) {
      identifiers = [path.parent.parent.parent.node.id.name];
      inferMembershipFromIdentifiers(comment, identifiers);
    }
 
    // class Foo { bar() { } }
    if (n.MethodDefinition.check(path.node) &&
        n.ClassBody.check(path.parent.node) &&
        n.ClassDeclaration.check(path.parent.parent.node)) {
      identifiers = [path.parent.parent.node.id.name];
      var scope = 'instance';
      if (path.node.static == true) {
        scope = 'static';
      }
      inferMembershipFromIdentifiers(comment, identifiers, scope);
    }
 
    // var Foo = class { bar() { } }
    if (n.MethodDefinition.check(path.node) &&
        n.ClassBody.check(path.parent.node) &&
        n.ClassExpression.check(path.parent.parent.node) &&
        n.AssignmentExpression.check(path.parent.parent.parent.node)) {
      identifiers = extractIdentifiers(path.parent.parent.parent.node);
      scope = 'instance';
      if (path.node.static == true) {
        scope = 'static';
      }
      inferMembershipFromIdentifiers(comment, identifiers, scope);
    }
 
    return comment;
  });
};