All files / dist/helpers ast.js

82.95% Statements 73/88
84.72% Branches 61/72
85.71% Functions 24/28
90.14% Lines 64/71
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    1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x   1x   1x   1x   1x   1x     12x       12x 11x 11x       8x 7x 7x         540x       7x         41x       148x       19x 1x 18x 4x 14x   14x 11x 3x 2x   1x                             51x 279x   33x 33x 25x   1x         24x 24x   21x       246x   125x   279x         5x 16x 9x 7x   7x           4x       33x 15x 15x                              
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.isObjectAssignOrExtendsExpression = isObjectAssignOrExtendsExpression;
exports.isObjectAssignExpression = isObjectAssignExpression;
exports.isExtendsHelperExpression = isExtendsHelperExpression;
exports.isImport = isImport;
exports.isIdentifierNamed = isIdentifierNamed;
exports.isCommentBlock = isCommentBlock;
exports.getIdName = getIdName;
exports.findPropertiesOfNode = findPropertiesOfNode;
exports.getInternalStaticThingsOfClass = getInternalStaticThingsOfClass;
exports.getOtherPropertiesOfIdentifier = getOtherPropertiesOfIdentifier;
exports.getPropertiesOfObjectAssignOrExtendHelper = getPropertiesOfObjectAssignOrExtendHelper;
exports.getPropNames = getPropNames;
exports.groupPropertiesByName = groupPropertiesByName;
exports.convertFunctionDeclarationToExpression = convertFunctionDeclarationToExpression;
exports.convertDeclarationToExpression = convertDeclarationToExpression;
 
var _babelTypes = require('babel-types');
 
var t = _interopRequireWildcard(_babelTypes);
 
var _arrayFlatten = require('array-flatten');
 
var _arrayFlatten2 = _interopRequireDefault(_arrayFlatten);
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
function _interopRequireWildcard(obj) { Eif (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
 
function isObjectAssignOrExtendsExpression(node) {
  return isObjectAssignExpression(node) || isExtendsHelperExpression(node);
}
 
function isObjectAssignExpression(node) {
  if (!t.isCallExpression(node)) return false;
  const callee = node && node.callee;
  return !!(callee.object && callee.property && callee.object.name === 'Object' && callee.property.name === 'assign');
}
 
function isExtendsHelperExpression(node) {
  if (!t.isCallExpression(node)) return false;
  const callee = node && node.callee;
  return isIdentifierNamed(callee, '_extends');
}
 
// t.isImport only exists if the dynamic import syntax plugin is used, so avoid using that to make it optional.
function isImport(node) {
  return node && node.type === 'Import';
}
 
function isIdentifierNamed(node, name) {
  return t.isIdentifier(node, { name: name });
}
 
// This doesn't exist on babel-types
function isCommentBlock(node) {
  return node && node.type === 'CommentBlock';
}
 
function getIdName(node) {
  return node.id && node.id.name;
}
 
function findPropertiesOfNode(blockScopeNode, declaration) {
  if (t.isFunctionDeclaration(declaration) || t.isArrowFunctionExpression(declaration)) {
    return null;
  } else if (t.isObjectExpression(declaration)) {
    return declaration.properties;
  } else Iif (t.isClass(declaration)) {
    return [...getInternalStaticThingsOfClass(declaration), ...getOtherPropertiesOfIdentifier(blockScopeNode, declaration.id.name)];
  } else if (t.isIdentifier(declaration)) {
    return getOtherPropertiesOfIdentifier(blockScopeNode, declaration.name);
  } else if (isObjectAssignOrExtendsExpression(declaration)) {
    return getPropertiesOfObjectAssignOrExtendHelper(declaration, blockScopeNode);
  }
  return null;
}
 
function getInternalStaticThingsOfClass(classNode) {
  return classNode.body.body.filter(item => item.static);
}
 
/**
 * Traverse the top-level of the scope (program or function body) looking for either:
 *  - ObjectExpression assignments to the object variable.
 *  - Property assignments directly to our object (but not to nested properties).
 *
 *  @return Array<{key, value}>
 */
function getOtherPropertiesOfIdentifier(blockScopeNode, idName) {
  return (0, _arrayFlatten2.default)(blockScopeNode.body.map(node => {
    if (t.isExpressionStatement(node)) {
      // ID = value | ID.key = value | ID.key.nested = value
      const { left, right } = node.expression;
      if (t.isAssignmentExpression(node.expression)) {
        if (t.isIdentifier(left) && left.name === idName) {
          // ID = value
          Iif (t.isObjectExpression(right)) {
            // ID = {}
            return right.properties; // Array<ObjectProperty>
          }
        } else {
          const { object, property: key } = left;
          if (t.isIdentifier(object) && object.name === idName) {
            // ID.key = value
            return { key, value: right }; // ObjectProperty-like (key, value)
          }
        }
      }
    } else if (t.isVariableDeclaration(node)) {
      // console.log(require('util').inspect(node, { depth: 4 }));
      return node.declarations.filter(declaration => declaration.id.name === idName).map(declaration => declaration.init).filter(init => init).filter(init => t.isObjectExpression(init) || isObjectAssignOrExtendsExpression(init)).map(init => t.isObjectExpression(init) ? init.properties : getPropertiesOfObjectAssignOrExtendHelper(init, blockScopeNode));
    }
  }).filter(item => item));
}
 
function getPropertiesOfObjectAssignOrExtendHelper(node, blockScopeNode) {
  // Check all the args and recursively try to get props of identifiers (although they may be imported)
  return (0, _arrayFlatten2.default)(node.arguments.map(arg => {
    if (t.isObjectExpression(arg)) {
      return arg.properties;
    } else Eif (t.isIdentifier(arg)) {
      // Recursive, although props will be empty if arg is an imported object
      return getOtherPropertiesOfIdentifier(blockScopeNode, arg.name);
    }
  }));
}
 
function getPropNames(props) {
  return props.map(prop => prop.key.name);
}
 
function groupPropertiesByName(properties) {
  return properties && properties.reduce((accumulator, property) => {
    accumulator[property.key.name] = property.value;
    return accumulator;
  }, {});
}
 
function convertFunctionDeclarationToExpression(declaration) {
  return t.functionExpression(declaration.id, declaration.params, declaration.body, declaration.generator, declaration.async);
}
 
function convertDeclarationToExpression(declaration) {
  if (t.isFunctionDeclaration(declaration)) {
    return convertFunctionDeclarationToExpression(declaration);
  } else {
    // console.log('-----', declaration.type)
    return declaration;
  }
}