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 | 12x 12x 12x 12x 6705x 6705x 476x 476x 476x 476x 476x 476x 476x 476x 476x 7267x 153x 153x 144x 160x 7114x 6594x 16x 16x 6594x 6594x 6649x 6649x 6649x 6649x 6649x 6545x 104x 104x 97x 520x 460x 460x 460x 476x 460x 548x 109x 12x 16x 16x 6203x 16x | "use strict"; const fs = require("fs"); const path = require("path"); const assert = require("assert"); const NodeModule = require("./NodeModule"); /** * @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 = NodeModule.resolve(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) { Iif ( 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 && NodeModule.resolve(node.source.value, dir); for (const specifier of node.specifiers) { Eif (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 = NodeModule.resolve(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 {NodeModule} nodeModule * @param {import('@babel/core')} babel * @returns {Map<string, ModuleExport>} */ module.exports = function getModuleExports(nodeModule, babel) { /** @type {Map<string, ModuleExport>} */ const exports = new Map(); for (const entry of traverseDeep(nodeModule.entry, babel)) { exports.set(entry.external, entry); } return exports; }; |