All files / lib/internal getModuleExports.js

82.46% Statements 141/171
85.19% Branches 23/27
100% Functions 4/4
82.46% Lines 141/171

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 1721x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 13x 13x 13x 13x 13x 13x 13x 13x                               1x 1x 1x 1x 1x 6374x 6374x 6374x 6374x 1x 1x 1x 1x 1x 347x 347x 347x 347x 347x 347x 347x 347x 347x 347x 347x 347x 347x 347x 347x 347x 347x 347x 6800x 112x 112x 112x 104x 106x 106x 106x 102x 102x 18x 18x     106x 106x 104x 6800x 6303x 12x 12x 12x 12x 12x                       12x 12x 6303x 6303x 6303x 6303x 6325x 6325x 6325x     6325x 6325x 6268x 6268x 6268x 6268x 6268x 6325x 57x 57x 57x 52x 52x 52x 52x 52x 52x 57x 6325x 6325x 6688x 334x 334x 334x 334x 334x 334x 334x 334x 334x 6800x 347x 1x 1x 1x 1x 1x 1x 1x 13x 13x 13x 13x 6320x 6320x 13x 13x 13x  
"use strict";
 
const fs = require("fs");
const path = require("path");
const assert = require("assert");
const babel = require("@babel/core");
const resolveModule = require("./resolveModule");
 
const { types: t } = babel;
 
/**
 * @typedef {object} ModuleExport
 * @property {string} source
 * @property {string} external
 * @property {string} internal
 */
 
/**
 * @param {string} name
 * @param {string} [indexFile]
 * @returns {string}
 */
function resolveEntry(name, indexFile) {
  if (!indexFile) {
    const indexFilePath = resolveModule(name);
 
    assert.ok(indexFilePath, `failed to find 'indexFile' of '${name}'.`);
 
    return indexFilePath;
  }

  const pkgJSON = resolveModule(`${name}/package.json`);

  assert.ok(pkgJSON, `failed to resolve 'package.json' for '${name}'.`);

  const pkgDir = path.dirname(pkgJSON);
  const indexFilePath = resolveModule(path.join(pkgDir, indexFile));

  assert.ok(
    indexFilePath,
    `failed to resolve 'indexFile' specified for '${name}'.`
  );

  return indexFilePath;
}
 
/**
 * @param {string} source
 * @returns {string}
 */
function makeRelativeSource(source) {
  const chunks = source.split(path.sep);
  return chunks.slice(chunks.lastIndexOf("node_modules") + 1).join("/");
}
 
/**
 * @param {string} filename
 * @returns {Generator<ModuleExport, void, any>}
 */
function* traverseDeep(filename) {
  const content = fs.readFileSync(filename, "utf-8");
  const ast = babel.parse(content, {
    filename,
    ast: true,
    babelrc: false,
    configFile: false,
    sourceType: "module",
  });
 
  assert.ok(babel.types.isFile(ast));
 
  const dir = path.dirname(filename);
 
  /** @type {Map<string, { name: string; source: string }>} */
  const imports = new Map();
 
  for (const node of ast.program.body) {
    if (t.isImportDeclaration(node)) {
      const sourcePath = resolveModule(node.source.value, dir);
 
      if (sourcePath) {
        for (const specifier of node.specifiers) {
          imports.set(specifier.local.name, {
            source: makeRelativeSource(sourcePath),
            name: t.isImportNamespaceSpecifier(specifier)
              ? "*"
              : t.isImportDefaultSpecifier(specifier)
              ? "default"
              : t.isIdentifier(specifier.imported)
              ? specifier.imported.name
              : specifier.imported.value,
          });
        }
      }
    } else if (t.isExportNamedDeclaration(node)) {
      if (t.isVariableDeclaration(node.declaration)) {
        for (const declaration of node.declaration.declarations) {
          if (
            t.isIdentifier(declaration.id) &&
            t.isIdentifier(declaration.init)
          ) {
            const internal = declaration.init.name;
            const imported = imports.get(internal);

            if (imported) {
              yield {
                source: imported.source,
                internal: imported.name,
                external: declaration.id.name,
              };
            }
          }
        }
      }
 
      const sourcePath = node.source && resolveModule(node.source.value, dir);
 
      for (const specifier of node.specifiers) {
        if (t.isExportSpecifier(specifier)) {
          const internal = specifier.local.name;
          const external = t.isIdentifier(specifier.exported)
            ? specifier.exported.name
            : specifier.exported.value;
 
          if (sourcePath) {
            yield {
              internal,
              external,
              source: makeRelativeSource(sourcePath),
            };
          } else {
            const imported = imports.get(internal);
 
            if (imported) {
              yield {
                external,
                source: imported.source,
                internal: imported.name,
              };
            }
          }
        }
      }
    } else if (t.isExportAllDeclaration(node)) {
      const sourcePath = resolveModule(node.source.value, dir);
 
      assert.ok(
        sourcePath,
        `failed to resolve '${node.source.value}' from '${dir}'`
      );
 
      yield* traverseDeep(sourcePath);
    }
  }
}
 
/**
 * @param {string} name
 * @param {string} [indexFile]
 * @returns {Map<string, ModuleExport>}
 */
module.exports = function getModuleExports(name, indexFile) {
  /** @type {Map<string, ModuleExport>} */
  const exports = new Map();
 
  for (const entry of traverseDeep(resolveEntry(name, indexFile))) {
    exports.set(entry.external, entry);
  }
 
  return exports;
};