UNPKG

solidity-docgen

Version:

Documentation generator for Solidity smart contracts.

53 lines 2.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getContractsInScope = void 0; const utils_1 = require("solidity-ast/utils"); const map_values_1 = require("./map-values"); const map_keys_1 = require("./map-keys"); function getContractsInScope(item) { const cache = new WeakMap(); return (0, map_values_1.filterValues)(flattenScope(run(item.__item_context.file)), (0, utils_1.isNodeType)('ContractDefinition')); function run(file) { if (cache.has(file)) { return cache.get(file); } const scope = {}; cache.set(file, scope); for (const c of file.nodes) { if ('name' in c) { scope[c.name] = () => ({ definition: c }); } } for (const i of (0, utils_1.findAll)('ImportDirective', file)) { const importedFile = item.__item_context.build.deref('SourceUnit', i.sourceUnit); const importedScope = run(importedFile); if (i.unitAlias) { scope[i.unitAlias] = () => ({ namespace: importedScope }); } else if (i.symbolAliases.length === 0) { Object.assign(scope, importedScope); } else { for (const a of i.symbolAliases) { // Delayed function call supports circular dependencies scope[a.local ?? a.foreign.name] = importedScope[a.foreign.name] ?? (() => importedScope[a.foreign.name]()); } } } ; return scope; } } exports.getContractsInScope = getContractsInScope; function flattenScope(scope) { return Object.fromEntries(Object.entries(scope).flatMap(([k, fn]) => { const v = fn(); if ('definition' in v) { return [[k, v.definition]]; } else { return Object.entries((0, map_keys_1.mapKeys)(flattenScope(v.namespace), k2 => k + '.' + k2)); } })); } //# sourceMappingURL=scope.js.map