Beispiel #1
0
/**
 * Try to find the part of JavaScript a comment is referring to, by
 * looking at the syntax tree closest to that comment.
 *
 * @param {Object} path abstract syntax tree path
 * @returns {?Object} ast path, if one is found.
 * @private
 */
function findTarget(path: ?Object) {
  if (!path) {
    return path;
  }

  if (
    t.isExportDefaultDeclaration(path) ||
    (t.isExportNamedDeclaration(path) && path.has('declaration'))
  ) {
    path = path.get('declaration');
  }

  if (t.isVariableDeclaration(path)) {
    // var x = init;
    path = path.get('declarations')[0];
  } else if (t.isExpressionStatement(path)) {
    // foo.x = TARGET
    path = path.get('expression').get('right');
  } else if (t.isObjectProperty(path)) {
    // var foo = { x: TARGET }; object property
    path = path.get('value');
  } else if (t.isClassProperty(path) && path.get('value').node) {
    // var foo = { x = TARGET }; class property
    path = path.get('value');
  }

  return path.node && path;
}
Beispiel #2
0
 function findKind(path) {
   if (!path) {
     return comment;
   } else if (t.isClassDeclaration(path)) {
     comment.kind = 'class';
   } else if (t.isFunction(path)) {
     if (path.node && (path.node.kind === 'get' || path.node.kind === 'set')) {
       comment.kind = 'member';
     } else if (path.node && path.node.id && path.node.id.name && !!/^[A-Z]/.exec(path.node.id.name)) {
       comment.kind = 'class';
     } else {
       comment.kind = 'function';
     }
   } else if (t.isTypeAlias(path)) {
     comment.kind = 'typedef';
   } else if (t.isVariableDeclaration(path)) {
     if (path.node.kind === 'const') {
       comment.kind = 'constant';
     } else {
       // This behavior is in need of fixing https://github.com/documentationjs/documentation/issues/351
       findKind(path.node.declarations[0].init);
     }
   } else if (t.isExportNamedDeclaration(path) && path.node.declaration) {
     // && makes sure that
     // export { foo } from bar;
     // doesn't check for a non-existent declaration type
     if (path.node.declaration.kind === 'const') {
       comment.kind = 'constant';
     }
   } else if (t.isExpressionStatement(path)) {
     // module.exports = function() {}
     findKind(path.node.expression.right);
   }
 }
Beispiel #3
0
/**
 * Should we pull this component path?
 * Currently: is it considered "top level"? Ie. not in closures/constructors etc.
 */
function shouldPullComponent(path) {
  let tPath = path.parentPath;
  while (tPath) {
    if (T.isProgram(tPath)) {
      return true;
    }

    if (
      !T.isExportDefaultDeclaration(tPath) &&
      !T.isExportNamedDeclaration(tPath) &&
      !T.isVariableDeclaration(tPath)
    ) {
      break;
    }

    tPath = tPath.parentPath;
  }

  return false;
}