All files getModuleExports.js

88.76% Statements 150/169
89.66% Branches 26/29
100% Functions 3/3
88.76% Lines 150/169

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 1701x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 364x 364x 364x 364x 1x 1x 1x 1x 1x 1x 155x 155x 155x 155x 155x 155x 155x 155x 155x 155x 155x 155x 155x 155x 155x 155x 155x 155x 155x 155x 155x 155x 155x 155x 155x 538x 29x 29x 29x 27x 34x 34x 34x 32x 32x 13x 13x     34x 34x 27x 538x 344x 4x 4x 4x 4x 4x                               4x 4x 344x 344x 344x 344x 354x 354x 354x     354x 354x 354x 354x 330x 330x 330x 330x 330x 354x 24x 24x 24x 23x 23x 23x 23x 23x 23x 24x 354x 354x 509x 152x 152x 152x 152x 152x 152x 152x 152x 152x 538x 155x 155x 152x 152x 183x 183x 183x 183x 183x 37x 37x 183x 152x 155x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 207x 207x 3x 3x 3x  
"use strict";
 
const fs = require("fs");
const path = require("path");
const assert = require("assert");
const resolveModule = require("./resolveModule");
const resolvePackageEntry = require("./resolvePackageEntry");
 
/**
 * @typedef {object} ModuleExport
 * @property {string} source
 * @property {string} external
 * @property {string} internal
 */
 
/**
 * @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
 * @param {import('@babel/core')} babel
 * @returns {Generator<ModuleExport, void, any>}
 */
function* traverseDeep(filename, babel) {
  const { types: t } = babel;
  const content = fs.readFileSync(filename, "utf-8");
  const ast = babel.parse(content, {
    filename,
    ast: true,
    babelrc: false,
    configFile: false,
    sourceType: "module",
  });
 
  assert.ok(t.isFile(ast));
 
  const dir = path.dirname(filename);
 
  /** @type {Map<string, { name: string; source: string }>} */
  const imports = new Map();
 
  /** @type {Set<string>} */
  const exportedNames = new Set();
 
  /** @type {Set<string>} */
  const reexportedModules = new Set();
 
  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) {
              const external = declaration.id.name;

              exportedNames.add(external);

              yield {
                external,
                source: imported.source,
                internal: imported.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;
 
          exportedNames.add(external);
 
          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}'`
      );
 
      reexportedModules.add(sourcePath);
    }
  }
 
  for (const reexportedModule of reexportedModules) {
    // https://262.ecma-international.org/6.0/#sec-getexportednames
    for (const entry of traverseDeep(reexportedModule, babel)) {
      if (
        // https://exploringjs.com/es6/leanpub-endnotes.html#fn-modules_2
        entry.external !== "default" &&
        !exportedNames.has(entry.external)
      ) {
        yield entry;
      }
    }
  }
}
 
/**
 * @param {string} name
 * @param {string | undefined} indexFile
 * @param {import('@babel/core')} babel
 * @returns {Map<string, ModuleExport>}
 */
module.exports = function getModuleExports(name, indexFile, babel) {
  /** @type {Map<string, ModuleExport>} */
  const exports = new Map();
  const packageEntry = resolvePackageEntry(name, indexFile);
 
  for (const entry of traverseDeep(packageEntry, babel)) {
    exports.set(entry.external, entry);
  }
 
  return exports;
};